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 java.io.IOException;
31
32 import org.apache.hc.core5.function.Supplier;
33 import org.apache.hc.core5.http.HeaderElements;
34 import org.apache.hc.core5.http.HttpException;
35 import org.apache.hc.core5.http.HttpHeaders;
36 import org.apache.hc.core5.http.HttpRequest;
37 import org.apache.hc.core5.http.HttpResponse;
38 import org.apache.hc.core5.http.URIScheme;
39 import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester;
40 import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer;
41 import org.apache.hc.core5.http.impl.bootstrap.StandardFilter;
42 import org.apache.hc.core5.http.impl.routing.RequestRouter;
43 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
44 import org.apache.hc.core5.http.nio.AsyncFilterChain;
45 import org.apache.hc.core5.http.nio.AsyncPushProducer;
46 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
47 import org.apache.hc.core5.reactor.IOReactorConfig;
48 import org.apache.hc.core5.testing.extension.SocksProxyResource;
49 import org.apache.hc.core5.testing.nio.extension.HttpAsyncRequesterResource;
50 import org.apache.hc.core5.testing.nio.extension.HttpAsyncServerResource;
51 import org.apache.hc.core5.util.Timeout;
52 import org.junit.jupiter.api.Order;
53 import org.junit.jupiter.api.extension.RegisterExtension;
54
55 public abstract class Http1SocksProxyCoreTransportTest extends HttpCoreTransportTest {
56
57 private static final Timeout TIMEOUT = Timeout.ofMinutes(1);
58
59 @RegisterExtension
60 @Order(-Integer.MAX_VALUE)
61 private final SocksProxyResource proxyResource;
62 @RegisterExtension
63 private final HttpAsyncServerResource serverResource;
64 @RegisterExtension
65 private final HttpAsyncRequesterResource clientResource;
66
67 public Http1SocksProxyCoreTransportTest(final URIScheme scheme) {
68 super(scheme);
69 this.proxyResource = new SocksProxyResource();
70 this.serverResource = new HttpAsyncServerResource(bootstrap -> bootstrap
71 .setIOReactorConfig(
72 IOReactorConfig.custom()
73 .setSoTimeout(TIMEOUT)
74 .build())
75 .setRequestRouter(RequestRouter.<Supplier<AsyncServerExchangeHandler>>builder()
76 .addRoute(RequestRouter.LOCAL_AUTHORITY, "*", () -> new EchoHandler(2048))
77 .resolveAuthority(RequestRouter.LOCAL_AUTHORITY_RESOLVER)
78 .build())
79 .addFilterBefore(StandardFilter.MAIN_HANDLER.name(), "no-keepalive", (request, entityDetails, context, responseTrigger, chain) ->
80 chain.proceed(request, entityDetails, context, new AsyncFilterChain.ResponseTrigger() {
81
82 @Override
83 public void sendInformation(
84 final HttpResponse response) throws HttpException, IOException {
85 responseTrigger.sendInformation(response);
86 }
87
88 @Override
89 public void submitResponse(
90 final HttpResponse response,
91 final AsyncEntityProducer entityProducer) throws HttpException, IOException {
92 if (request.getPath().startsWith("/no-keep-alive")) {
93 response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
94 }
95 responseTrigger.submitResponse(response, entityProducer);
96 }
97
98 @Override
99 public void pushPromise(
100 final HttpRequest promise,
101 final AsyncPushProducer responseProducer) throws HttpException, IOException {
102 responseTrigger.pushPromise(promise, responseProducer);
103 }
104
105 }))
106 );
107 this.clientResource = new HttpAsyncRequesterResource(bootstrap -> bootstrap
108 .setIOReactorConfig(IOReactorConfig.custom()
109 .setSocksProxyAddress(proxyResource.proxy().getProxyAddress())
110 .setSoTimeout(TIMEOUT)
111 .build())
112 );
113 }
114
115 @Override
116 HttpAsyncServer serverStart() throws IOException {
117 return serverResource.start();
118 }
119
120 @Override
121 HttpAsyncRequester clientStart() {
122 return clientResource.start();
123 }
124
125 }