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.HttpContext;
44  import org.junit.jupiter.api.BeforeEach;
45  import org.junit.jupiter.api.Test;
46  
47  class TestRequestValidateTrace {
48  
49      private RequestValidateTrace interceptor;
50      private HttpRequest request;
51      private HttpContext context;
52  
53      @BeforeEach
54      void setUp() {
55          interceptor = new RequestValidateTrace();
56          context = HttpClientContext.create();
57      }
58  
59      @Test
60      void testTraceRequestWithoutSensitiveHeaders() throws HttpException, IOException {
61          request = new BasicHttpRequest("TRACE", "/");
62          interceptor.process(request, null, context);
63          assertNull(request.getHeader(HttpHeaders.AUTHORIZATION));
64      }
65  
66      @Test
67      void testTraceRequestWithSensitiveHeaders() {
68          request = new BasicHttpRequest("TRACE", "/");
69          request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer token");
70          assertThrows(ProtocolException.class, () -> interceptor.process(request, null, context));
71      }
72  
73      @Test
74      void testTraceRequestWithBody() {
75          request = new BasicHttpRequest("TRACE", "/");
76          final EntityDetails entity = new BasicEntityDetails(10, null);
77          assertThrows(ProtocolException.class, () -> interceptor.process(request, entity, context));
78      }
79  
80      @Test
81      void testNonTraceRequest() throws HttpException, IOException {
82          request = new BasicHttpRequest("GET", "/");
83          request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer token");
84          interceptor.process(request, null, context);
85          assertNotNull(request.getHeader(HttpHeaders.AUTHORIZATION));
86      }
87  
88      @Test
89      void testTraceRequestWithCookieHeader() {
90          request = new BasicHttpRequest("TRACE", "/");
91          request.setHeader(HttpHeaders.COOKIE, "someCookie=someValue");
92          assertThrows(ProtocolException.class, () -> interceptor.process(request, null, context));
93      }
94  }