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 java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.ObjectInputStream;
33  import java.io.ObjectOutputStream;
34  
35  import org.apache.http.HttpStatus;
36  import org.apache.http.HttpVersion;
37  import org.apache.http.StatusLine;
38  import org.junit.Assert;
39  import org.junit.Test;
40  
41  /**
42   * Simple tests for {@link StatusLine}.
43   */
44  public class TestStatusLine {
45  
46      @Test
47      public void testConstructor() {
48          final StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
49          Assert.assertEquals(HttpVersion.HTTP_1_1, statusline.getProtocolVersion());
50          Assert.assertEquals(HttpStatus.SC_OK, statusline.getStatusCode());
51          Assert.assertEquals("OK", statusline.getReasonPhrase());
52      }
53  
54      @Test
55      public void testConstructorInvalidInput() {
56          try {
57              new BasicStatusLine(null, HttpStatus.SC_OK, "OK");
58              Assert.fail("IllegalArgumentException should have been thrown");
59          } catch (final IllegalArgumentException e) { /* expected */ }
60          try {
61              new BasicStatusLine(HttpVersion.HTTP_1_1, -1, "OK");
62              Assert.fail("IllegalArgumentException should have been thrown");
63          } catch (final IllegalArgumentException e) { /* expected */ }
64      }
65  
66      @Test
67      public void testToString() throws Exception {
68          StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
69          Assert.assertEquals("HTTP/1.1 200 OK", statusline.toString());
70          statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
71          // toString uses default formatting, hence the trailing space
72          Assert.assertEquals("HTTP/1.1 200 ", statusline.toString());
73      }
74  
75      @Test
76      public void testCloning() throws Exception {
77          final BasicStatusLine orig = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
78          final BasicStatusLine clone = (BasicStatusLine) orig.clone();
79          Assert.assertEquals(orig.getReasonPhrase(), clone.getReasonPhrase());
80          Assert.assertEquals(orig.getStatusCode(), clone.getStatusCode());
81          Assert.assertEquals(orig.getProtocolVersion(), clone.getProtocolVersion());
82      }
83  
84      @Test
85      public void testSerialization() throws Exception {
86          final BasicStatusLine orig = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
87          final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
88          final ObjectOutputStream outstream = new ObjectOutputStream(outbuffer);
89          outstream.writeObject(orig);
90          outstream.close();
91          final byte[] raw = outbuffer.toByteArray();
92          final ByteArrayInputStream inbuffer = new ByteArrayInputStream(raw);
93          final ObjectInputStream instream = new ObjectInputStream(inbuffer);
94          final BasicStatusLine clone = (BasicStatusLine) instream.readObject();
95          Assert.assertEquals(orig.getReasonPhrase(), clone.getReasonPhrase());
96          Assert.assertEquals(orig.getStatusCode(), clone.getStatusCode());
97          Assert.assertEquals(orig.getProtocolVersion(), clone.getProtocolVersion());
98      }
99  
100 }