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.core5.http.protocol;
29  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertThrows;
32  import static org.mockito.Mockito.mock;
33  import static org.mockito.Mockito.when;
34  
35  import java.io.IOException;
36  import java.net.InetSocketAddress;
37  
38  import org.apache.hc.core5.http.ContentType;
39  import org.apache.hc.core5.http.EndpointDetails;
40  import org.apache.hc.core5.http.HttpException;
41  import org.apache.hc.core5.http.HttpRequest;
42  import org.apache.hc.core5.http.HttpRequestInterceptor;
43  import org.apache.hc.core5.http.HttpVersion;
44  import org.apache.hc.core5.http.ProtocolException;
45  import org.apache.hc.core5.http.ProtocolVersion;
46  import org.apache.hc.core5.http.impl.BasicEntityDetails;
47  import org.apache.hc.core5.http.message.BasicHeader;
48  import org.apache.hc.core5.http.message.BasicHttpRequest;
49  import org.apache.hc.core5.net.URIAuthority;
50  import org.junit.jupiter.api.Test;
51  
52  public class TesForwardedRequest {
53      private final HttpRequest request = new BasicHttpRequest("GET", "/");
54      private final HttpContext context = mock(HttpContext.class);
55  
56      private static final String FORWARDED_HEADER_NAME = "Forwarded";
57  
58      @Test
59      public void testProcess() throws IOException, HttpException {
60          final HttpRequestInterceptor processor = new ForwardedRequest();
61  
62          // Create a mock endpoint with a remote address
63          final InetSocketAddress remoteAddress = new InetSocketAddress("192.168.1.100", 12345);
64          final InetSocketAddress localAddress = new InetSocketAddress("127.0.0.1", 2263);
65  
66          final EndpointDetails endpointDetails = mock(EndpointDetails.class);
67          when(endpointDetails.getRemoteAddress()).thenReturn(remoteAddress);
68          when(endpointDetails.getLocalAddress()).thenReturn(localAddress);
69  
70          // Create a mock HTTP request with a host and port
71          final String host = "somehost";
72          final int port = 8888;
73  
74          request.setAuthority(new URIAuthority(host, port));
75  
76          // Create a mock HTTP context with the endpoint and protocol version
77          final ProtocolVersion version = HttpVersion.HTTP_1_1;
78          final HttpContext context = new BasicHttpContext();
79          context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, endpointDetails);
80  
81          // Process the HTTP request
82          processor.process(request, new BasicEntityDetails(1, ContentType.APPLICATION_JSON), context);
83  
84          // Check the value of the Forwarded header
85          final String expectedValue = "by=" + remoteAddress.getAddress().getHostAddress() + ":" + remoteAddress.getPort() + ";" +
86                  "for=" + localAddress.getAddress().getHostAddress() + ":" + localAddress.getPort() + ";" +
87                  "host=\"" + host + "\";port=" + port + ";proto=" + version.getProtocol();
88  
89          assertEquals(expectedValue, request.getFirstHeader(FORWARDED_HEADER_NAME).getValue());
90      }
91  
92  
93      @Test
94      public void testProcessWithIPv6() throws IOException, HttpException {
95          final HttpRequestInterceptor processor = new ForwardedRequest();
96  
97          // Create a mock endpoint with a remote IPv6 address
98          final InetSocketAddress remoteAddress = InetSocketAddress.createUnresolved("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", 12345);
99          final InetSocketAddress localAddress = InetSocketAddress.createUnresolved("[0:0:0:0:0:0:0:1]", 2263);
100 
101         final EndpointDetails endpointDetails = mock(EndpointDetails.class);
102         when(endpointDetails.getRemoteAddress()).thenReturn(remoteAddress);
103         when(endpointDetails.getLocalAddress()).thenReturn(localAddress);
104 
105         // Create a mock HTTP request with a host and port
106         final String host = "somehost";
107         final int port = 8888;
108 
109         request.setAuthority(new URIAuthority(host, port));
110 
111         // Create a mock HTTP context with the endpoint and protocol version
112         final ProtocolVersion version = HttpVersion.HTTP_1_1;
113         final HttpContext context = new BasicHttpContext();
114         context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, endpointDetails);
115 
116         // Process the HTTP request
117         processor.process(request, new BasicEntityDetails(1, ContentType.APPLICATION_JSON), context);
118 
119         // Check the value of the Forwarded header
120         final String expectedValue = "by=" + remoteAddress.getHostName() + ":" + remoteAddress.getPort() +
121                 ";for=" + localAddress.getHostName()  + ":" + localAddress.getPort() +
122                 ";host=\"" + host + "\";port=" + port + ";proto=" + version.getProtocol();
123 
124 
125         assertEquals(expectedValue, request.getFirstHeader(FORWARDED_HEADER_NAME).getValue());
126     }
127 
128     @Test
129     void testProcess_withNullEndpointDetails_shouldAddValidHeader() throws Exception {
130         final HttpRequestInterceptor processor = new ForwardedRequest();
131 
132         // Create a mock HTTP request with a host and port
133         final String host = "somehost";
134         final int port = 8888;
135 
136         request.setAuthority(new URIAuthority(host, port));
137 
138         // Create a mock HTTP context with the endpoint and protocol version
139         final ProtocolVersion version = HttpVersion.HTTP_1_1;
140         final HttpContext context = new BasicHttpContext();
141         context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, null);
142 
143         // Process the HTTP request
144         processor.process(request, new BasicEntityDetails(1, ContentType.APPLICATION_JSON), context);
145 
146         // Check the value of the Forwarded header
147         final String expectedValue = "host=\"" + host + "\";port=" + port + ";proto=" + version.getProtocol();
148 
149         assertEquals(expectedValue, request.getFirstHeader(FORWARDED_HEADER_NAME).getValue());
150     }
151 
152     @Test
153     public void testWithForwardedHeader() throws Exception {
154         final HttpRequestInterceptor processor = new ForwardedRequest();
155 
156         // Create a mock HTTP request with a host and port
157         final String host = "newhost";
158         final int port = 8888;
159 
160         request.setAuthority(new URIAuthority(host, port));
161 
162         // Create a mock HTTP context with the endpoint and protocol version
163         final ProtocolVersion version = HttpVersion.HTTP_1_1;
164         final HttpContext context = new BasicHttpContext();
165         context.setAttribute(HttpCoreContext.CONNECTION_ENDPOINT, null);
166         final String forwaredHeaderValue = "host=oldhost;port=8855;proto=HTTP";
167 
168         request.setHeader(new BasicHeader(FORWARDED_HEADER_NAME, forwaredHeaderValue));
169 
170          // Process the HTTP request
171         processor.process(request, new BasicEntityDetails(1, ContentType.APPLICATION_JSON), context);
172 
173         // Check the value of the Forwarded header
174         final String expectedValue = forwaredHeaderValue + ", " + "host=\"" + host + "\";port=" + port + ";proto=" + version.getProtocol();
175 
176         assertEquals(expectedValue, request.getFirstHeader(FORWARDED_HEADER_NAME).getValue());
177     }
178 
179     @Test
180     public void testProcessWithNullHttpRequest() {
181         final ForwardedRequest httpRequestModifier = new ForwardedRequest();
182         assertThrows(NullPointerException.class, () -> httpRequestModifier.process(null, null, context));
183     }
184 
185     @Test
186     public void testProcessWithNullHttpContext() {
187         final ForwardedRequest httpRequestModifier = new ForwardedRequest();
188         assertThrows(NullPointerException.class, () -> httpRequestModifier.process(request, null, null));
189     }
190 
191     @Test
192     public void testProcessWithNullAuthority() {
193         final ForwardedRequest httpRequestModifier = new ForwardedRequest();
194         assertThrows(ProtocolException.class, () -> httpRequestModifier.process(request, null, context));
195     }
196 }