-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestParserTest.java
More file actions
405 lines (337 loc) · 14.7 KB
/
RequestParserTest.java
File metadata and controls
405 lines (337 loc) · 14.7 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package core;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import static core.HttpMessageReader.readExactNumberOfBytes;
import static core.HttpMessageReader.readStartLineAndHeaders;
import static core.HttpStatusCode.*;
import static core.RequestParser.buildAbsoluteURI;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static org.junit.Assert.*;
public class RequestParserTest {
RequestParser requestParser;
Configuration configuration;
@Before
public void setUp() throws Exception {
configuration = new Configuration();
requestParser = new RequestParser(configuration);
}
private ByteArrayInputStream in(String httpPacket) {
return new ByteArrayInputStream(httpPacket.getBytes(ISO_8859_1));
}
private void assertHttpError(String httpPacket, HttpStatusCode error) {
try {
requestParser.parse(in(httpPacket));
fail();
} catch (HttpError e) {
assertEquals(error, e.getErrorCode());
}
}
private byte[] mergeByteArrays(byte[] firstArray, byte[] secondArray) {
byte[] resultingArray = new byte[firstArray.length + secondArray.length];
System.arraycopy(firstArray, 0, resultingArray, 0, firstArray.length);
System.arraycopy(secondArray, 0, resultingArray, firstArray.length, secondArray.length);
return resultingArray;
}
private Request parse(String httpPacket) {
requestParser.parse(in(httpPacket));
return requestParser.request;
}
@Test
public void testReadStartLine_RFC2616_4_1() throws Exception {
assertEquals("request line", readStartLineAndHeaders(in("request line\r\n\r\nbody")));
}
@Test
public void testReadStartLineAndHeaders_RFC2616_4_1() throws Exception {
assertEquals("request line\r\nheader", readStartLineAndHeaders(in("request line\r\nheader\r\n\r\nbody")));
}
@Test
public void testReadStartLineAndBody_RFC2616_4_1() throws Exception {
InputStream in = in("request line\r\n\r\nbody");
assertEquals("request line", readStartLineAndHeaders(in));
assertEquals("body", readExactNumberOfBytes(in, 4, StandardCharsets.ISO_8859_1));
}
@Test
public void testReadStartLineAndHeadersAndBody_RFC2616_4_1() throws Exception {
InputStream in = in("request line\r\nheader 1\r\nheader 2\r\n\r\nbody\r\n\r\nbody");
assertEquals("request line\r\nheader 1\r\nheader 2", readStartLineAndHeaders(in));
assertEquals("body\r\n\r\nbody", readExactNumberOfBytes(in, 12, StandardCharsets.ISO_8859_1));
}
@Test
public void testSkipNewLinesBeforeStartLine_RFC2616_4_1() throws Exception {
assertEquals("abc\r\ndef", readStartLineAndHeaders(in("\r\n\r\nabc\r\ndef\r\n\r\n")));
}
@Test
public void testReadNonAsciiHeaders() throws Exception {
assertEquals("abc\u00FF", readStartLineAndHeaders(in("abc\u00FF\r\n\r\n")));
}
@Test
public void testMethod() throws Exception {
Request request = parse("POST / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertEquals("POST", request.requestMethod);
}
@Test
public void testRequestURI() throws Exception {
Request request = parse("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertEquals("/", request.requestURI.getPath());
}
@Test
public void testHttpVersion() throws Exception {
Request request = parse("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertEquals("HTTP/1.1", request.httpVersion);
}
@Test
public void testInvalidMethod_RFC2616_5_1_1() throws Exception {
assertHttpError("~!#$@!%#^&$ / HTTP/1.1\r\nHost: localhost\r\n\r\n", BAD_REQUEST);
}
@Test
public void testInvalidRequestURI() throws Exception {
assertHttpError("GET /\\\\ HTTP/1.1\r\nHost: localhost\r\n\r\n", BAD_REQUEST);
}
@Test
public void testRelativeRequestURI() throws Exception {
assertHttpError("GET test.html HTTP/1.1\r\nHost: localhost\r\n\r\n", BAD_REQUEST);
}
@Test
public void testInvalidHost() throws Exception {
assertHttpError("GET / HTTP/1.1\r\nHost: @#^%$&\r\n\r\n", BAD_REQUEST);
}
@Test
public void testHostWithPort() throws Exception {
Request request = parse("GET / HTTP/1.1\r\nHost: localhost:8888\r\n\r\n");
assertEquals(8888, request.requestURI.getPort());
}
@Test
public void testRelativeRequestUri() throws Exception {
assertHttpError("GET test.html HTTP/1.1\r\nHost: localhost\r\n\r\n", BAD_REQUEST);
}
@Test
public void testInvalidHttpVersion_RFC2616_3_1() throws Exception {
assertHttpError("GET / HTTP99\r\nHost: localhost\r\n\r\n", BAD_REQUEST);
}
@Test
public void testInvalidStartLineFormat_RFC2616_5_1() throws Exception {
assertHttpError("GET / HTTP/1.1 FOO BAR\r\nHost: localhost\r\n\r\n", BAD_REQUEST);
}
@Test
public void testHeaders() throws Exception {
Request request = parse("GET / HTTP/1.1\r\nHost: localhost\r\nUser-Agent: java\r\n\r\n");
assertEquals("localhost", request.getHeader("Host"));
assertEquals("java", request.getHeader("User-Agent"));
}
@Test
public void testInvalidHeaderLine_RFC2616_4_2() throws Exception {
assertHttpError("GET / HTTP/1.1\r\nHost: localhost\r\nHeader test\r\n\r\n", BAD_REQUEST);
}
@Test
public void testNewLinesInHeaderValues_RFC2616_4_2() throws Exception {
Request request = parse("GET / HTTP/1.1\r\nHost: localhost\r\nUser-Agent: ja\r\n\t \r\n va\r\n\r\n");
assertEquals("ja va", request.getHeader("User-Agent"));
}
@Test
public void testNoSpacesAllowedInHeaderName_RFC2616_4_2() throws Exception {
assertHttpError("GET / HTTP/1.1\r\nHost: localhost\r\nHea der: test\r\n\r\n", BAD_REQUEST);
}
@Test
public void testHeaderValueIsTrimmed_RFC2616_4_2() throws Exception {
Request request = parse("GET / HTTP/1.1\r\nHost: localhost \r\n \r\n\r\n");
assertEquals("localhost", request.getHeader("Host"));
}
@Test
public void testMergeMultipleHeadersWithSameName_RFC2616_4_2() throws Exception {
Request request = parse("GET / HTTP/1.1\r\nUser-Agent: browser\r\nHost: localhost\r\nUser-Agent: java\r\n\r\n");
assertEquals("browser, java", request.getHeader("User-Agent"));
}
@Test
public void testHeaderNamesAreCaseInsensitive_RFC2616_4_2() throws Exception {
Request request = parse("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertEquals("localhost", request.getHeader("HOST"));
assertEquals("localhost", request.getHeader("host"));
}
@Test
public void testHeaderNameCaseIsPreserved() throws Exception {
Request request = parse("GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertTrue(request.headers.keySet().contains("Host"));
assertFalse(request.headers.keySet().contains("HOST"));
}
@Test
public void testAbsoluteURINotImplemented_RFC2616_5_1_2() throws Exception {
assertHttpError("GET http://www.google.com/ HTTP/1.1\r\nHost: localhost\r\n\r\n", NOT_IMPLEMENTED);
}
@Test
public void testHostHeaderMustBePresent_RFC2616_5_1_2() throws Exception {
assertHttpError("GET / HTTP/1.1\r\n\r\n", BAD_REQUEST);
assertHttpError("GET / HTTP/1.1\r\nUser-Agent: java\r\n\r\n", BAD_REQUEST);
}
@Test
public void testAbsPathContainsFragment() throws Exception {
Request request = parse("GET /test.html#def HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertEquals("def", request.requestURI.getFragment());
}
@Test
public void testMergeHostAndAbsPathIntoRequestURI() throws Exception {
Request request = parse("GET /test.html?abc=123&a#def HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertEquals("http://localhost/test.html?abc=123&a#def", request.requestURI.toString());
}
@Test
public void testTransferEncodingHeaderNotImplemented() throws Exception {
assertHttpError("GET / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\n", NOT_IMPLEMENTED);
}
@Test
public void testAsteriskRequestUriNotImplemented_RFC2616_5_1_2() throws Exception {
assertHttpError("POST * HTTP/1.1\r\nHost: localhost\r\n\r\n", NOT_IMPLEMENTED);
}
@Test
public void testTooLongRequestUri_RFC2616_10_4_15() throws Exception {
int defaultMaximumURILength = configuration.getMaximumURILength();
configuration.setMaximumURILength(5);
assertHttpError("GET /1234567 HTTP/1.1\r\nHost: localhost\r\n\r\n", REQUEST_URI_TOO_LONG);
configuration.setMaximumURILength(defaultMaximumURILength);
}
@Test
public void testUnsupportedHttpVersion_RFC2616_10_5_6() throws Exception {
assertHttpError("GET / HTTP/0.9\r\nHost: localhost\r\n\r\n", HTTP_VERSION_NOT_SUPPORTED);
}
@Test
public void testUnsupportedMethod_RFC2616_5_1_1() throws Exception {
assertHttpError("ANYMETHOD / HTTP/1.1\r\nHost: localhost\r\n\r\n", NOT_IMPLEMENTED);
}
@Test
public void testParseQuery() throws Exception {
Request request = parse("GET /?b=test&a=abc&a&a=&b=%26123#anchor HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertEquals(new LinkedList<String>() {{
add("abc");
add(null);
add("");
}}, request.parameters.get("a"));
assertEquals(new LinkedList<String>() {{
add("test");
add("&123");
}}, request.parameters.get("b"));
}
@Test
public void testParseEmptyQuery() throws Exception {
Request request = parse("GET /? HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertTrue(request.parameters.isEmpty());
}
@Test
public void testQueryIsParsedForGetRequestsOnly() throws Exception {
Request request = parse("POST /?b=test&a=abc&a&a=&b=%26123#anchor HTTP/1.1\r\nHost: localhost\r\n\r\n");
assertTrue(request.parameters.isEmpty());
}
@Test
public void testSkipBodyIfThereIsNoContentLengthHeader_RFC2616_4_4() throws Exception {
Request request = parse("POST / HTTP/1.1\r\nHost: localhost\r\n\r\nbody here");
assertEquals(null, request.body);
}
@Test
public void testSkipBodyIfContentLengthIsZero_RFC2616_4_4() throws Exception {
Request request = parse("POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 0\r\n\r\nbody here");
assertEquals("", request.body);
}
@Test
public void testContentLengthIsNotInteger_RFC2616_4_4() throws Exception {
assertHttpError("POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: abc\r\n\r\nbody here", BAD_REQUEST);
}
@Test
public void testBodyLengthIsSmallerThanContentLength_RFC2616_4_4() throws Exception {
assertHttpError("POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 99\r\n\r\nbody here", BAD_REQUEST);
}
@Test
public void testBodyIsCutToContentLength_RFC2616_4_4() throws Exception {
Request request = parse("POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 4\r\n\r\nlong long body");
assertEquals("long", request.body);
}
@Test
public void testBody() throws Exception {
Request request = parse("POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 9\r\n\r\nbody here");
assertEquals("body here", request.body);
}
@Test
public void testBodyIsNotTrimmedAndNoLWSAreReplaced() throws Exception {
Request request = parse("POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 19\r\n\r\n\r\nbody \r\n \there\r\n\t ");
assertEquals("\r\nbody \r\n \there\r\n\t ", request.body);
}
@Test
public void testBodyInOtherCharset() throws Exception {
String requestString = "POST /test HTTP/1.0\r\n" +
"Host: www.google.com\r\n" +
"Content-Type: text/html; charset=UTF-16\r\n" +
"Content-length: 12\r\n\r\n";
InputStream in = new ByteArrayInputStream(mergeByteArrays(
requestString.getBytes(StandardCharsets.ISO_8859_1),
"body\uFFFF".getBytes(StandardCharsets.UTF_16)
));
requestParser.parse(in);
Request request = requestParser.request;
assertEquals("body\uFFFF", request.body);
}
@Test
public void testUrlencodedBodyIsParsed_HTML401_specification_17_13_4() throws Exception {
Request request = parse("POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 20\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\na=1&a=&bc=12+%26%203");
assertEquals("a=1&a=&bc=12+%26%203", request.body);
assertEquals(new LinkedList<String>() {{
add("1");
add("");
}}, request.parameters.get("a"));
assertEquals(new LinkedList<String>() {{
add("12 & 3");
}}, request.parameters.get("bc"));
}
@Test
public void testParseEmptyBody() throws Exception {
Request request = parse("POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 0\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n");
assertTrue(request.parameters.isEmpty());
}
@Test
public void testMultipartBodyNotImplemented_HTML401_specification_17_13_4() throws Exception {
assertHttpError("POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 4\r\nContent-Type: multipart/form-data\r\n\r\nbody", NOT_IMPLEMENTED);
}
@Test
public void testBodyWithOtherContentTypeIsNotParsed() throws Exception {
Request request = parse("POST / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 20\r\nContent-Type: test\r\n\r\na=1&a=&bc=12+%26%203");
assertEquals("a=1&a=&bc=12+%26%203", request.body);
assertTrue(request.parameters.isEmpty());
}
@Test
public void testQueryIsParsedForPostRequestsOnly() throws Exception {
Request request = parse("GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 20\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\na=1&a=&bc=12+%26%203");
assertTrue(request.parameters.isEmpty());
}
@Test
public void testContentHeadersRequireBody() throws Exception {
assertHttpError("GET / HTTP/1.1\r\nHost: localhost\r\nContent-Type: test\r\n\r\n", BAD_REQUEST);
}
@Test
public void testSetFieldsSuccess() throws Exception {
Request request = requestParser.setFields(in("GET /?a=1#abc HTTP/1.1\r\nHost: localhost\r\nContent-Length: 4\r\n\r\nbody"));
assertEquals("GET", request.requestMethod);
assertEquals("http://localhost/?a=1#abc", request.requestURI.toString());
assertEquals("HTTP/1.1", request.httpVersion);
assertEquals("body", request.body);
assertEquals("1", request.parameters.get("a").get(0));
assertEquals(null, request.responseStatusCode);
}
@Test
public void testSetFieldsFailure() throws Exception {
Request request = requestParser.setFields(in("GET /?a=1#abc HTTP/0.9\r\nHost: localhost\r\nContent-Length: 4\r\n\r\nbody"));
assertEquals("GET", request.requestMethod);
assertEquals("/?a=1#abc", request.requestURI.toString());
assertEquals("HTTP/0.9", request.httpVersion);
assertEquals(null, request.body);
assertEquals(null, request.parameters.get("a"));
assertEquals(HTTP_VERSION_NOT_SUPPORTED, request.responseStatusCode);
}
@Test
public void testUseLFInsteadOfCRLF_RFC2616_2_2() throws Exception {
assertHttpError("GET / HTTP/1.1\nHost: localhost\n\n", BAD_REQUEST);
}
@Test
public void testBuildAbsoluteURI() throws Exception {
assertEquals("http://localhost/test.html?a=b#c", buildAbsoluteURI("localhost", new URI("/test.html?a=b#c")).toString());
}
}