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.InterruptedIOException;
32  import java.nio.charset.StandardCharsets;
33  
34  import org.apache.hc.core5.http.ClassicHttpResponse;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.MessageConstraintException;
37  import org.apache.hc.core5.http.NoHttpResponseException;
38  import org.apache.hc.core5.http.config.H1Config;
39  import org.apache.hc.core5.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
49      public void testBasicMessageParsing() throws Exception {
50          final String s =
51              "HTTP/1.1 200 OK\r\n" +
52              "Server: whatever\r\n" +
53              "Date: some date\r\n" +
54              "Set-Cookie: c1=stuff\r\n" +
55              "\r\n";
56          final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
57          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
58  
59          final DefaultHttpResponseParser parser = new DefaultHttpResponseParser();
60          final ClassicHttpResponse httpresponse = parser.parse(inBuffer, inputStream);
61  
62          Assert.assertEquals(200, httpresponse.getCode());
63          Assert.assertEquals("OK", httpresponse.getReasonPhrase());
64          final Header[] headers = httpresponse.getHeaders();
65          Assert.assertEquals(3, headers.length);
66      }
67  
68      @Test(expected = NoHttpResponseException.class)
69      public void testConnectionClosedException() throws Exception {
70          final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {});
71          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
72  
73          final DefaultHttpResponseParser parser = new DefaultHttpResponseParser();
74          parser.parse(inBuffer, inputStream);
75      }
76  
77      @Test
78      public void testBasicMessageParsingLeadingEmptyLines() throws Exception {
79          final String s =
80                  "\r\n" +
81                  "\r\n" +
82                  "HTTP/1.1 200 OK\r\n" +
83                  "Server: whatever\r\n" +
84                  "\r\n";
85          final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
86          final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
87  
88          final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(
89                  H1Config.custom().setMaxEmptyLineCount(3).build());
90          final ClassicHttpResponse httpresponse = parser.parse(inBuffer, inputStream);
91  
92          Assert.assertEquals(200, httpresponse.getCode());
93          Assert.assertEquals("OK", httpresponse.getReasonPhrase());
94          final Header[] headers = httpresponse.getHeaders();
95          Assert.assertEquals(1, headers.length);
96      }
97  
98      @Test(expected = MessageConstraintException.class)
99      public void testBasicMessageParsingTooManyLeadingEmptyLines() throws Exception {
100         final String s =
101                 "\r\n" +
102                 "\r\n" +
103                 "\r\n" +
104                 "HTTP/1.1 200 OK\r\n" +
105                 "Server: whatever\r\n" +
106                 "\r\n";
107         final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
108         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
109 
110         final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(
111                 H1Config.custom().setMaxEmptyLineCount(3).build());
112         parser.parse(inBuffer, inputStream);
113     }
114 
115     @Test
116     public void testMessageParsingTimeout() throws Exception {
117         final String s =
118             "HTTP\000/1.1 200\000 OK\r\n" +
119             "Server: wha\000tever\r\n" +
120             "Date: some date\r\n" +
121             "Set-Coo\000kie: c1=stuff\r\n" +
122             "\000\r\n";
123         final TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
124         final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
125 
126         final DefaultHttpResponseParser parser = new DefaultHttpResponseParser();
127 
128         int timeoutCount = 0;
129 
130         ClassicHttpResponse httpresponse = null;
131         for (int i = 0; i < 10; i++) {
132             try {
133                 httpresponse = parser.parse(inBuffer, inputStream);
134                 break;
135             } catch (final InterruptedIOException ex) {
136                 timeoutCount++;
137             }
138         }
139         Assert.assertNotNull(httpresponse);
140         Assert.assertEquals(5, timeoutCount);
141 
142         Assert.assertEquals(200, httpresponse.getCode());
143         Assert.assertEquals("OK", httpresponse.getReasonPhrase());
144         final Header[] headers = httpresponse.getHeaders();
145         Assert.assertEquals(3, headers.length);
146     }
147 
148 }
149