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.function.Supplier;
38 import org.apache.hc.core5.http.ContentType;
39 import org.apache.hc.core5.http.HttpHost;
40 import org.apache.hc.core5.http.HttpResponse;
41 import org.apache.hc.core5.http.HttpStatus;
42 import org.apache.hc.core5.http.Message;
43 import org.apache.hc.core5.http.Method;
44 import org.apache.hc.core5.http.URIScheme;
45 import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester;
46 import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer;
47 import org.apache.hc.core5.http.impl.routing.RequestRouter;
48 import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
49 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
50 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
51 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
52 import org.apache.hc.core5.http.nio.ssl.TlsUpgradeCapable;
53 import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
54 import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
55 import org.apache.hc.core5.reactor.IOReactorConfig;
56 import org.apache.hc.core5.reactor.ListenerEndpoint;
57 import org.apache.hc.core5.reactor.ProtocolIOSession;
58 import org.apache.hc.core5.reactor.ssl.TlsDetails;
59 import org.apache.hc.core5.testing.nio.extension.HttpAsyncRequesterResource;
60 import org.apache.hc.core5.testing.nio.extension.HttpAsyncServerResource;
61 import org.apache.hc.core5.util.Timeout;
62 import org.hamcrest.CoreMatchers;
63 import org.junit.jupiter.api.Assertions;
64 import org.junit.jupiter.api.Test;
65 import org.junit.jupiter.api.extension.RegisterExtension;
66
67 public class TLSUpgradeTest {
68
69 private static final Timeout TIMEOUT = Timeout.ofSeconds(30);
70
71 @RegisterExtension
72 private final HttpAsyncServerResource serverResource;
73 @RegisterExtension
74 private final HttpAsyncRequesterResource clientResource;
75
76 public TLSUpgradeTest() {
77 this.serverResource = new HttpAsyncServerResource(bootstrap -> bootstrap
78 .setIOReactorConfig(
79 IOReactorConfig.custom()
80 .setSoTimeout(TIMEOUT)
81 .build())
82 .setRequestRouter(RequestRouter.<Supplier<AsyncServerExchangeHandler>>builder()
83 .addRoute(RequestRouter.LOCAL_AUTHORITY, "*", () -> new EchoHandler(2048))
84 .resolveAuthority(RequestRouter.LOCAL_AUTHORITY_RESOLVER)
85 .build())
86 );
87 this.clientResource = new HttpAsyncRequesterResource(bootstrap -> bootstrap
88 .setIOReactorConfig(IOReactorConfig.custom()
89 .setSoTimeout(TIMEOUT)
90 .build())
91 );
92 }
93
94 @Test
95 public void testTLSUpgrade() throws Exception {
96 final HttpAsyncServer server = serverResource.start();
97 final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS);
98 final ListenerEndpoint listener = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
99 final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
100 final HttpAsyncRequester requester = clientResource.start();
101
102 final HttpHost target = new HttpHost(URIScheme.HTTPS.id, "localhost", address.getPort());
103 final Future<Message<HttpResponse, String>> resultFuture1 = requester.execute(
104 new BasicRequestProducer(Method.POST, target, "/stuff",
105 new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)),
106 new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
107 final Message<HttpResponse, String> message1 = resultFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
108 assertThat(message1, CoreMatchers.notNullValue());
109 final HttpResponse response1 = message1.getHead();
110 assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
111 final String body1 = message1.getBody();
112 assertThat(body1, CoreMatchers.equalTo("some stuff"));
113
114
115 final Future<AsyncClientEndpoint> endpointFuture = requester.connect(
116 new HttpHost(URIScheme.HTTP.id, "localhost", address.getPort()), TIMEOUT);
117
118 final AsyncClientEndpoint clientEndpoint = endpointFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
119 Assertions.assertInstanceOf(TlsUpgradeCapable.class, clientEndpoint);
120
121
122 final BasicFuture<TlsDetails> tlsFuture = new BasicFuture<>(null);
123 ((TlsUpgradeCapable) clientEndpoint).tlsUpgrade(target, new FutureContribution<ProtocolIOSession>(tlsFuture) {
124
125 @Override
126 public void completed(final ProtocolIOSession protocolIOSession) {
127 tlsFuture.completed(protocolIOSession.getTlsDetails());
128 }
129
130 });
131
132 final TlsDetails tlsDetails = tlsFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
133 Assertions.assertNotNull(tlsDetails);
134
135
136 final Future<Message<HttpResponse, String>> resultFuture2 = clientEndpoint.execute(
137 new BasicRequestProducer(Method.POST, target, "/stuff",
138 new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)),
139 new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
140 final Message<HttpResponse, String> message2 = resultFuture2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
141 assertThat(message2, CoreMatchers.notNullValue());
142 final HttpResponse response2 = message2.getHead();
143 assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
144 final String body2 = message2.getBody();
145 assertThat(body2, CoreMatchers.equalTo("some stuff"));
146 }
147
148 }