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.conn;
29  
30  import org.apache.http.Consts;
31  import org.apache.http.Header;
32  import org.apache.http.HttpResponse;
33  import org.apache.http.HttpVersion;
34  import org.apache.http.NoHttpResponseException;
35  import org.apache.http.ProtocolException;
36  import org.apache.http.io.HttpMessageParser;
37  import org.apache.http.io.SessionInputBuffer;
38  import org.apache.http.util.CharArrayBuffer;
39  import org.junit.Assert;
40  import org.junit.Test;
41  
42  /**
43   * Tests for {@link DefaultResponseParser}.
44   */
45  public class TestDefaultHttpResponseParser {
46  
47      @Test
48      public void testResponseParsingWithSomeGarbage() throws Exception {
49          final String s =
50              "garbage\r\n" +
51              "garbage\r\n" +
52              "more garbage\r\n" +
53              "HTTP/1.1 200 OK\r\n" +
54              "header1: value1\r\n" +
55              "header2: value2\r\n" +
56              "\r\n";
57  
58          final SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII);
59          final HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer);
60  
61          final HttpResponse response = parser.parse();
62          Assert.assertNotNull(response);
63          Assert.assertEquals(HttpVersion.HTTP_1_1, response.getProtocolVersion());
64          Assert.assertEquals(200, response.getStatusLine().getStatusCode());
65  
66          final Header[] headers = response.getAllHeaders();
67          Assert.assertNotNull(headers);
68          Assert.assertEquals(2, headers.length);
69          Assert.assertEquals("header1", headers[0].getName());
70          Assert.assertEquals("header2", headers[1].getName());
71      }
72  
73      @Test(expected=ProtocolException.class)
74      public void testResponseParsingWithTooMuchGarbage() throws Exception {
75          final String s =
76              "garbage\r\n" +
77              "garbage\r\n" +
78              "more garbage\r\n" +
79              "HTTP/1.1 200 OK\r\n" +
80              "header1: value1\r\n" +
81              "header2: value2\r\n" +
82              "\r\n";
83  
84          final SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII);
85          final HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer) {
86  
87              @Override
88              protected boolean reject(final CharArrayBuffer line, final int count) {
89                  return count >= 2;
90              }
91  
92          };
93          parser.parse();
94      }
95  
96      @Test(expected=NoHttpResponseException.class)
97      public void testResponseParsingNoResponse() throws Exception {
98          final SessionInputBuffer inbuffer = new SessionInputBufferMock("", Consts.ASCII);
99          final HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer);
100         parser.parse();
101     }
102 
103     @Test(expected=ProtocolException.class)
104     public void testResponseParsingOnlyGarbage() throws Exception {
105         final String s =
106             "garbage\r\n" +
107             "garbage\r\n" +
108             "more garbage\r\n" +
109             "a lot more garbage\r\n";
110         final SessionInputBuffer inbuffer = new SessionInputBufferMock(s, Consts.ASCII);
111         final HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inbuffer);
112         parser.parse();
113     }
114 
115 }