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.classic;
29
30 import java.io.IOException;
31
32 import org.apache.hc.core5.http.ClassicHttpResponse;
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.URIScheme;
37 import org.apache.hc.core5.http.impl.bootstrap.HttpRequester;
38 import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
39 import org.apache.hc.core5.http.impl.bootstrap.StandardFilter;
40 import org.apache.hc.core5.http.impl.routing.RequestRouter;
41 import org.apache.hc.core5.http.io.HttpFilterChain;
42 import org.apache.hc.core5.http.io.HttpRequestHandler;
43 import org.apache.hc.core5.http.io.SocketConfig;
44 import org.apache.hc.core5.testing.classic.extension.HttpRequesterResource;
45 import org.apache.hc.core5.testing.classic.extension.HttpServerResource;
46 import org.apache.hc.core5.testing.extension.SocksProxyResource;
47 import org.apache.hc.core5.util.Timeout;
48 import org.junit.jupiter.api.Order;
49 import org.junit.jupiter.api.extension.RegisterExtension;
50
51 public abstract class ClassicHttp1SocksProxyCoreTransportTest extends ClassicHttpCoreTransportTest {
52
53 private static final Timeout TIMEOUT = Timeout.ofMinutes(1);
54
55 @RegisterExtension
56 @Order(-Integer.MAX_VALUE)
57 private final SocksProxyResource proxyResource;
58 @RegisterExtension
59 private final HttpServerResource serverResource;
60 @RegisterExtension
61 private final HttpRequesterResource clientResource;
62
63 public ClassicHttp1SocksProxyCoreTransportTest(final URIScheme scheme) {
64 super(scheme);
65 this.proxyResource = new SocksProxyResource();
66 this.serverResource = new HttpServerResource(scheme, bootstrap -> bootstrap
67 .setSocketConfig(SocketConfig.custom()
68 .setSoTimeout(TIMEOUT)
69 .build())
70 .setRequestRouter(RequestRouter.<HttpRequestHandler>builder()
71 .addRoute(RequestRouter.LOCAL_AUTHORITY, "*", new EchoHandler())
72 .resolveAuthority(RequestRouter.LOCAL_AUTHORITY_RESOLVER)
73 .build())
74 .addFilterBefore(StandardFilter.MAIN_HANDLER.name(), "no-keep-alive", (request, responseTrigger, context, chain) ->
75 chain.proceed(request, new HttpFilterChain.ResponseTrigger() {
76
77 @Override
78 public void sendInformation(
79 final ClassicHttpResponse response) throws HttpException, IOException {
80 responseTrigger.sendInformation(response);
81 }
82
83 @Override
84 public void submitResponse(
85 final ClassicHttpResponse response) throws HttpException, IOException {
86 if (request.getPath().startsWith("/no-keep-alive")) {
87 response.setHeader(HttpHeaders.CONNECTION, HeaderElements.CLOSE);
88 }
89 responseTrigger.submitResponse(response);
90 }
91
92 }, context)));
93 this.clientResource = new HttpRequesterResource(bootstrap -> bootstrap
94 .setSocketConfig(SocketConfig.custom()
95 .setSocksProxyAddress(proxyResource.proxy().getProxyAddress())
96 .setSoTimeout(TIMEOUT)
97 .build()));
98 }
99
100 @Override
101 HttpServer serverStart() throws IOException {
102 return serverResource.start();
103 }
104
105 @Override
106 HttpRequester clientStart() throws IOException {
107 return clientResource.start();
108 }
109
110 }