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