OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:math'; | 5 import 'dart:math'; |
6 import 'dart:scalarlist'; | 6 import 'dart:scalarlist'; |
7 | 7 |
| 8 part '../../../sdk/lib/io/http.dart'; |
| 9 part '../../../sdk/lib/io/http_headers.dart'; |
8 part '../../../sdk/lib/io/http_parser.dart'; | 10 part '../../../sdk/lib/io/http_parser.dart'; |
9 | 11 |
10 class HttpParserTest { | 12 class HttpParserTest { |
11 static void runAllTests() { | 13 static void runAllTests() { |
12 testParseRequest(); | 14 testParseRequest(); |
13 testParseResponse(); | 15 testParseResponse(); |
14 testParseInvalidRequest(); | 16 testParseInvalidRequest(); |
15 testParseInvalidResponse(); | 17 testParseInvalidResponse(); |
16 } | 18 } |
17 | 19 |
18 static void _testParseRequest(String request, | 20 static void _testParseRequest(String request, |
19 String expectedMethod, | 21 String expectedMethod, |
20 String expectedUri, | 22 String expectedUri, |
21 {int expectedContentLength: 0, | 23 {int expectedContentLength: 0, |
22 int expectedBytesReceived: 0, | 24 int expectedBytesReceived: 0, |
23 Map expectedHeaders: null, | 25 Map expectedHeaders: null, |
24 bool chunked: false, | 26 bool chunked: false, |
25 bool upgrade: false, | 27 bool upgrade: false, |
26 int unparsedLength: 0, | 28 int unparsedLength: 0, |
27 bool connectionClose: false, | 29 bool connectionClose: false, |
28 String expectedVersion: "1.1"}) { | 30 String expectedVersion: "1.1"}) { |
29 _HttpParser httpParser; | 31 _HttpParser httpParser; |
30 bool headersCompleteCalled; | 32 bool headersCompleteCalled; |
31 bool dataEndCalled; | 33 bool dataEndCalled; |
32 String method; | 34 String method; |
33 String uri; | 35 String uri; |
34 String version; | 36 String version; |
35 Map headers; | 37 HttpHeaders headers; |
36 int contentLength; | 38 int contentLength; |
37 int bytesReceived; | 39 int bytesReceived; |
38 | 40 |
39 void reset() { | 41 void reset() { |
40 httpParser = new _HttpParser.requestParser(); | 42 httpParser = new _HttpParser.requestParser(); |
41 httpParser.requestStart = (m, u, v) { method = m; uri = u; version = v; }; | 43 httpParser.requestStart = (m, u, v, h) { |
42 httpParser.responseStart = (s, r, v) { Expect.fail("Expected request"); }; | 44 method = m; |
43 httpParser.headerReceived = (f, v) { | 45 uri = u; |
44 Expect.isFalse(headersCompleteCalled); | 46 version = v; |
45 headers[f] = v; | 47 headers = h; |
46 }; | 48 headersCompleteCalled = true; |
47 httpParser.headersComplete = () { | |
48 Expect.isFalse(headersCompleteCalled); | |
49 if (!chunked) { | 49 if (!chunked) { |
50 Expect.equals(expectedContentLength, httpParser.contentLength); | 50 Expect.equals(expectedContentLength, httpParser.contentLength); |
51 } else { | 51 } else { |
52 Expect.equals(-1, httpParser.contentLength); | 52 Expect.equals(-1, httpParser.contentLength); |
53 } | 53 } |
54 if (expectedHeaders != null) { | 54 if (expectedHeaders != null) { |
55 expectedHeaders.forEach( | 55 expectedHeaders.forEach( |
56 (String name, String value) => | 56 (String name, String value) => |
57 Expect.equals(value, headers[name])); | 57 Expect.equals(value, headers[name][0])); |
58 } | 58 } |
59 Expect.equals(upgrade, httpParser.upgrade); | 59 Expect.equals(upgrade, httpParser.upgrade); |
60 Expect.equals(connectionClose, !httpParser.persistentConnection); | 60 Expect.equals(connectionClose, !httpParser.persistentConnection); |
61 headersCompleteCalled = true; | 61 headersCompleteCalled = true; |
62 }; | 62 }; |
| 63 httpParser.responseStart = (s, r, v, h) { |
| 64 Expect.fail("Expected request"); |
| 65 }; |
63 httpParser.dataReceived = (List<int> data) { | 66 httpParser.dataReceived = (List<int> data) { |
64 Expect.isTrue(headersCompleteCalled); | 67 Expect.isTrue(headersCompleteCalled); |
65 bytesReceived += data.length; | 68 bytesReceived += data.length; |
66 }; | 69 }; |
67 httpParser.dataEnd = (close) { | 70 httpParser.dataEnd = (close) { |
68 Expect.isFalse(close); | 71 Expect.isFalse(close); |
69 dataEndCalled = true; | 72 dataEndCalled = true; |
70 }; | 73 }; |
71 httpParser.closed = () { }; | 74 httpParser.closed = () { }; |
72 | 75 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 bool upgrade: false, | 169 bool upgrade: false, |
167 int unparsedLength: 0, | 170 int unparsedLength: 0, |
168 String expectedVersion: "1.1"}) { | 171 String expectedVersion: "1.1"}) { |
169 _HttpParser httpParser; | 172 _HttpParser httpParser; |
170 bool headersCompleteCalled; | 173 bool headersCompleteCalled; |
171 bool dataEndCalled; | 174 bool dataEndCalled; |
172 bool dataEndClose; | 175 bool dataEndClose; |
173 int statusCode; | 176 int statusCode; |
174 String reasonPhrase; | 177 String reasonPhrase; |
175 String version; | 178 String version; |
176 Map headers; | 179 HttpHeaders headers; |
177 int contentLength; | 180 int contentLength; |
178 int bytesReceived; | 181 int bytesReceived; |
179 | 182 |
180 void reset() { | 183 void reset() { |
181 httpParser = new _HttpParser.responseParser(); | 184 httpParser = new _HttpParser.responseParser(); |
182 if (responseToMethod != null) { | 185 if (responseToMethod != null) { |
183 httpParser.responseToMethod = responseToMethod; | 186 httpParser.responseToMethod = responseToMethod; |
184 } | 187 } |
185 httpParser.requestStart = (m, u, v) => Expect.fail("Expected response"); | 188 httpParser.requestStart = (m, u, v, h) { |
186 httpParser.responseStart = (s, r, v) { | 189 Expect.fail("Expected response"); |
| 190 }; |
| 191 httpParser.responseStart = (s, r, v, h) { |
187 statusCode = s; | 192 statusCode = s; |
188 reasonPhrase = r; | 193 reasonPhrase = r; |
189 version = v; | 194 version = v; |
190 }; | 195 headers = h; |
191 httpParser.headerReceived = (f, v) { | |
192 Expect.isFalse(headersCompleteCalled); | |
193 headers[f] = v; | |
194 }; | |
195 httpParser.headersComplete = () { | |
196 Expect.isFalse(headersCompleteCalled); | 196 Expect.isFalse(headersCompleteCalled); |
197 if (!chunked && !close) { | 197 if (!chunked && !close) { |
198 Expect.equals(expectedContentLength, httpParser.contentLength); | 198 Expect.equals(expectedContentLength, httpParser.contentLength); |
199 } else { | 199 } else { |
200 Expect.equals(-1, httpParser.contentLength); | 200 Expect.equals(-1, httpParser.contentLength); |
201 } | 201 } |
202 if (expectedHeaders != null) { | 202 if (expectedHeaders != null) { |
203 expectedHeaders.forEach((String name, String value) { | 203 expectedHeaders.forEach((String name, String value) { |
204 Expect.equals(value, headers[name]); | 204 Expect.equals(value, headers[name][0]); |
205 }); | 205 }); |
206 } | 206 } |
207 Expect.equals(upgrade, httpParser.upgrade); | 207 Expect.equals(upgrade, httpParser.upgrade); |
208 headersCompleteCalled = true; | 208 headersCompleteCalled = true; |
209 }; | 209 }; |
210 httpParser.dataReceived = (List<int> data) { | 210 httpParser.dataReceived = (List<int> data) { |
211 Expect.isTrue(headersCompleteCalled); | 211 Expect.isTrue(headersCompleteCalled); |
212 bytesReceived += data.length; | 212 bytesReceived += data.length; |
213 }; | 213 }; |
214 httpParser.dataEnd = (close) { | 214 httpParser.dataEnd = (close) { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 // Methods with HTTP prefix. | 318 // Methods with HTTP prefix. |
319 "H", "HT", "HTT", "HTTP", "HX", "HTX", "HTTX", "HTTPX"]; | 319 "H", "HT", "HTT", "HTTP", "HX", "HTX", "HTTX", "HTTPX"]; |
320 methods.forEach((method) { | 320 methods.forEach((method) { |
321 request = "$method / HTTP/1.1\r\n\r\n"; | 321 request = "$method / HTTP/1.1\r\n\r\n"; |
322 _testParseRequest(request, method, "/"); | 322 _testParseRequest(request, method, "/"); |
323 request = "$method /index.html HTTP/1.1\r\n\r\n"; | 323 request = "$method /index.html HTTP/1.1\r\n\r\n"; |
324 _testParseRequest(request, method, "/index.html"); | 324 _testParseRequest(request, method, "/index.html"); |
325 }); | 325 }); |
326 | 326 |
327 request = "GET / HTTP/1.0\r\n\r\n"; | 327 request = "GET / HTTP/1.0\r\n\r\n"; |
328 _testParseRequest(request, "GET", "/", expectedVersion: "1.0", connectionClo
se: true); | 328 _testParseRequest(request, "GET", "/", |
| 329 expectedVersion: "1.0", |
| 330 connectionClose: true); |
329 | 331 |
330 request = "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n"; | 332 request = "GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n"; |
331 _testParseRequest(request, "GET", "/", expectedVersion: "1.0"); | 333 _testParseRequest(request, "GET", "/", expectedVersion: "1.0"); |
332 | 334 |
333 request = """ | 335 request = """ |
334 POST /test HTTP/1.1\r | 336 POST /test HTTP/1.1\r |
335 AAA: AAA\r | 337 AAA: AAA\r |
336 \r | 338 \r |
337 """; | 339 """; |
338 _testParseRequest(request, "POST", "/test"); | 340 _testParseRequest(request, "POST", "/test"); |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 response = "HTTP/1.1 100 Continue\r\nContent-Length: 0\r\n\r\n"; | 583 response = "HTTP/1.1 100 Continue\r\nContent-Length: 0\r\n\r\n"; |
582 _testParseResponse(response, 100, "Continue", expectedContentLength: 0); | 584 _testParseResponse(response, 100, "Continue", expectedContentLength: 0); |
583 | 585 |
584 response = "HTTP/1.1 100 Continue\r\nContent-Length: 10\r\n\r\n"; | 586 response = "HTTP/1.1 100 Continue\r\nContent-Length: 10\r\n\r\n"; |
585 _testParseResponse(response, | 587 _testParseResponse(response, |
586 100, | 588 100, |
587 "Continue", | 589 "Continue", |
588 expectedContentLength: 10, | 590 expectedContentLength: 10, |
589 expectedBytesReceived: 0); | 591 expectedBytesReceived: 0); |
590 | 592 |
591 response = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: Close\r\n\r\
n"; | 593 response = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n" |
| 594 "Connection: Close\r\n\r\n"; |
592 _testParseResponse(response, | 595 _testParseResponse(response, |
593 200, | 596 200, |
594 "OK", | 597 "OK", |
595 expectedContentLength: 0, | 598 expectedContentLength: 0, |
596 connectionClose: true); | 599 connectionClose: true); |
597 | 600 |
598 response = "HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n"; | 601 response = "HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n"; |
599 _testParseResponse(response, | 602 _testParseResponse(response, |
600 200, | 603 200, |
601 "OK", | 604 "OK", |
602 expectedContentLength: 0, | 605 expectedContentLength: 0, |
603 expectedVersion: "1.0", | 606 expectedVersion: "1.0", |
604 connectionClose: true); | 607 connectionClose: true); |
605 | 608 |
606 response = "HTTP/1.0 200 OK\r\nContent-Length: 0\r\nConnection: Keep-Alive\r
\n\r\n"; | 609 response = "HTTP/1.0 200 OK\r\nContent-Length: 0\r\n" |
607 _testParseResponse(response, 200, "OK", expectedContentLength: 0, expectedVe
rsion: "1.0"); | 610 "Connection: Keep-Alive\r\n\r\n"; |
| 611 _testParseResponse(response, |
| 612 200, |
| 613 "OK", |
| 614 expectedContentLength: 0, |
| 615 expectedVersion: "1.0"); |
608 | 616 |
609 response = "HTTP/1.1 204 No Content\r\nContent-Length: 11\r\n\r\n"; | 617 response = "HTTP/1.1 204 No Content\r\nContent-Length: 11\r\n\r\n"; |
610 _testParseResponse(response, | 618 _testParseResponse(response, |
611 204, | 619 204, |
612 "No Content", | 620 "No Content", |
613 expectedContentLength: 11, | 621 expectedContentLength: 11, |
614 expectedBytesReceived: 0); | 622 expectedBytesReceived: 0); |
615 | 623 |
616 response = "HTTP/1.1 304 Not Modified\r\nContent-Length: 12\r\n\r\n"; | 624 response = "HTTP/1.1 304 Not Modified\r\nContent-Length: 12\r\n\r\n"; |
617 _testParseResponse(response, | 625 _testParseResponse(response, |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
845 0123456789012345678901234567890\r | 853 0123456789012345678901234567890\r |
846 0\r\n\r\n"""; | 854 0\r\n\r\n"""; |
847 _testParseInvalidResponse(response); | 855 _testParseInvalidResponse(response); |
848 } | 856 } |
849 } | 857 } |
850 | 858 |
851 | 859 |
852 void main() { | 860 void main() { |
853 HttpParserTest.runAllTests(); | 861 HttpParserTest.runAllTests(); |
854 } | 862 } |
OLD | NEW |