1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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.config.Http1Config;
38 import org.apache.hc.core5.http.io.SessionInputBuffer;
39 import org.junit.jupiter.api.Assertions;
40 import org.junit.jupiter.api.Test;
41
42
43
44
45 public class TestResponseParser {
46
47 @Test
48 public void testBasicMessageParsing() throws Exception {
49 final String s =
50 "HTTP/1.1 200 OK\r\n" +
51 "Server: whatever\r\n" +
52 "Date: some date\r\n" +
53 "Set-Cookie: c1=stuff\r\n" +
54 "\r\n";
55 final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
56 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
57
58 final DefaultHttpResponseParser parser = new DefaultHttpResponseParser();
59 final ClassicHttpResponse httpresponse = parser.parse(inBuffer, inputStream);
60
61 Assertions.assertEquals(200, httpresponse.getCode());
62 Assertions.assertEquals("OK", httpresponse.getReasonPhrase());
63 final Header[] headers = httpresponse.getHeaders();
64 Assertions.assertEquals(3, headers.length);
65 }
66
67 @Test
68 public void testConnectionClosedException() throws Exception {
69 final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {});
70 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
71
72 final DefaultHttpResponseParser parser = new DefaultHttpResponseParser();
73 final ClassicHttpResponse response = parser.parse(inBuffer, inputStream);
74 Assertions.assertNull(response);
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 Http1Config.custom().setMaxEmptyLineCount(3).build());
90 final ClassicHttpResponse httpresponse = parser.parse(inBuffer, inputStream);
91
92 Assertions.assertEquals(200, httpresponse.getCode());
93 Assertions.assertEquals("OK", httpresponse.getReasonPhrase());
94 final Header[] headers = httpresponse.getHeaders();
95 Assertions.assertEquals(1, headers.length);
96 }
97
98 @Test
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 Http1Config.custom().setMaxEmptyLineCount(3).build());
112 Assertions.assertThrows(MessageConstraintException.class, () ->
113 parser.parse(inBuffer, inputStream));
114 }
115
116 @Test
117 public void testMessageParsingTimeout() throws Exception {
118 final String s =
119 "HTTP\000/1.1 200\000 OK\r\n" +
120 "Server: wha\000tever\r\n" +
121 "Date: some date\r\n" +
122 "Set-Coo\000kie: c1=stuff\r\n" +
123 "\000\r\n";
124 final TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
125 final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
126
127 final DefaultHttpResponseParser parser = new DefaultHttpResponseParser();
128
129 int timeoutCount = 0;
130
131 ClassicHttpResponse httpresponse = null;
132 for (int i = 0; i < 10; i++) {
133 try {
134 httpresponse = parser.parse(inBuffer, inputStream);
135 break;
136 } catch (final InterruptedIOException ex) {
137 timeoutCount++;
138 }
139 }
140 Assertions.assertNotNull(httpresponse);
141 Assertions.assertEquals(5, timeoutCount);
142
143 Assertions.assertEquals(200, httpresponse.getCode());
144 Assertions.assertEquals("OK", httpresponse.getReasonPhrase());
145 final Header[] headers = httpresponse.getHeaders();
146 Assertions.assertEquals(3, headers.length);
147 }
148
149 }
150