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