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  package org.apache.http.impl.io;
28  
29  import java.io.InterruptedIOException;
30  
31  import org.apache.http.ConnectionClosedException;
32  import org.apache.http.Consts;
33  import org.apache.http.Header;
34  import org.apache.http.HttpRequest;
35  import org.apache.http.HttpVersion;
36  import org.apache.http.RequestLine;
37  import org.apache.http.impl.SessionInputBufferMock;
38  import org.apache.http.io.SessionInputBuffer;
39  import org.junit.Assert;
40  import org.junit.Test;
41  
42  /**
43   * Unit tests for {@link DefaultHttpRequestParser}.
44   */
45  public class TestRequestParser {
46  
47      @Test(expected=IllegalArgumentException.class)
48      public void testInvalidConstructorInput() throws Exception {
49          new DefaultHttpRequestParser(null);
50      }
51  
52      @Test
53      public void testBasicMessageParsing() throws Exception {
54          final String s =
55              "GET / HTTP/1.1\r\n" +
56              "Host: localhost\r\n" +
57              "User-Agent: whatever\r\n" +
58              "Cookie: c1=stuff\r\n" +
59              "\r\n";
60          final SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII);
61  
62          final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(inbuffer);
63          final HttpRequest httprequest = parser.parse();
64  
65          final RequestLine reqline = httprequest.getRequestLine();
66          Assert.assertNotNull(reqline);
67          Assert.assertEquals("GET", reqline.getMethod());
68          Assert.assertEquals("/", reqline.getUri());
69          Assert.assertEquals(HttpVersion.HTTP_1_1, reqline.getProtocolVersion());
70          final Header[] headers = httprequest.getAllHeaders();
71          Assert.assertEquals(3, headers.length);
72      }
73  
74      @Test
75      public void testConnectionClosedException() throws Exception {
76          final SessionInputBuffer inbuffer = new SessionInputBufferMock(new byte[] {});
77  
78          final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(inbuffer);
79          try {
80              parser.parse();
81              Assert.fail("ConnectionClosedException should have been thrown");
82          } catch (final ConnectionClosedException expected) {
83          }
84      }
85  
86      @Test
87      public void testMessageParsingTimeout() throws Exception {
88          final String s =
89              "GET \000/ HTTP/1.1\r\000\n" +
90              "Host: loca\000lhost\r\n" +
91              "User-Agent: whatever\r\n" +
92              "Coo\000kie: c1=stuff\r\n" +
93              "\000\r\n";
94          final SessionInputBuffer inbuffer = new SessionInputBufferMock(
95                  new TimeoutByteArrayInputStream(s.getBytes(Consts.ASCII)), 16);
96  
97          final DefaultHttpRequestParser parser = new DefaultHttpRequestParser(inbuffer);
98  
99          int timeoutCount = 0;
100 
101         HttpRequest httprequest = null;
102         for (int i = 0; i < 10; i++) {
103             try {
104                 httprequest = parser.parse();
105                 break;
106             } catch (final InterruptedIOException ex) {
107                 timeoutCount++;
108             }
109 
110         }
111         Assert.assertNotNull(httprequest);
112         Assert.assertEquals(5, timeoutCount);
113 
114         @SuppressWarnings("null") // httprequest cannot be null here
115         final
116         RequestLine reqline = httprequest.getRequestLine();
117         Assert.assertNotNull(reqline);
118         Assert.assertEquals("GET", reqline.getMethod());
119         Assert.assertEquals("/", reqline.getUri());
120         Assert.assertEquals(HttpVersion.HTTP_1_1, reqline.getProtocolVersion());
121         final Header[] headers = httprequest.getAllHeaders();
122         Assert.assertEquals(3, headers.length);
123     }
124 
125 }
126