1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }