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