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 static org.hamcrest.MatcherAssert.assertThat;
31
32 import java.io.IOException;
33 import java.net.InetSocketAddress;
34 import java.util.concurrent.Future;
35
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.HttpRequest;
41 import org.apache.hc.core5.http.HttpResponse;
42 import org.apache.hc.core5.http.HttpStatus;
43 import org.apache.hc.core5.http.Message;
44 import org.apache.hc.core5.http.Method;
45 import org.apache.hc.core5.http.URIScheme;
46 import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester;
47 import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer;
48 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
49 import org.apache.hc.core5.http.nio.AsyncFilterChain;
50 import org.apache.hc.core5.http.nio.AsyncPushProducer;
51 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
52 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
53 import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
54 import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
55 import org.apache.hc.core5.http.protocol.UriPatternMatcher;
56 import org.apache.hc.core5.reactor.IOReactorConfig;
57 import org.apache.hc.core5.reactor.ListenerEndpoint;
58 import org.apache.hc.core5.testing.nio.extension.HttpAsyncRequesterResource;
59 import org.apache.hc.core5.testing.nio.extension.HttpAsyncServerResource;
60 import org.apache.hc.core5.util.Timeout;
61 import org.hamcrest.CoreMatchers;
62 import org.junit.jupiter.api.Test;
63 import org.junit.jupiter.api.extension.RegisterExtension;
64
65 public abstract class AsyncServerBootstrapFilterTest {
66
67 private static final Timeout TIMEOUT = Timeout.ofMinutes(1);
68
69 @RegisterExtension
70 private final HttpAsyncServerResource serverResource = new HttpAsyncServerResource(bootstrap -> bootstrap
71 .setIOReactorConfig(
72 IOReactorConfig.custom()
73 .setSoTimeout(TIMEOUT)
74 .build())
75 .setLookupRegistry(new UriPatternMatcher<>())
76 .register("*", () -> new EchoHandler(2048))
77 .addFilterLast("test-filter", (request, entityDetails, context, responseTrigger, chain) ->
78 chain.proceed(request, entityDetails, context, new AsyncFilterChain.ResponseTrigger() {
79
80 @Override
81 public void sendInformation(
82 final HttpResponse response) throws HttpException, IOException {
83 responseTrigger.sendInformation(response);
84 }
85
86 @Override
87 public void submitResponse(
88 final HttpResponse response,
89 final AsyncEntityProducer entityProducer) throws HttpException, IOException {
90 response.setHeader("X-Test-Filter", "active");
91 responseTrigger.submitResponse(response, entityProducer);
92 }
93
94 @Override
95 public void pushPromise(
96 final HttpRequest promise,
97 final AsyncPushProducer responseProducer) throws HttpException, IOException {
98 responseTrigger.pushPromise(promise, responseProducer);
99 }
100
101 })));
102
103 @RegisterExtension
104 private final HttpAsyncRequesterResource clientResource = new HttpAsyncRequesterResource((bootstrap) -> bootstrap
105 .setIOReactorConfig(IOReactorConfig.custom()
106 .setSoTimeout(TIMEOUT)
107 .build()));
108
109
110 @Test
111 public void testFilters() throws Exception {
112 final HttpAsyncServer server = serverResource.start();
113 final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTP);
114 final ListenerEndpoint listener = future.get();
115 final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
116 final HttpAsyncRequester requester = clientResource.start();
117
118 final HttpHost target = new HttpHost("http", "localhost", address.getPort());
119 final Future<Message<HttpResponse, String>> resultFuture = requester.execute(
120 new BasicRequestProducer(Method.POST, target, "/filters",
121 new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)),
122 new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
123 final Message<HttpResponse, String> message = resultFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
124 assertThat(message, CoreMatchers.notNullValue());
125 final HttpResponse response = message.getHead();
126 assertThat(response.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
127 final Header testFilterHeader = response.getHeader("X-Test-Filter");
128 assertThat(testFilterHeader, CoreMatchers.notNullValue());
129 }
130
131 }