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 static org.hamcrest.MatcherAssert.assertThat;
31
32 import java.io.IOException;
33
34 import org.apache.hc.core5.http.ClassicHttpRequest;
35 import org.apache.hc.core5.http.ClassicHttpResponse;
36 import org.apache.hc.core5.http.ContentType;
37 import org.apache.hc.core5.http.Header;
38 import org.apache.hc.core5.http.HttpException;
39 import org.apache.hc.core5.http.HttpHost;
40 import org.apache.hc.core5.http.HttpStatus;
41 import org.apache.hc.core5.http.Method;
42 import org.apache.hc.core5.http.URIScheme;
43 import org.apache.hc.core5.http.impl.bootstrap.HttpRequester;
44 import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
45 import org.apache.hc.core5.http.io.HttpFilterChain;
46 import org.apache.hc.core5.http.io.SocketConfig;
47 import org.apache.hc.core5.http.io.entity.StringEntity;
48 import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
49 import org.apache.hc.core5.http.protocol.HttpCoreContext;
50 import org.apache.hc.core5.testing.classic.extension.HttpRequesterResource;
51 import org.apache.hc.core5.testing.classic.extension.HttpServerResource;
52 import org.apache.hc.core5.util.Timeout;
53 import org.hamcrest.CoreMatchers;
54 import org.junit.jupiter.api.Test;
55 import org.junit.jupiter.api.extension.RegisterExtension;
56
57 public abstract class ClassicServerBootstrapFilterTest {
58
59 private static final Timeout TIMEOUT = Timeout.ofMinutes(1);
60
61 private final URIScheme scheme;
62
63 @RegisterExtension
64 private final HttpServerResource serverResource;
65
66 @RegisterExtension
67 private final HttpRequesterResource clientResource;
68
69 public ClassicServerBootstrapFilterTest(final URIScheme scheme) {
70 this.scheme = scheme;
71 this.serverResource = new HttpServerResource(scheme, bootstrap -> bootstrap
72 .setSocketConfig(SocketConfig.custom()
73 .setSoTimeout(TIMEOUT)
74 .build())
75 .register("*", new EchoHandler())
76 .addFilterLast("test-filter", (request, responseTrigger, context, chain) ->
77 chain.proceed(request, new HttpFilterChain.ResponseTrigger() {
78
79 @Override
80 public void sendInformation(
81 final ClassicHttpResponse response) throws HttpException, IOException {
82 responseTrigger.sendInformation(response);
83 }
84
85 @Override
86 public void submitResponse(
87 final ClassicHttpResponse response) throws HttpException, IOException {
88 response.setHeader("X-Test-Filter", "active");
89 responseTrigger.submitResponse(response);
90 }
91
92 }, context)));
93
94 this.clientResource = new HttpRequesterResource(bootstrap -> bootstrap
95 .setSocketConfig(SocketConfig.custom()
96 .setSoTimeout(TIMEOUT)
97 .build()));
98 }
99
100 @Test
101 public void testFilters() throws Exception {
102 final HttpServer server = serverResource.start();
103 final HttpRequester requester = clientResource.start();
104
105 server.start();
106 final HttpHost target = new HttpHost(scheme.id, "localhost", server.getLocalPort());
107 final HttpCoreContext context = HttpCoreContext.create();
108 final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST, "/filters");
109 request.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
110 try (final ClassicHttpResponse response = requester.execute(target, request, TIMEOUT, context)) {
111 assertThat(response.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
112 final Header testFilterHeader = response.getHeader("X-Test-Filter");
113 assertThat(testFilterHeader, CoreMatchers.notNullValue());
114 }
115 }
116
117 }