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