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.Assert;
38  import org.junit.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          Assert.assertNotNull(headers);
59          Assert.assertEquals(3, headers.length);
60          Assert.assertEquals("header1", headers[0].getName());
61          Assert.assertEquals("stuff", headers[0].getValue());
62          Assert.assertEquals("header2", headers[1].getName());
63          Assert.assertEquals("stuff", headers[1].getValue());
64          Assert.assertEquals("header3", headers[2].getName());
65          Assert.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          Assert.assertNotNull(headers);
76          Assert.assertEquals(1, headers.length);
77          Assert.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          try {
88              AbstractMessageParser.parseHeaders(inBuffer1, inputStream1, -1, -1, null);
89              Assert.fail("ProtocolException should have been thrown");
90          } catch (final ProtocolException ex) {
91              // expected
92          }
93          final String s2 = "  :  stuff\r\n" +
94              "header1: stuff\r\n" +
95              "\r\n";
96          final ByteArrayInputStream inputStream2 = new ByteArrayInputStream(s2.getBytes(StandardCharsets.US_ASCII));
97          final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
98          try {
99              AbstractMessageParser.parseHeaders(inBuffer2, inputStream2, -1, -1, null);
100             Assert.fail("ProtocolException should have been thrown");
101         } catch (final ProtocolException ex) {
102             // expected
103         }
104     }
105 
106     @Test
107     public void testParsingMalformedFirstHeader() throws Exception {
108         final String s =
109             "    header1: stuff\r\n" +
110             "header2: stuff \r\n";
111         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
112         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
113         final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
114         Assert.assertNotNull(headers);
115         Assert.assertEquals(2, headers.length);
116         Assert.assertEquals("header1", headers[0].getName());
117         Assert.assertEquals("stuff", headers[0].getValue());
118         Assert.assertEquals("header2", headers[1].getName());
119         Assert.assertEquals("stuff", headers[1].getValue());
120     }
121 
122     @Test
123     public void testEmptyDataStream() throws Exception {
124         final String s = "";
125         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
126         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
127         final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null);
128         Assert.assertNotNull(headers);
129         Assert.assertEquals(0, headers.length);
130     }
131 
132     @Test
133     public void testMaxHeaderCount() throws Exception {
134         final String s =
135             "header1: stuff\r\n" +
136             "header2: stuff \r\n" +
137             "header3: stuff\r\n" +
138             "\r\n";
139         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
140         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
141         try {
142             AbstractMessageParser.parseHeaders(inBuffer, inputStream, 2, -1, null);
143             Assert.fail("IOException should have been thrown");
144         } catch (final IOException ex) {
145             // expected
146         }
147     }
148 
149     @Test
150     public void testMaxHeaderCountForFoldedHeader() throws Exception {
151         final String s =
152             "header1: stuff\r\n" +
153             " stuff \r\n" +
154             " stuff\r\n" +
155             "\r\n";
156         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
157         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
158         try {
159             AbstractMessageParser.parseHeaders(inBuffer, inputStream, 2, 15, null);
160             Assert.fail("IOException should have been thrown");
161         } catch (final IOException ex) {
162             // expected
163         }
164     }
165 
166 }
167