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