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  package org.apache.hc.client5.http.impl.classic;
28  
29  import java.io.IOException;
30  
31  import org.apache.hc.client5.http.classic.ExecChain;
32  import org.apache.hc.client5.http.classic.ExecChainHandler;
33  import org.apache.hc.client5.http.classic.ExecChain.Scope;
34  import org.apache.hc.client5.http.classic.methods.HttpPost;
35  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
36  import org.apache.hc.client5.http.io.HttpClientConnectionManager;
37  import org.apache.hc.core5.http.ClassicHttpRequest;
38  import org.apache.hc.core5.http.ClassicHttpResponse;
39  import org.apache.hc.core5.http.Header;
40  import org.apache.hc.core5.http.HttpException;
41  import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
42  import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
43  import org.apache.hc.core5.http.io.HttpRequestHandler;
44  import org.apache.hc.core5.http.protocol.HttpContext;
45  import org.apache.hc.core5.io.CloseMode;
46  import org.junit.After;
47  import org.junit.Assert;
48  import org.junit.Before;
49  import org.junit.Test;
50  
51  @SuppressWarnings("boxing") // test code
52  public class TestHttpClientBuilderInterceptors {
53  
54      private HttpServer localServer;
55      private String uri;
56      private CloseableHttpClient httpClient;
57  
58      @Before
59      public void before() throws Exception {
60          this.localServer = ServerBootstrap.bootstrap()
61                  .register("/test", new HttpRequestHandler() {
62  
63              @Override
64              public void handle(
65                      final ClassicHttpRequest request,
66                      final ClassicHttpResponse response,
67                      final HttpContext context) throws HttpException, IOException {
68                  final Header testInterceptorHeader = request.getHeader("X-Test-Interceptor");
69                  if (testInterceptorHeader != null) {
70                      response.setHeader(testInterceptorHeader);
71                  }
72                  response.setCode(200);
73              }
74          }).create();
75  
76          this.localServer.start();
77          uri = "http://localhost:" + this.localServer.getLocalPort() + "/test";
78          final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
79                  .setMaxConnPerRoute(5)
80                  .build();
81          httpClient = HttpClientBuilder.create()
82                  .setConnectionManager(cm)
83                  .addExecInterceptorLast("test-interceptor", new ExecChainHandler() {
84  
85                      @Override
86                      public ClassicHttpResponse execute(
87                              final ClassicHttpRequest request,
88                              final Scope scope,
89                              final ExecChain chain) throws IOException, HttpException {
90                          request.setHeader("X-Test-Interceptor", "active");
91                          return chain.proceed(request, scope);
92                      }
93                  })
94                  .build();
95      }
96  
97      @After
98      public void after() throws Exception {
99          this.httpClient.close(CloseMode.IMMEDIATE);
100         this.localServer.stop();
101     }
102 
103     @Test
104     public void testAddExecInterceptorLastShouldBeExecuted() throws IOException, HttpException {
105         final ClassicHttpRequest request = new HttpPost(uri);
106         final ClassicHttpResponse response = httpClient.execute(request);
107         Assert.assertEquals(200, response.getCode());
108         final Header testFilterHeader = response.getHeader("X-Test-Interceptor");
109         Assert.assertNotNull(testFilterHeader);
110     }
111 
112 }