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.http.impl.io;
29  
30  import java.io.IOException;
31  
32  import org.apache.http.Consts;
33  import org.apache.http.Header;
34  import org.apache.http.HeaderElement;
35  import org.apache.http.NameValuePair;
36  import org.apache.http.ProtocolException;
37  import org.apache.http.impl.SessionInputBufferMock;
38  import org.apache.http.io.SessionInputBuffer;
39  import org.apache.http.message.BufferedHeader;
40  import org.junit.Assert;
41  import org.junit.Test;
42  
43  /**
44   * Unit tests for {@link AbstractMessageParser}.
45   */
46  public class TestMessageParser {
47  
48      @Test
49      public void testInvalidInput() throws Exception {
50          try {
51              // the first argument must not be null
52              AbstractMessageParser.parseHeaders(null, -1, -1, null);
53              Assert.fail("IllegalArgumentException should have been thrown");
54          } catch (final IllegalArgumentException ex) {
55              // expected
56          }
57          try {
58              new BufferedHeader(null);
59              Assert.fail("IllegalArgumentException should have been thrown");
60          } catch (final IllegalArgumentException ex) {
61              // expected
62          }
63      }
64  
65      @Test
66      public void testBasicHeaderParsing() throws Exception {
67          final String s =
68              "header1: stuff\r\n" +
69              "header2  : stuff \r\n" +
70              "header3: stuff\r\n" +
71              "     and more stuff\r\n" +
72              "\t and even more stuff\r\n" +
73              "     \r\n" +
74              "\r\n";
75          final SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
76          final Header[] headers = AbstractMessageParser.parseHeaders
77              (receiver, -1, -1, null);
78          Assert.assertNotNull(headers);
79          Assert.assertEquals(3, headers.length);
80          Assert.assertEquals("header1", headers[0].getName());
81          Assert.assertEquals("stuff", headers[0].getValue());
82          Assert.assertEquals("header2", headers[1].getName());
83          Assert.assertEquals("stuff", headers[1].getValue());
84          Assert.assertEquals("header3", headers[2].getName());
85          Assert.assertEquals("stuff and more stuff and even more stuff", headers[2].getValue());
86  
87          final Header h = headers[0];
88  
89          Assert.assertTrue(h instanceof BufferedHeader);
90          Assert.assertNotNull(((BufferedHeader)h).getBuffer());
91          Assert.assertEquals("header1: stuff", ((BufferedHeader)h).toString());
92          Assert.assertEquals(8, ((BufferedHeader)h).getValuePos());
93      }
94  
95      @Test
96      public void testBufferedHeader() throws Exception {
97          final String s =
98              "header1  : stuff; param1 = value1; param2 = \"value 2\" \r\n" +
99              "\r\n";
100         final SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
101         final Header[] headers = AbstractMessageParser.parseHeaders
102             (receiver, -1, -1, null);
103         Assert.assertNotNull(headers);
104         Assert.assertEquals(1, headers.length);
105         Assert.assertEquals("header1  : stuff; param1 = value1; param2 = \"value 2\" ", headers[0].toString());
106         final HeaderElement[] elements = headers[0].getElements();
107         Assert.assertNotNull(elements);
108         Assert.assertEquals(1, elements.length);
109         Assert.assertEquals("stuff", elements[0].getName());
110         Assert.assertEquals(null, elements[0].getValue());
111         final NameValuePair[] params = elements[0].getParameters();
112         Assert.assertNotNull(params);
113         Assert.assertEquals(2, params.length);
114         Assert.assertEquals("param1", params[0].getName());
115         Assert.assertEquals("value1", params[0].getValue());
116         Assert.assertEquals("param2", params[1].getName());
117         Assert.assertEquals("value 2", params[1].getValue());
118     }
119 
120     @Test
121     public void testParsingInvalidHeaders() throws Exception {
122         String s = "    stuff\r\n" +
123             "header1: stuff\r\n" +
124             "\r\n";
125         SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
126         try {
127             AbstractMessageParser.parseHeaders(receiver, -1, -1, null);
128             Assert.fail("ProtocolException should have been thrown");
129         } catch (final ProtocolException ex) {
130             // expected
131         }
132         s = "  :  stuff\r\n" +
133             "header1: stuff\r\n" +
134             "\r\n";
135         receiver = new SessionInputBufferMock(s, Consts.ASCII);
136         try {
137             AbstractMessageParser.parseHeaders(receiver, -1, -1, null);
138             Assert.fail("ProtocolException should have been thrown");
139         } catch (final ProtocolException ex) {
140             // expected
141         }
142     }
143 
144     @Test
145     public void testParsingMalformedFirstHeader() throws Exception {
146         final String s =
147             "    header1: stuff\r\n" +
148             "header2  : stuff \r\n";
149         final SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
150         final Header[] headers = AbstractMessageParser.parseHeaders
151             (receiver, -1, -1, null);
152         Assert.assertNotNull(headers);
153         Assert.assertEquals(2, headers.length);
154         Assert.assertEquals("header1", headers[0].getName());
155         Assert.assertEquals("stuff", headers[0].getValue());
156         Assert.assertEquals("header2", headers[1].getName());
157         Assert.assertEquals("stuff", headers[1].getValue());
158     }
159 
160     @Test
161     public void testEmptyDataStream() throws Exception {
162         final String s = "";
163         final SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
164         final Header[] headers = AbstractMessageParser.parseHeaders
165             (receiver, -1, -1, null);
166         Assert.assertNotNull(headers);
167         Assert.assertEquals(0, headers.length);
168     }
169 
170     @Test
171     public void testMaxHeaderCount() throws Exception {
172         final String s =
173             "header1: stuff\r\n" +
174             "header2: stuff \r\n" +
175             "header3: stuff\r\n" +
176             "\r\n";
177         final SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
178         try {
179             AbstractMessageParser.parseHeaders(receiver, 2, -1, null);
180             Assert.fail("IOException should have been thrown");
181         } catch (final IOException ex) {
182             // expected
183         }
184     }
185 
186     @Test
187     public void testMaxHeaderCountForFoldedHeader() throws Exception {
188         final String s =
189             "header1: stuff\r\n" +
190             " stuff \r\n" +
191             " stuff\r\n" +
192             "\r\n";
193         final SessionInputBuffer receiver = new SessionInputBufferMock(s, Consts.ASCII);
194         try {
195             AbstractMessageParser.parseHeaders(receiver, 2, 15, null);
196             Assert.fail("IOException should have been thrown");
197         } catch (final IOException ex) {
198             // expected
199         }
200     }
201 
202 }
203