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.Header;
31  import org.apache.http.HttpStatus;
32  import org.apache.http.HttpVersion;
33  import org.apache.http.RequestLine;
34  import org.apache.http.StatusLine;
35  import org.apache.http.util.CharArrayBuffer;
36  import org.junit.Assert;
37  import org.junit.Test;
38  
39  /**
40   * Tests for {@link BasicLineFormatter}.
41   */
42  public class TestBasicLineFormatter {
43  
44      @Test
45      public void testHttpVersionFormatting() throws Exception {
46          final String s = BasicLineFormatter.formatProtocolVersion
47              (HttpVersion.HTTP_1_1, null);
48          Assert.assertEquals("HTTP/1.1", s);
49      }
50  
51      @Test
52      public void testHttpVersionFormattingInvalidInput() throws Exception {
53          try {
54              BasicLineFormatter.formatProtocolVersion
55                  (null, BasicLineFormatter.INSTANCE);
56              Assert.fail("IllegalArgumentException should habe been thrown");
57          } catch (final IllegalArgumentException ex) {
58              // expected
59          }
60          try {
61              BasicLineFormatter.INSTANCE.appendProtocolVersion
62                  (new CharArrayBuffer(10), (HttpVersion) null);
63              Assert.fail("IllegalArgumentException should habe been thrown");
64          } catch (final IllegalArgumentException ex) {
65              // expected
66          }
67      }
68  
69  
70      @Test
71      public void testRLFormatting() throws Exception {
72          final RequestLine requestline = new BasicRequestLine("GET", "/stuff", HttpVersion.HTTP_1_1);
73          final String s = BasicLineFormatter.formatRequestLine(requestline, null);
74          Assert.assertEquals("GET /stuff HTTP/1.1", s);
75      }
76  
77      @Test
78      public void testRLFormattingInvalidInput() throws Exception {
79          try {
80              BasicLineFormatter.formatRequestLine
81                  (null, BasicLineFormatter.INSTANCE);
82              Assert.fail("IllegalArgumentException should habe been thrown");
83          } catch (final IllegalArgumentException ex) {
84              // expected
85          }
86          try {
87              BasicLineFormatter.INSTANCE.formatRequestLine
88                  (new CharArrayBuffer(10), (RequestLine) null);
89              Assert.fail("IllegalArgumentException should habe been thrown");
90          } catch (final IllegalArgumentException ex) {
91              // expected
92          }
93      }
94  
95  
96  
97      @Test
98      public void testSLFormatting() throws Exception {
99          StatusLine statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
100         String s = BasicLineFormatter.formatStatusLine(statusline, null);
101         Assert.assertEquals("HTTP/1.1 200 OK", s);
102         statusline = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
103         s = BasicLineFormatter.formatStatusLine(statusline, null);
104         Assert.assertEquals("HTTP/1.1 200 ", s);
105         // see "testSLParseSuccess" in TestBasicLineParser:
106         // trailing space is correct
107     }
108 
109     @Test
110     public void testSLFormattingInvalidInput() throws Exception {
111         try {
112             BasicLineFormatter.formatStatusLine
113                 (null, BasicLineFormatter.INSTANCE);
114             Assert.fail("IllegalArgumentException should habe been thrown");
115         } catch (final IllegalArgumentException ex) {
116             // expected
117         }
118         try {
119             BasicLineFormatter.INSTANCE.formatStatusLine
120                 (new CharArrayBuffer(10), (StatusLine) null);
121             Assert.fail("IllegalArgumentException should habe been thrown");
122         } catch (final IllegalArgumentException ex) {
123             // expected
124         }
125     }
126 
127 
128     @Test
129     public void testHeaderFormatting() throws Exception {
130         final Header header1 = new BasicHeader("name", "value");
131         String s = BasicLineFormatter.formatHeader(header1, null);
132         Assert.assertEquals("name: value", s);
133         final Header header2 = new BasicHeader("name", null);
134         s = BasicLineFormatter.formatHeader(header2, null);
135         Assert.assertEquals("name: ", s);
136     }
137 
138     @Test
139     public void testHeaderFormattingInvalidInput() throws Exception {
140         try {
141             BasicLineFormatter.formatHeader
142                 (null, BasicLineFormatter.INSTANCE);
143             Assert.fail("IllegalArgumentException should habe been thrown");
144         } catch (final IllegalArgumentException ex) {
145             // expected
146         }
147         try {
148             BasicLineFormatter.INSTANCE.formatHeader
149                 (new CharArrayBuffer(10), (Header) null);
150             Assert.fail("IllegalArgumentException should habe been thrown");
151         } catch (final IllegalArgumentException ex) {
152             // expected
153         }
154     }
155 
156     @Test
157     public void testHeaderFormattingRequestSplitting() throws Exception {
158         final Header header = new BasicHeader("Host", "apache.org\r\nOops: oops");
159         final String s = BasicLineFormatter.formatHeader(header, null);
160         Assert.assertFalse(s.contains("\n"));
161         Assert.assertEquals("Host: apache.org  Oops: oops", s);
162     }
163 }