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.impl.io;
29  
30  import java.io.ByteArrayInputStream;
31  import java.io.IOException;
32  import java.nio.charset.StandardCharsets;
33  
34  import org.apache.hc.core5.http.Header;
35  import org.apache.hc.core5.http.ProtocolException;
36  import org.apache.hc.core5.http.io.SessionInputBuffer;
37  import org.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Unit tests for {@link AbstractMessageParser}.
42   */
43  public class TestMessageParser {
44  
45      @Test
46      public void testBasicHeaderParsing() throws Exception {
47          final String s =
48              "header1: stuff\r\n" +
49              "header2:  stuff \r\n" +
50              "header3: stuff\r\n" +
51              "     and more stuff\r\n" +
52              "\t and even more stuff\r\n" +
53              "     \r\n" +
54              "\r\n";
55          final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
56          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
57          final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
58          Assertions.assertNotNull(headers);
59          Assertions.assertEquals(3, headers.length);
60          Assertions.assertEquals("header1", headers[0].getName());
61          Assertions.assertEquals("stuff", headers[0].getValue());
62          Assertions.assertEquals("header2", headers[1].getName());
63          Assertions.assertEquals("stuff", headers[1].getValue());
64          Assertions.assertEquals("header3", headers[2].getName());
65          Assertions.assertEquals("stuff and more stuff and even more stuff", headers[2].getValue());
66      }
67  
68      @Test
69      public void testParsingHeader() throws Exception {
70          final String s = "header1: stuff; param1 = value1; param2 = \"value 2\" \r\n" +
71                  "\r\n";
72          final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
73          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
74          final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
75          Assertions.assertNotNull(headers);
76          Assertions.assertEquals(1, headers.length);
77          Assertions.assertEquals("header1: stuff; param1 = value1; param2 = \"value 2\" ", headers[0].toString());
78      }
79  
80      @Test
81      public void testParsingInvalidHeaders() throws Exception {
82          final String s1 = "    stuff\r\n" +
83              "header1: stuff\r\n" +
84              "\r\n";
85          final ByteArrayInputStream inputStream1 = new ByteArrayInputStream(s1.getBytes(StandardCharsets.US_ASCII));
86          final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
87          Assertions.assertThrows(ProtocolException.class, () ->
88                  AbstractMessageParser.parseHeaders(inBuffer1, inputStream1, -1, -1, null));
89          final String s2 = "  :  stuff\r\n" +
90              "header1: stuff\r\n" +
91              "\r\n";
92          final ByteArrayInputStream inputStream2 = new ByteArrayInputStream(s2.getBytes(StandardCharsets.US_ASCII));
93          final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
94          Assertions.assertThrows(ProtocolException.class, () ->
95                  AbstractMessageParser.parseHeaders(inBuffer2, inputStream2, -1, -1, null));
96      }
97  
98      @Test
99      public void testParsingMalformedFirstHeader() throws Exception {
100         final String s =
101             "    header1: stuff\r\n" +
102             "header2: stuff \r\n";
103         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
104         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
105         final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
106         Assertions.assertNotNull(headers);
107         Assertions.assertEquals(2, headers.length);
108         Assertions.assertEquals("header1", headers[0].getName());
109         Assertions.assertEquals("stuff", headers[0].getValue());
110         Assertions.assertEquals("header2", headers[1].getName());
111         Assertions.assertEquals("stuff", headers[1].getValue());
112     }
113 
114     @Test
115     public void testEmptyDataStream() throws Exception {
116         final String s = "";
117         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
118         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
119         final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
120         Assertions.assertNotNull(headers);
121         Assertions.assertEquals(0, headers.length);
122     }
123 
124     @Test
125     public void testMaxHeaderCount() throws Exception {
126         final String s =
127             "header1: stuff\r\n" +
128             "header2: stuff \r\n" +
129             "header3: stuff\r\n" +
130             "\r\n";
131         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
132         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
133         Assertions.assertThrows(IOException.class, () ->
134                 AbstractMessageParser.parseHeaders(inBuffer, inputStream, 2, -1, null));
135     }
136 
137     @Test
138     public void testMaxHeaderCountForFoldedHeader() throws Exception {
139         final String s =
140             "header1: stuff\r\n" +
141             " stuff \r\n" +
142             " stuff\r\n" +
143             "\r\n";
144         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
145         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
146         Assertions.assertThrows(IOException.class, () ->
147                 AbstractMessageParser.parseHeaders(inBuffer, inputStream, 2, 15, null));
148     }
149 
150 }
151