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.HttpVersion;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  /**
35   * Unit tests for {@link BasicHttpResponse}.
36   */
37  public class TestBasicHttpResponse {
38  
39      @Test
40      public void testBasics() {
41          final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
42          Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion());
43          Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
44          Assert.assertEquals(200, response.getStatusLine().getStatusCode());
45          Assert.assertEquals("OK", response.getStatusLine().getReasonPhrase());
46      }
47  
48      @Test
49      public void testStatusLineMutation() {
50          final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
51          Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
52          Assert.assertEquals(200, response.getStatusLine().getStatusCode());
53          Assert.assertEquals("OK", response.getStatusLine().getReasonPhrase());
54          response.setReasonPhrase("Kind of OK");
55          Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
56          Assert.assertEquals(200, response.getStatusLine().getStatusCode());
57          Assert.assertEquals("Kind of OK", response.getStatusLine().getReasonPhrase());
58          response.setStatusCode(299);
59          Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
60          Assert.assertEquals(299, response.getStatusLine().getStatusCode());
61          Assert.assertEquals(null, response.getStatusLine().getReasonPhrase());
62          response.setStatusLine(HttpVersion.HTTP_1_0, 298);
63          Assert.assertEquals(HttpVersion.HTTP_1_0, response.getStatusLine().getProtocolVersion());
64          Assert.assertEquals(298, response.getStatusLine().getStatusCode());
65          Assert.assertEquals(null, response.getStatusLine().getReasonPhrase());
66          response.setStatusLine(HttpVersion.HTTP_1_1, 200, "OK");
67          Assert.assertEquals(HttpVersion.HTTP_1_1, response.getStatusLine().getProtocolVersion());
68          Assert.assertEquals(200, response.getStatusLine().getStatusCode());
69          Assert.assertEquals("OK", response.getStatusLine().getReasonPhrase());
70          response.setStatusLine(new BasicStatusLine(HttpVersion.HTTP_1_0, 500, "Boom"));
71          Assert.assertEquals(HttpVersion.HTTP_1_0, response.getStatusLine().getProtocolVersion());
72          Assert.assertEquals(500, response.getStatusLine().getStatusCode());
73          Assert.assertEquals("Boom", response.getStatusLine().getReasonPhrase());
74      }
75  
76      @Test
77      public void testInvalidStatusCode() {
78          try {
79              new BasicHttpResponse(HttpVersion.HTTP_1_1, -200, "OK");
80              Assert.fail("IllegalArgumentException expected");
81          } catch (final IllegalArgumentException expected) {
82          }
83          final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
84          try {
85              response.setStatusCode(-1);
86              Assert.fail("IllegalArgumentException expected");
87          } catch (final IllegalArgumentException expected) {
88          }
89          try {
90              response.setStatusLine(HttpVersion.HTTP_1_1, -1);
91              Assert.fail("IllegalArgumentException expected");
92          } catch (final IllegalArgumentException expected) {
93          }
94          try {
95              response.setStatusLine(HttpVersion.HTTP_1_1, -1, "not ok");
96              Assert.fail("IllegalArgumentException expected");
97          } catch (final IllegalArgumentException expected) {
98          }
99      }
100 
101 }