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.HttpVersion;
36  import org.apache.http.RequestLine;
37  import org.junit.Assert;
38  import org.junit.Test;
39  
40  /**
41   * Simple tests for {@link RequestLine}.
42   */
43  public class TestRequestLine {
44  
45      @Test
46      public void testConstructor() {
47          final RequestLine requestline = new BasicRequestLine("GET", "/stuff", HttpVersion.HTTP_1_1);
48          Assert.assertEquals("GET", requestline.getMethod());
49          Assert.assertEquals("/stuff", requestline.getUri());
50          Assert.assertEquals(HttpVersion.HTTP_1_1, requestline.getProtocolVersion());
51      }
52  
53      @Test
54      public void testConstructorInvalidInput() {
55          try {
56              new BasicRequestLine(null, "/stuff", HttpVersion.HTTP_1_1);
57              Assert.fail("IllegalArgumentException should have been thrown");
58          } catch (final IllegalArgumentException e) { /* expected */ }
59          try {
60              new BasicRequestLine("GET", null, HttpVersion.HTTP_1_1);
61              Assert.fail("IllegalArgumentException should have been thrown");
62          } catch (final IllegalArgumentException e) { /* expected */ }
63          try {
64              new BasicRequestLine("GET", "/stuff", (HttpVersion)null);
65              Assert.fail("IllegalArgumentException should have been thrown");
66          } catch (final IllegalArgumentException e) { /* expected */ }
67      }
68  
69      @Test
70      public void testCloning() throws Exception {
71          final BasicRequestLine orig = new BasicRequestLine("GET", "/stuff", HttpVersion.HTTP_1_1);
72          final BasicRequestLine clone = (BasicRequestLine) orig.clone();
73          Assert.assertEquals(orig.getMethod(), clone.getMethod());
74          Assert.assertEquals(orig.getUri(), clone.getUri());
75          Assert.assertEquals(orig.getProtocolVersion(), clone.getProtocolVersion());
76      }
77  
78      @Test
79      public void testSerialization() throws Exception {
80          final BasicRequestLine orig = new BasicRequestLine("GET", "/stuff", HttpVersion.HTTP_1_1);
81          final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
82          final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
83          outStream.writeObject(orig);
84          outStream.close();
85          final byte[] raw = outbuffer.toByteArray();
86          final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
87          final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
88          final BasicRequestLine clone = (BasicRequestLine) inStream.readObject();
89          Assert.assertEquals(orig.getMethod(), clone.getMethod());
90          Assert.assertEquals(orig.getUri(), clone.getUri());
91          Assert.assertEquals(orig.getProtocolVersion(), clone.getProtocolVersion());
92      }
93  
94  }