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.client5.http.protocol;
29  
30  import static org.junit.jupiter.api.Assertions.assertNotNull;
31  import static org.junit.jupiter.api.Assertions.assertNull;
32  import static org.junit.jupiter.api.Assertions.assertThrows;
33  
34  import java.io.IOException;
35  
36  import org.apache.hc.core5.http.EntityDetails;
37  import org.apache.hc.core5.http.HttpException;
38  import org.apache.hc.core5.http.HttpHeaders;
39  import org.apache.hc.core5.http.HttpRequest;
40  import org.apache.hc.core5.http.ProtocolException;
41  import org.apache.hc.core5.http.impl.BasicEntityDetails;
42  import org.apache.hc.core5.http.message.BasicHttpRequest;
43  import org.apache.hc.core5.http.protocol.BasicHttpContext;
44  import org.apache.hc.core5.http.protocol.HttpContext;
45  import org.junit.jupiter.api.BeforeEach;
46  import org.junit.jupiter.api.Test;
47  
48  class TestRequestTraceInterceptor {
49  
50      private RequestTraceInterceptor interceptor;
51      private HttpRequest request;
52      private HttpContext context;
53  
54      @BeforeEach
55      void setUp() {
56          interceptor = new RequestTraceInterceptor();
57          context = new BasicHttpContext();
58      }
59  
60      @Test
61      void testTraceRequestWithoutSensitiveHeaders() throws HttpException, IOException {
62          request = new BasicHttpRequest("TRACE", "/");
63          interceptor.process(request, null, context);
64          assertNull(request.getHeader(HttpHeaders.AUTHORIZATION));
65      }
66  
67      @Test
68      void testTraceRequestWithSensitiveHeaders() {
69          request = new BasicHttpRequest("TRACE", "/");
70          request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer token");
71          assertThrows(ProtocolException.class, () -> interceptor.process(request, null, context));
72      }
73  
74      @Test
75      void testTraceRequestWithBody() {
76          request = new BasicHttpRequest("TRACE", "/");
77          final EntityDetails entity = new BasicEntityDetails(10, null);
78          assertThrows(ProtocolException.class, () -> interceptor.process(request, entity, context));
79      }
80  
81      @Test
82      void testNonTraceRequest() throws HttpException, IOException {
83          request = new BasicHttpRequest("GET", "/");
84          request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer token");
85          interceptor.process(request, null, context);
86          assertNotNull(request.getHeader(HttpHeaders.AUTHORIZATION));
87      }
88  
89      @Test
90      void testTraceRequestWithCookieHeader() {
91          request = new BasicHttpRequest("TRACE", "/");
92          request.setHeader(HttpHeaders.COOKIE, "someCookie=someValue");
93          assertThrows(ProtocolException.class, () -> interceptor.process(request, null, context));
94      }
95  }