View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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 }