1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.testing.nio;
29
30 import static org.hamcrest.MatcherAssert.assertThat;
31
32 import java.net.InetSocketAddress;
33 import java.util.concurrent.Future;
34
35 import org.apache.hc.core5.concurrent.BasicFuture;
36 import org.apache.hc.core5.concurrent.FutureContribution;
37 import org.apache.hc.core5.http.ContentType;
38 import org.apache.hc.core5.http.HttpHost;
39 import org.apache.hc.core5.http.HttpResponse;
40 import org.apache.hc.core5.http.HttpStatus;
41 import org.apache.hc.core5.http.Message;
42 import org.apache.hc.core5.http.Method;
43 import org.apache.hc.core5.http.URIScheme;
44 import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester;
45 import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer;
46 import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
47 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
48 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
49 import org.apache.hc.core5.http.nio.ssl.TlsUpgradeCapable;
50 import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
51 import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
52 import org.apache.hc.core5.http.protocol.UriPatternMatcher;
53 import org.apache.hc.core5.reactor.IOReactorConfig;
54 import org.apache.hc.core5.reactor.ListenerEndpoint;
55 import org.apache.hc.core5.reactor.ProtocolIOSession;
56 import org.apache.hc.core5.reactor.ssl.TlsDetails;
57 import org.apache.hc.core5.testing.nio.extension.HttpAsyncRequesterResource;
58 import org.apache.hc.core5.testing.nio.extension.HttpAsyncServerResource;
59 import org.apache.hc.core5.util.Timeout;
60 import org.hamcrest.CoreMatchers;
61 import org.junit.jupiter.api.Assertions;
62 import org.junit.jupiter.api.Test;
63 import org.junit.jupiter.api.extension.RegisterExtension;
64
65 public class TLSUpgradeTest {
66
67 private static final Timeout TIMEOUT = Timeout.ofSeconds(30);
68
69 @RegisterExtension
70 private final HttpAsyncServerResource serverResource;
71 @RegisterExtension
72 private final HttpAsyncRequesterResource clientResource;
73
74 public TLSUpgradeTest() {
75 this.serverResource = new HttpAsyncServerResource(bootstrap -> bootstrap
76 .setIOReactorConfig(
77 IOReactorConfig.custom()
78 .setSoTimeout(TIMEOUT)
79 .build())
80 .setLookupRegistry(new UriPatternMatcher<>())
81 .register("*", () -> new EchoHandler(2048))
82 );
83 this.clientResource = new HttpAsyncRequesterResource(bootstrap -> bootstrap
84 .setIOReactorConfig(IOReactorConfig.custom()
85 .setSoTimeout(TIMEOUT)
86 .build())
87 );
88 }
89
90 @Test
91 public void testTLSUpgrade() throws Exception {
92 final HttpAsyncServer server = serverResource.start();
93 final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS);
94 final ListenerEndpoint listener = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
95 final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
96 final HttpAsyncRequester requester = clientResource.start();
97
98 final HttpHost target = new HttpHost(URIScheme.HTTPS.id, "localhost", address.getPort());
99 final Future<Message<HttpResponse, String>> resultFuture1 = requester.execute(
100 new BasicRequestProducer(Method.POST, target, "/stuff",
101 new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)),
102 new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
103 final Message<HttpResponse, String> message1 = resultFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
104 assertThat(message1, CoreMatchers.notNullValue());
105 final HttpResponse response1 = message1.getHead();
106 assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
107 final String body1 = message1.getBody();
108 assertThat(body1, CoreMatchers.equalTo("some stuff"));
109
110
111 final Future<AsyncClientEndpoint> endpointFuture = requester.connect(
112 new HttpHost(URIScheme.HTTP.id, "localhost", address.getPort()), TIMEOUT);
113
114 final AsyncClientEndpoint clientEndpoint = endpointFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
115 Assertions.assertInstanceOf(TlsUpgradeCapable.class, clientEndpoint);
116
117
118 final BasicFuture<TlsDetails> tlsFuture = new BasicFuture<>(null);
119 ((TlsUpgradeCapable) clientEndpoint).tlsUpgrade(target, new FutureContribution<ProtocolIOSession>(tlsFuture) {
120
121 @Override
122 public void completed(final ProtocolIOSession protocolIOSession) {
123 tlsFuture.completed(protocolIOSession.getTlsDetails());
124 }
125
126 });
127
128 final TlsDetails tlsDetails = tlsFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
129 Assertions.assertNotNull(tlsDetails);
130
131
132 final Future<Message<HttpResponse, String>> resultFuture2 = clientEndpoint.execute(
133 new BasicRequestProducer(Method.POST, target, "/stuff",
134 new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)),
135 new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
136 final Message<HttpResponse, String> message2 = resultFuture2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
137 assertThat(message2, CoreMatchers.notNullValue());
138 final HttpResponse response2 = message2.getHead();
139 assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
140 final String body2 = message2.getBody();
141 assertThat(body2, CoreMatchers.equalTo("some stuff"));
142 }
143
144 }