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