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.http.message;
29  
30  import org.apache.http.HttpEntity;
31  import org.apache.http.HttpRequest;
32  import org.apache.http.HttpResponse;
33  import org.apache.http.HttpStatus;
34  import org.apache.http.HttpVersion;
35  import org.apache.http.entity.BasicHttpEntity;
36  import org.junit.Assert;
37  import org.junit.Test;
38  
39  /**
40   * Unit tests for {@link org.apache.http.HttpMessage}.
41   *
42   */
43  public class TestBasicMessages {
44  
45      @Test
46      public void testDefaultResponseConstructors() {
47          HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_0, HttpStatus.SC_BAD_REQUEST, "Bad Request");
48          Assert.assertNotNull(response.getProtocolVersion());
49          Assert.assertEquals(HttpVersion.HTTP_1_0, response.getProtocolVersion());
50          Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusLine().getStatusCode());
51  
52          response = new BasicHttpResponse(new BasicStatusLine(
53                  HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "whatever"));
54          Assert.assertNotNull(response.getProtocolVersion());
55          Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion());
56          Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getStatusLine().getStatusCode());
57          Assert.assertEquals("whatever", response.getStatusLine().getReasonPhrase());
58      }
59  
60      @Test
61      public void testSetResponseStatus() {
62          HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
63          Assert.assertNotNull(response.getProtocolVersion());
64          Assert.assertNotNull(response.getStatusLine());
65          Assert.assertEquals(200, response.getStatusLine().getStatusCode());
66  
67          response = new BasicHttpResponse(HttpVersion.HTTP_1_0, HttpStatus.SC_BAD_REQUEST, "Bad Request");
68          Assert.assertNotNull(response.getProtocolVersion());
69          Assert.assertEquals(HttpVersion.HTTP_1_0, response.getProtocolVersion());
70          Assert.assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusLine().getStatusCode());
71  
72          response = new BasicHttpResponse(new BasicStatusLine(
73                  HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "whatever"));
74          Assert.assertNotNull(response.getProtocolVersion());
75          Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion());
76          Assert.assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, response.getStatusLine().getStatusCode());
77          Assert.assertEquals("whatever", response.getStatusLine().getReasonPhrase());
78  
79          response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
80          try {
81              response.setStatusCode(-23);
82              Assert.fail("IllegalArgumentException should have been thrown");
83          } catch (final IllegalArgumentException ex) {
84              // expected
85          }
86          response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
87          try {
88              response.setStatusLine(null);
89              Assert.fail("IllegalArgumentException should have been thrown");
90          } catch (final IllegalArgumentException ex) {
91              // expected
92          }
93      }
94  
95      @Test
96      public void testSetResponseEntity() {
97          final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
98          Assert.assertNull(response.getEntity());
99  
100         final HttpEntity entity = new BasicHttpEntity();
101         response.setEntity(entity);
102         Assert.assertTrue(entity == response.getEntity());
103     }
104 
105     @Test
106     public void testDefaultRequestConstructors() {
107         HttpRequest request = new BasicHttpRequest("WHATEVER", "/");
108         Assert.assertNotNull(request.getProtocolVersion());
109         Assert.assertEquals("WHATEVER", request.getRequestLine().getMethod());
110         Assert.assertEquals("/", request.getRequestLine().getUri());
111 
112         request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_0);
113         Assert.assertEquals(HttpVersion.HTTP_1_0, request.getProtocolVersion());
114         Assert.assertEquals("GET", request.getRequestLine().getMethod());
115         Assert.assertEquals("/", request.getRequestLine().getUri());
116 
117         try {
118             new BasicHttpRequest(null, null);
119             Assert.fail("IllegalArgumentException should have been thrown");
120         } catch (final IllegalArgumentException ex) {
121             // expected
122         }
123         try {
124             new BasicHttpRequest("GET", null);
125             Assert.fail("IllegalArgumentException should have been thrown");
126         } catch (final IllegalArgumentException ex) {
127             // expected
128         }
129         try {
130             new BasicHttpRequest(null);
131             Assert.fail("IllegalArgumentException should have been thrown");
132         } catch (final IllegalArgumentException ex) {
133             // expected
134         }
135     }
136 
137     @Test
138     public void testDefaultEntityEnclosingRequestConstructors() {
139         BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("GET", "/");
140         Assert.assertNotNull(request.getProtocolVersion());
141         Assert.assertEquals("GET", request.getRequestLine().getMethod());
142         Assert.assertEquals("/", request.getRequestLine().getUri());
143 
144         request = new BasicHttpEntityEnclosingRequest("GET", "/", HttpVersion.HTTP_1_0);
145         Assert.assertEquals(HttpVersion.HTTP_1_0, request.getProtocolVersion());
146         Assert.assertEquals("GET", request.getRequestLine().getMethod());
147         Assert.assertEquals("/", request.getRequestLine().getUri());
148     }
149 
150     @Test
151     public void testSetRequestEntity() {
152         final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("GET", "/");
153         Assert.assertNull(request.getEntity());
154 
155         final HttpEntity entity = new BasicHttpEntity();
156         request.setEntity(entity);
157         Assert.assertTrue(entity == request.getEntity());
158     }
159 
160     @Test
161     public void testExpectContinue() {
162         final BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("GET", "/");
163         Assert.assertFalse(request.expectContinue());
164         request.addHeader("Expect", "100-Continue");
165         Assert.assertTrue(request.expectContinue());
166     }
167 
168 }
169