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.methods.HttpPost;
32  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
33  import org.apache.hc.client5.http.io.HttpClientConnectionManager;
34  import org.apache.hc.core5.http.ClassicHttpRequest;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.HttpException;
37  import org.apache.hc.core5.http.HttpResponse;
38  import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
39  import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
40  import org.apache.hc.core5.http.io.entity.EntityUtils;
41  import org.apache.hc.core5.io.CloseMode;
42  import org.junit.jupiter.api.AfterEach;
43  import org.junit.jupiter.api.Assertions;
44  import org.junit.jupiter.api.BeforeEach;
45  import org.junit.jupiter.api.Test;
46  
47  @SuppressWarnings("boxing") // test code
48  public class TestHttpClientBuilderInterceptors {
49  
50      private HttpServer localServer;
51      private String uri;
52      private CloseableHttpClient httpClient;
53  
54      @BeforeEach
55      public void before() throws Exception {
56          this.localServer = ServerBootstrap.bootstrap()
57                  .register("/test", (request, response, context) -> {
58                      final Header testInterceptorHeader = request.getHeader("X-Test-Interceptor");
59                      if (testInterceptorHeader != null) {
60                          response.setHeader(testInterceptorHeader);
61                      }
62                      response.setCode(200);
63                  }).create();
64  
65          this.localServer.start();
66          uri = "http://localhost:" + this.localServer.getLocalPort() + "/test";
67          final HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
68                  .setMaxConnPerRoute(5)
69                  .build();
70          httpClient = HttpClientBuilder.create()
71                  .setConnectionManager(cm)
72                  .addExecInterceptorLast("test-interceptor", (request, scope, chain) -> {
73                      request.setHeader("X-Test-Interceptor", "active");
74                      return chain.proceed(request, scope);
75                  })
76                  .build();
77      }
78  
79      @AfterEach
80      public void after() throws Exception {
81          this.httpClient.close(CloseMode.IMMEDIATE);
82          this.localServer.stop();
83      }
84  
85      @Test
86      public void testAddExecInterceptorLastShouldBeExecuted() throws IOException, HttpException {
87          final ClassicHttpRequest request = new HttpPost(uri);
88          final HttpResponse response = httpClient.execute(request, httpResponse -> {
89              EntityUtils.consume(httpResponse.getEntity());
90              return httpResponse;
91          });
92          Assertions.assertEquals(200, response.getCode());
93          final Header testFilterHeader = response.getHeader("X-Test-Interceptor");
94          Assertions.assertNotNull(testFilterHeader);
95      }
96  
97  }