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 org.apache.hc.core5.http.HttpHost;
31  import org.apache.hc.core5.http.HttpRequest;
32  import org.apache.hc.core5.http.HttpResponse;
33  import org.apache.hc.core5.http.HttpStatus;
34  import org.apache.hc.core5.http.Method;
35  import org.apache.hc.core5.net.URIAuthority;
36  import org.junit.Assert;
37  import org.junit.Test;
38  
39  import java.net.URI;
40  
41  /**
42   * Unit tests for {@link org.apache.hc.core5.http.HttpMessage}.
43   *
44   */
45  public class TestBasicMessages {
46  
47      @Test
48      public void testDefaultResponseConstructors() {
49          HttpResponse response = new BasicHttpResponse(HttpStatus.SC_BAD_REQUEST, "Bad Request");
50          Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getCode());
51  
52          response = new BasicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "whatever");
53          Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getCode());
54          Assert.assertEquals("whatever", response.getReasonPhrase());
55      }
56  
57      @Test
58      public void testSetResponseStatus() {
59          HttpResponse response = new BasicHttpResponse(200, "OK");
60          Assert.assertNotNull(response.getCode());
61          Assert.assertEquals(200, response.getCode());
62  
63          response = new BasicHttpResponse(HttpStatus.SC_BAD_REQUEST, "Bad Request");
64          Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getCode());
65  
66          response = new BasicHttpResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "whatever");
67          Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getCode());
68          Assert.assertEquals("whatever", response.getReasonPhrase());
69  
70          response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
71          try {
72              response.setCode(-23);
73              Assert.fail("IllegalArgumentException should have been thrown");
74          } catch (final IllegalArgumentException ex) {
75              // expected
76          }
77      }
78  
79      @Test
80      public void testDefaultRequestConstructors() {
81          HttpRequest request = new BasicHttpRequest("WHATEVER", "/");
82          Assert.assertEquals("WHATEVER", request.getMethod());
83          Assert.assertEquals("/", request.getPath());
84  
85          request = new BasicHttpRequest(Method.GET, "/");
86          Assert.assertEquals(Method.GET.name(), request.getMethod());
87          Assert.assertEquals("/", request.getPath());
88  
89          try {
90              new BasicHttpRequest(Method.GET, (URI) null);
91              Assert.fail("NullPointerException should have been thrown");
92          } catch (final NullPointerException ex) {
93              // expected
94          }
95      }
96  
97      @Test
98      public void testResponseBasics() {
99          final BasicHttpResponse response = new BasicHttpResponse(200, "OK");
100         Assert.assertEquals(200, response.getCode());
101         Assert.assertEquals("OK", response.getReasonPhrase());
102     }
103 
104     @Test
105     public void testResponseStatusLineMutation() {
106         final BasicHttpResponse response = new BasicHttpResponse(200, "OK");
107         Assert.assertEquals(200, response.getCode());
108         Assert.assertEquals("OK", response.getReasonPhrase());
109         response.setReasonPhrase("Kind of OK");
110         Assert.assertEquals(200, response.getCode());
111         Assert.assertEquals("Kind of OK", response.getReasonPhrase());
112         response.setCode(299);
113         Assert.assertEquals(299, response.getCode());
114         Assert.assertEquals(null, response.getReasonPhrase());
115     }
116 
117     @Test
118     public void testResponseInvalidStatusCode() {
119         try {
120             new BasicHttpResponse(-200, "OK");
121             Assert.fail("IllegalArgumentException expected");
122         } catch (final IllegalArgumentException expected) {
123         }
124         final BasicHttpResponse response = new BasicHttpResponse(200, "OK");
125         try {
126             response.setCode(-1);
127             Assert.fail("IllegalArgumentException expected");
128         } catch (final IllegalArgumentException expected) {
129         }
130     }
131 
132     @Test
133     public void testRequestBasics() throws Exception {
134         final HttpRequest request = new BasicHttpRequest(Method.GET, "/stuff");
135         Assert.assertEquals(Method.GET.name(), request.getMethod());
136         Assert.assertEquals("/stuff", request.getPath());
137         Assert.assertEquals(null, request.getAuthority());
138         Assert.assertEquals(new URI("/stuff"), request.getUri());
139     }
140 
141     @Test
142     public void testRequestWithRelativeURI() throws Exception {
143         final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("/stuff"));
144         Assert.assertEquals(Method.GET.name(), request.getMethod());
145         Assert.assertEquals("/stuff", request.getPath());
146         Assert.assertEquals(null, request.getAuthority());
147         Assert.assertEquals(new URI("/stuff"), request.getUri());
148     }
149 
150     @Test
151     public void testRequestWithAbsoluteURI() throws Exception {
152         final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("https://host:9443/stuff?param=value"));
153         Assert.assertEquals(Method.GET.name(), request.getMethod());
154         Assert.assertEquals("/stuff?param=value", request.getPath());
155         Assert.assertEquals(new URIAuthority("host", 9443), request.getAuthority());
156         Assert.assertEquals("https", request.getScheme());
157         Assert.assertEquals(new URI("https://host:9443/stuff?param=value"), request.getUri());
158     }
159 
160     @Test
161     public void testRequestWithAbsoluteURIAsPath() throws Exception {
162         final HttpRequest request = new BasicHttpRequest(Method.GET, "https://host:9443/stuff?param=value");
163         Assert.assertEquals(Method.GET.name(), request.getMethod());
164         Assert.assertEquals("/stuff?param=value", request.getPath());
165         Assert.assertEquals(new URIAuthority("host", 9443), request.getAuthority());
166         Assert.assertEquals("https", request.getScheme());
167         Assert.assertEquals(new URI("https://host:9443/stuff?param=value"), request.getUri());
168     }
169 
170     @Test
171     public void testRequestWithNoPath() throws Exception {
172         final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("http://host"));
173         Assert.assertEquals(Method.GET.name(), request.getMethod());
174         Assert.assertEquals("/", request.getPath());
175         Assert.assertEquals(new URIAuthority("host"), request.getAuthority());
176         Assert.assertEquals("http", request.getScheme());
177         Assert.assertEquals(new URI("http://host/"), request.getUri());
178     }
179 
180     @Test
181     public void testRequestWithUserInfo() throws Exception {
182         final HttpRequest request = new BasicHttpRequest(Method.GET, new URI("https://user:pwd@host:9443/stuff?param=value"));
183         Assert.assertEquals(Method.GET.name(), request.getMethod());
184         Assert.assertEquals("/stuff?param=value", request.getPath());
185         Assert.assertEquals(new URIAuthority("user:pwd", "host", 9443), request.getAuthority());
186         Assert.assertEquals("https", request.getScheme());
187         Assert.assertEquals(new URI("https://host:9443/stuff?param=value"), request.getUri());
188     }
189 
190     @Test
191     public void testRequestWithAuthority() throws Exception {
192         final HttpRequest request = new BasicHttpRequest(Method.GET, new HttpHost("http", "somehost", -1), "/stuff");
193         Assert.assertEquals(Method.GET.name(), request.getMethod());
194         Assert.assertEquals("/stuff", request.getPath());
195         Assert.assertEquals(new URIAuthority("somehost"), request.getAuthority());
196         Assert.assertEquals(new URI("http://somehost/stuff"), request.getUri());
197     }
198 
199     @Test
200     public void testRequestWithAuthorityRelativePath() throws Exception {
201         final HttpRequest request = new BasicHttpRequest(Method.GET, new HttpHost("http", "somehost", -1), "stuff");
202         Assert.assertEquals(Method.GET.name(), request.getMethod());
203         Assert.assertEquals("stuff", request.getPath());
204         Assert.assertEquals(new URIAuthority("somehost"), request.getAuthority());
205         Assert.assertEquals(new URI("http://somehost/stuff"), request.getUri());
206     }
207 
208     @Test
209     public void testRequestHostWithReservedChars() throws Exception {
210         final HttpRequest request = new BasicHttpRequest(Method.GET, URI.create("http://someuser%21@%21example%21.com/stuff"));
211         Assert.assertEquals(Method.GET.name(), request.getMethod());
212         Assert.assertEquals("/stuff", request.getPath());
213         Assert.assertEquals(new URIAuthority("someuser%21", "%21example%21.com", -1), request.getAuthority());
214         Assert.assertEquals(new URI("http://%21example%21.com/stuff"), request.getUri());
215     }
216 
217     @Test(expected = IllegalArgumentException.class)
218     public void testRequestPathWithMultipleLeadingSlashes() throws Exception {
219         new BasicHttpRequest(Method.GET, URI.create("http://host//stuff"));
220     }
221 
222     @Test
223     public void testRequestAbsoluteRequestUri() throws Exception {
224         final BasicHttpRequest request = new BasicHttpRequest(Method.GET, new HttpHost("http", "somehost", -1), "stuff");
225         Assert.assertEquals("stuff", request.getRequestUri());
226         request.setAbsoluteRequestUri(true);
227         Assert.assertEquals("http://somehost/stuff", request.getRequestUri());
228     }
229 
230 }
231