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