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.jupiter.api.Assertions;
36  import org.junit.jupiter.api.BeforeEach;
37  import org.junit.jupiter.api.Test;
38  
39  /**
40   * Tests for {@link BasicLineFormatter}.
41   */
42  public class TestBasicLineFormatter {
43  
44      private BasicLineFormatter formatter;
45  
46      @BeforeEach
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          Assertions.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          Assertions.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          Assertions.assertThrows(NullPointerException.class, () ->
71                  formatter.formatRequestLine(null, requestline));
72          Assertions.assertThrows(NullPointerException.class, () ->
73                  formatter.formatRequestLine(buf, null));
74      }
75  
76      @Test
77      public void testSLFormatting() throws Exception {
78          final CharArrayBuffer buf = new CharArrayBuffer(64);
79          final StatusLine statusline1 = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
80          this.formatter.formatStatusLine(buf, statusline1);
81          Assertions.assertEquals("HTTP/1.1 200 OK", buf.toString());
82  
83          buf.clear();
84          final StatusLine statusline2 = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null);
85          this.formatter.formatStatusLine(buf, statusline2);
86          Assertions.assertEquals("HTTP/1.1 200 ", buf.toString());
87          // see "testSLParseSuccess" in TestBasicLineParser:
88          // trailing space is correct
89      }
90  
91      @Test
92      public void testSLFormattingInvalidInput() throws Exception {
93          final CharArrayBuffer buf = new CharArrayBuffer(64);
94          final StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
95          Assertions.assertThrows(NullPointerException.class, () ->
96                  formatter.formatStatusLine(null, statusline));
97          Assertions.assertThrows(NullPointerException.class, () ->
98                  formatter.formatStatusLine(buf, null));
99      }
100 
101     @Test
102     public void testHeaderFormatting() throws Exception {
103         final CharArrayBuffer buf = new CharArrayBuffer(64);
104         final Header header1 = new BasicHeader("name", "value");
105         this.formatter.formatHeader(buf, header1);
106         Assertions.assertEquals("name: value", buf.toString());
107 
108         buf.clear();
109         final Header header2 = new BasicHeader("name", null);
110         this.formatter.formatHeader(buf, header2);
111         Assertions.assertEquals("name: ", buf.toString());
112     }
113 
114     @Test
115     public void testHeaderFormattingInvalidInput() throws Exception {
116         final CharArrayBuffer buf = new CharArrayBuffer(64);
117         final Header header = new BasicHeader("name", "value");
118         Assertions.assertThrows(NullPointerException.class, () ->
119                 formatter.formatHeader(null, header));
120         Assertions.assertThrows(NullPointerException.class, () ->
121                 formatter.formatHeader(buf, null));
122     }
123 
124     @Test
125     public void testHeaderFormattingRequestSplitting() throws Exception {
126         final CharArrayBuffer buf = new CharArrayBuffer(64);
127         final Header header = new BasicHeader("Host", "apache.org\r\nOops: oops");
128         formatter.formatHeader(buf, header);
129         final String s = buf.toString();
130         Assertions.assertFalse(s.contains("\n"));
131         Assertions.assertEquals("Host: apache.org  Oops: oops", s);
132     }
133 }