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.message;
29  
30  import java.net.URI;
31  
32  import org.apache.hc.core5.http.HttpHost;
33  import org.apache.hc.core5.http.HttpRequest;
34  import org.apache.hc.core5.http.Method;
35  import org.apache.hc.core5.http.URIScheme;
36  import org.apache.hc.core5.net.URIAuthority;
37  import org.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.Test;
39  
40  public class HttpRequestWrapperTest {
41  
42      @Test
43      public void testRequestBasics() throws Exception {
44          final HttpRequest request = new BasicHttpRequest(Method.GET, "/stuff");
45          final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request);
46  
47          Assertions.assertEquals(Method.GET.name(), httpRequestWrapper.getMethod());
48          Assertions.assertEquals("/stuff", httpRequestWrapper.getPath());
49          Assertions.assertNull(httpRequestWrapper.getAuthority());
50          Assertions.assertEquals(new URI("/stuff"), httpRequestWrapper.getUri());
51          httpRequestWrapper.setPath("/another-stuff");
52          Assertions.assertEquals("/another-stuff", httpRequestWrapper.getPath());
53      }
54  
55      @Test
56      public void testDefaultRequestConstructors() {
57          final HttpRequest request1 = new BasicHttpRequest("WHATEVER", "/");
58          final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request1);
59          Assertions.assertEquals("WHATEVER", httpRequestWrapper.getMethod());
60          Assertions.assertEquals("/", httpRequestWrapper.getPath());
61  
62          final HttpRequest request2 = new BasicHttpRequest(Method.GET, "/");
63          final HttpRequestWrapper httpRequestWrapper2 = new HttpRequestWrapper(request2);
64          Assertions.assertEquals(Method.GET.name(), httpRequestWrapper2.getMethod());
65          Assertions.assertEquals("/", httpRequestWrapper2.getPath());
66  
67          Assertions.assertThrows(NullPointerException.class, () ->
68                  new BasicHttpRequest(Method.GET, (URI) null));
69      }
70  
71  
72      @Test
73      public void testRequestWithRelativeURI() throws Exception {
74          final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("/stuff"));
75          final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request);
76          Assertions.assertEquals(Method.GET.name(), httpRequestWrapper.getMethod());
77          Assertions.assertEquals("/stuff", httpRequestWrapper.getPath());
78          Assertions.assertNull(httpRequestWrapper.getAuthority());
79          Assertions.assertEquals(new URI("/stuff"), httpRequestWrapper.getUri());
80      }
81  
82      @Test
83      public void testRequestWithAbsoluteURI() throws Exception {
84          final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("https://host:9443/stuff?param=value"));
85          final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request);
86          Assertions.assertEquals(Method.GET.name(), httpRequestWrapper.getMethod());
87          Assertions.assertEquals("/stuff?param=value", httpRequestWrapper.getPath());
88          Assertions.assertEquals(new URIAuthority("host", 9443), httpRequestWrapper.getAuthority());
89          Assertions.assertEquals("https", httpRequestWrapper.getScheme());
90          Assertions.assertEquals(new URI("https://host:9443/stuff?param=value"), httpRequestWrapper.getUri());
91          httpRequestWrapper.setScheme((URIScheme.HTTP.id));
92          Assertions.assertEquals("http", httpRequestWrapper.getScheme());
93      }
94  
95      @Test
96      public void testRequestWithAbsoluteURIAsPath() throws Exception {
97          final HttpRequest request = new BasicHttpRequest(Method.GET, "https://host:9443/stuff?param=value");
98          final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request);
99          Assertions.assertEquals(Method.GET.name(), httpRequestWrapper.getMethod());
100         Assertions.assertEquals("/stuff?param=value", httpRequestWrapper.getPath());
101         Assertions.assertEquals(new URIAuthority("host", 9443), httpRequestWrapper.getAuthority());
102         Assertions.assertEquals("https", httpRequestWrapper.getScheme());
103         Assertions.assertEquals(new URI("https://host:9443/stuff?param=value"), httpRequestWrapper.getUri());
104     }
105 
106     @Test
107     public void testRequestWithNoPath() throws Exception {
108         final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("http://host"));
109         final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request);
110         Assertions.assertEquals(Method.GET.name(), httpRequestWrapper.getMethod());
111         Assertions.assertEquals("/", httpRequestWrapper.getPath());
112         Assertions.assertEquals(new URIAuthority("host"), httpRequestWrapper.getAuthority());
113         Assertions.assertEquals("http", httpRequestWrapper.getScheme());
114         Assertions.assertEquals(new URI("http://host/"), httpRequestWrapper.getUri());
115     }
116 
117     @Test
118     public void testRequestWithUserInfo() throws Exception {
119         final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("https://user:pwd@host:9443/stuff?param=value"));
120         final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request);
121         Assertions.assertEquals(Method.GET.name(), httpRequestWrapper.getMethod());
122         Assertions.assertEquals("/stuff?param=value", httpRequestWrapper.getPath());
123         Assertions.assertEquals(new URIAuthority("user:pwd", "host", 9443), httpRequestWrapper.getAuthority());
124         Assertions.assertEquals("https", httpRequestWrapper.getScheme());
125         Assertions.assertEquals(new URI("https://host:9443/stuff?param=value"), httpRequestWrapper.getUri());
126     }
127 
128     @Test
129     public void testRequestWithAuthority() throws Exception {
130         final HttpRequest request = new BasicHttpRequest(Method.GET, new HttpHost("http", "somehost", -1), "/stuff");
131         final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request);
132         Assertions.assertEquals(Method.GET.name(), httpRequestWrapper.getMethod());
133         Assertions.assertEquals("/stuff", httpRequestWrapper.getPath());
134         Assertions.assertEquals(new URIAuthority("somehost"), httpRequestWrapper.getAuthority());
135         Assertions.assertEquals(new URI("http://somehost/stuff"), httpRequestWrapper.getUri());
136 
137         httpRequestWrapper.setAuthority(new URIAuthority("newHost"));
138         Assertions.assertEquals(new URIAuthority("newHost"), httpRequestWrapper.getAuthority());
139 
140     }
141 
142     @Test
143     public void testRequestWithAuthorityRelativePath() throws Exception {
144         final HttpRequest request = new BasicHttpRequest(Method.GET, new HttpHost("http", "somehost", -1), "stuff");
145         final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request);
146         Assertions.assertEquals(Method.GET.name(), httpRequestWrapper.getMethod());
147         Assertions.assertEquals("stuff", httpRequestWrapper.getPath());
148         Assertions.assertEquals("stuff", httpRequestWrapper.getRequestUri());
149         Assertions.assertEquals(new URIAuthority("somehost"), httpRequestWrapper.getAuthority());
150         Assertions.assertEquals(new URI("http://somehost/stuff"), httpRequestWrapper.getUri());
151     }
152 
153     @Test
154     public void testRequestHostWithReservedChars() throws Exception {
155         final HttpRequest request = new BasicHttpRequest(Method.GET, URI.create("http://someuser%21@%21example%21.com/stuff"));
156         final HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper(request);
157         Assertions.assertEquals(Method.GET.name(), httpRequestWrapper.getMethod());
158         Assertions.assertEquals("/stuff", httpRequestWrapper.getPath());
159         Assertions.assertEquals(new URIAuthority("someuser%21", "%21example%21.com", -1), httpRequestWrapper.getAuthority());
160         Assertions.assertEquals(new URI("http://%21example%21.com/stuff"), httpRequestWrapper.getUri());
161     }
162 }