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 java.io.IOException;
31  
32  import org.apache.hc.core5.http.ClassicHttpRequest;
33  import org.apache.hc.core5.http.ClassicHttpResponse;
34  import org.apache.hc.core5.http.ContentType;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.HttpException;
37  import org.apache.hc.core5.http.HttpHost;
38  import org.apache.hc.core5.http.HttpStatus;
39  import org.apache.hc.core5.http.Method;
40  import org.apache.hc.core5.http.impl.bootstrap.HttpRequester;
41  import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
42  import org.apache.hc.core5.http.impl.bootstrap.RequesterBootstrap;
43  import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
44  import org.apache.hc.core5.http.io.HttpFilterChain;
45  import org.apache.hc.core5.http.io.HttpFilterHandler;
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.HttpContext;
50  import org.apache.hc.core5.http.protocol.HttpCoreContext;
51  import org.apache.hc.core5.io.CloseMode;
52  import org.apache.hc.core5.util.Timeout;
53  import org.hamcrest.CoreMatchers;
54  import org.hamcrest.MatcherAssert;
55  import org.junit.Rule;
56  import org.junit.Test;
57  import org.junit.rules.ExternalResource;
58  import org.slf4j.Logger;
59  import org.slf4j.LoggerFactory;
60  
61  public class ClassicServerBootstrapFilterTest {
62  
63      private static final Timeout TIMEOUT = Timeout.ofSeconds(30);
64  
65      private final Logger log = LoggerFactory.getLogger(getClass());
66  
67      private HttpServer server;
68  
69      @Rule
70      public ExternalResource serverResource = new ExternalResource() {
71  
72          @Override
73          protected void before() throws Throwable {
74              log.debug("Starting up test server");
75              server = ServerBootstrap.bootstrap()
76                      .setSocketConfig(SocketConfig.custom()
77                              .setSoTimeout(TIMEOUT)
78                              .build())
79                      .register("*", new EchoHandler())
80                      .addFilterLast("test-filter", new HttpFilterHandler() {
81  
82                          @Override
83                          public void handle(
84                                  final ClassicHttpRequest request,
85                                  final HttpFilterChain.ResponseTrigger responseTrigger,
86                                  final HttpContext context,
87                                  final HttpFilterChain chain) throws HttpException, IOException {
88                              chain.proceed(request, new HttpFilterChain.ResponseTrigger() {
89  
90                                  @Override
91                                  public void sendInformation(
92                                          final ClassicHttpResponse response) throws HttpException, IOException {
93                                      responseTrigger.sendInformation(response);
94                                  }
95  
96                                  @Override
97                                  public void submitResponse(
98                                          final ClassicHttpResponse response) throws HttpException, IOException {
99                                      response.setHeader("X-Test-Filter", "active");
100                                     responseTrigger.submitResponse(response);
101                                 }
102 
103                             }, context);
104                         }
105 
106                     })
107                     .setExceptionListener(LoggingExceptionListener.INSTANCE)
108                     .setStreamListener(LoggingHttp1StreamListener.INSTANCE)
109                     .create();
110         }
111 
112         @Override
113         protected void after() {
114             log.debug("Shutting down test server");
115             if (server != null) {
116                 try {
117                     server.close(CloseMode.IMMEDIATE);
118                 } catch (final Exception ignore) {
119                 }
120             }
121         }
122 
123     };
124 
125     private HttpRequester requester;
126 
127     @Rule
128     public ExternalResource clientResource = new ExternalResource() {
129 
130         @Override
131         protected void before() throws Throwable {
132             log.debug("Starting up test client");
133             requester = RequesterBootstrap.bootstrap()
134                     .setSocketConfig(SocketConfig.custom()
135                             .setSoTimeout(TIMEOUT)
136                             .build())
137                     .setMaxTotal(2)
138                     .setDefaultMaxPerRoute(2)
139                     .setStreamListener(LoggingHttp1StreamListener.INSTANCE)
140                     .setConnPoolListener(LoggingConnPoolListener.INSTANCE)
141                     .create();
142         }
143 
144         @Override
145         protected void after() {
146             log.debug("Shutting down test client");
147             if (requester != null) {
148                 try {
149                     requester.close(CloseMode.GRACEFUL);
150                 } catch (final Exception ignore) {
151                 }
152             }
153         }
154 
155     };
156 
157     @Test
158     public void testFilters() throws Exception {
159         server.start();
160         final HttpHost target = new HttpHost("http", "localhost", server.getLocalPort());
161         final HttpCoreContext context = HttpCoreContext.create();
162         final ClassicHttpRequest request = new BasicClassicHttpRequest(Method.POST, "/filters");
163         request.setEntity(new StringEntity("some stuff", ContentType.TEXT_PLAIN));
164         try (final ClassicHttpResponse response = requester.execute(target, request, TIMEOUT, context)) {
165             MatcherAssert.assertThat(response.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
166             final Header testFilterHeader = response.getHeader("X-Test-Filter");
167             MatcherAssert.assertThat(testFilterHeader, CoreMatchers.notNullValue());
168         }
169     }
170 
171 }