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.testing.sync;
28  
29  import org.apache.hc.client5.http.classic.methods.HttpPost;
30  import org.apache.hc.client5.testing.extension.sync.ClientProtocolLevel;
31  import org.apache.hc.client5.testing.extension.sync.TestClient;
32  import org.apache.hc.core5.http.ClassicHttpRequest;
33  import org.apache.hc.core5.http.Header;
34  import org.apache.hc.core5.http.HttpHost;
35  import org.apache.hc.core5.http.HttpResponse;
36  import org.apache.hc.core5.http.URIScheme;
37  import org.apache.hc.core5.http.io.entity.EntityUtils;
38  import org.junit.jupiter.api.Assertions;
39  import org.junit.jupiter.api.BeforeEach;
40  import org.junit.jupiter.api.Test;
41  
42  @SuppressWarnings("boxing") // test code
43  public class TestHttpClientBuilderInterceptors extends AbstractIntegrationTestBase {
44  
45      public TestHttpClientBuilderInterceptors() {
46          super(URIScheme.HTTP, ClientProtocolLevel.STANDARD);
47      }
48  
49      @BeforeEach
50      public void before() throws Exception {
51          configureServer(bootstrap -> bootstrap
52                  .register("/test", (request, response, context) -> {
53                      final Header testInterceptorHeader = request.getHeader("X-Test-Interceptor");
54                      if (testInterceptorHeader != null) {
55                          response.setHeader(testInterceptorHeader);
56                      }
57                      response.setCode(200);
58                  }));
59          configureClient(builder -> builder
60                  .addExecInterceptorLast("test-interceptor", (request, scope, chain) -> {
61                      request.setHeader("X-Test-Interceptor", "active");
62                      return chain.proceed(request, scope);
63                  }));
64      }
65  
66      @Test
67      public void testAddExecInterceptorLastShouldBeExecuted() throws Exception {
68          final HttpHost httpHost = startServer();
69          final TestClient client = client();
70          final ClassicHttpRequest request = new HttpPost("/test");
71          final HttpResponse response = client.execute(httpHost, request, httpResponse -> {
72              EntityUtils.consume(httpResponse.getEntity());
73              return httpResponse;
74          });
75          Assertions.assertEquals(200, response.getCode());
76          final Header testFilterHeader = response.getHeader("X-Test-Interceptor");
77          Assertions.assertNotNull(testFilterHeader);
78      }
79  
80  }