Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: tests/standalone/src/io/HttpParserTest.dart

Issue 9834008: Add error handling to the HTTP library (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #source("../../../../runtime/bin/http_parser.dart"); 5 #source("../../../../runtime/bin/http_parser.dart");
6 6
7 class HttpParserTest { 7 class HttpParserTest {
8 static void runAllTests() { 8 static void runAllTests() {
9 testParseRequest(); 9 testParseRequest();
10 testParseResponse(); 10 testParseResponse();
11 testParseInvalidRequest();
12 testParseInvalidResponse();
11 } 13 }
12 14
13 static void _testParseRequest(String request, 15 static void _testParseRequest(String request,
14 String expectedMethod, 16 String expectedMethod,
15 String expectedUri, 17 String expectedUri,
16 [int expectedContentLength = -1, 18 [int expectedContentLength = -1,
17 int expectedBytesReceived = 0, 19 int expectedBytesReceived = 0,
18 Map expectedHeaders = null, 20 Map expectedHeaders = null,
19 bool chunked = false]) { 21 bool chunked = false]) {
20 _HttpParser httpParser; 22 _HttpParser httpParser;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 } 84 }
83 85
84 // Test parsing the request three times delivering the data in 86 // Test parsing the request three times delivering the data in
85 // different chunks. 87 // different chunks.
86 List<int> requestData = request.charCodes(); 88 List<int> requestData = request.charCodes();
87 testWrite(requestData); 89 testWrite(requestData);
88 testWrite(requestData, 10); 90 testWrite(requestData, 10);
89 testWrite(requestData, 1); 91 testWrite(requestData, 1);
90 } 92 }
91 93
94 static void _testParseInvalidRequest(String request) {
95 _HttpParser httpParser;
96 bool errorCalled;
97
98 void reset() {
99 httpParser = new _HttpParser();
100 httpParser.responseStart = (s, r) { Expect.fail("Expected request"); };
101 httpParser.error = (e) {
102 errorCalled = true;
103 };
104
105 errorCalled = false;
106 }
107
108 void checkExpectations() {
Mads Ager (google) 2012/03/22 14:30:08 Not sure that this method adds much. Just inline?
Søren Gjesse 2012/03/23 07:34:20 Done.
109 Expect.isTrue(errorCalled);
110 }
111
112 void testWrite(List<int> requestData, [int chunkSize = -1]) {
113 if (chunkSize == -1) chunkSize = requestData.length;
114 reset();
115 for (int pos = 0; pos < requestData.length; pos += chunkSize) {
116 int remaining = requestData.length - pos;
117 int writeLength = Math.min(chunkSize, remaining);
118 httpParser.writeList(requestData, pos, writeLength);
119 }
120 checkExpectations();
121 }
122
123 // Test parsing the request three times delivering the data in
124 // different chunks.
125 List<int> requestData = request.charCodes();
126 testWrite(requestData);
127 testWrite(requestData, 10);
128 testWrite(requestData, 1);
129 }
130
92 static void _testParseResponse(String response, 131 static void _testParseResponse(String response,
93 int expectedStatusCode, 132 int expectedStatusCode,
94 String expectedReasonPhrase, 133 String expectedReasonPhrase,
95 [int expectedContentLength = -1, 134 [int expectedContentLength = -1,
96 int expectedBytesReceived = 0, 135 int expectedBytesReceived = 0,
97 Map expectedHeaders = null, 136 Map expectedHeaders = null,
98 bool chunked = false, 137 bool chunked = false,
99 bool close = false, 138 bool close = false,
100 String responseToMethod = null]) { 139 String responseToMethod = null]) {
101 _HttpParser httpParser; 140 _HttpParser httpParser;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 } 209 }
171 210
172 // Test parsing the request three times delivering the data in 211 // Test parsing the request three times delivering the data in
173 // different chunks. 212 // different chunks.
174 List<int> responseData = response.charCodes(); 213 List<int> responseData = response.charCodes();
175 testWrite(responseData); 214 testWrite(responseData);
176 testWrite(responseData, 10); 215 testWrite(responseData, 10);
177 testWrite(responseData, 1); 216 testWrite(responseData, 1);
178 } 217 }
179 218
219 static void _testParseInvalidResponse(String response, [bool close = false]) {
220 _HttpParser httpParser;
221 bool errorCalled;
222
223 void reset() {
224 httpParser = new _HttpParser();
225 httpParser.requestStart = (m, u) => Expect.fail("Expected response");
226 httpParser.error = (e) => errorCalled = true;
227
228 errorCalled = false;
229 }
230
231 void checkExpectations() {
Mads Ager (google) 2012/03/22 14:30:08 Ditto.
Søren Gjesse 2012/03/23 07:34:20 Done.
232 Expect.isTrue(errorCalled);
233 }
234
235 void testWrite(List<int> requestData, [int chunkSize = -1]) {
236 if (chunkSize == -1) chunkSize = requestData.length;
237 reset();
238 for (int pos = 0; pos < requestData.length; pos += chunkSize) {
239 int remaining = requestData.length - pos;
240 int writeLength = Math.min(chunkSize, remaining);
241 httpParser.writeList(requestData, pos, writeLength);
242 }
243 if (close) httpParser.connectionClosed();
244 checkExpectations();
245 }
246
247 // Test parsing the request three times delivering the data in
248 // different chunks.
249 List<int> responseData = response.charCodes();
250 testWrite(responseData);
251 testWrite(responseData, 10);
252 testWrite(responseData, 1);
253 }
254
180 static void testParseRequest() { 255 static void testParseRequest() {
181 String request; 256 String request;
182 Map headers; 257 Map headers;
183 request = "GET / HTTP/1.1\r\n\r\n"; 258 request = "GET / HTTP/1.1\r\n\r\n";
184 _testParseRequest(request, "GET", "/"); 259 _testParseRequest(request, "GET", "/");
185 260
186 request = "POST / HTTP/1.1\r\n\r\n"; 261 request = "POST / HTTP/1.1\r\n\r\n";
187 _testParseRequest(request, "POST", "/"); 262 _testParseRequest(request, "POST", "/");
188 263
189 request = "GET /index.html HTTP/1.1\r\n\r\n"; 264 request = "GET /index.html HTTP/1.1\r\n\r\n";
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 0123456789012345678901234567890\r 505 0123456789012345678901234567890\r
431 0\r\n\r\n"""; 506 0\r\n\r\n""";
432 _testParseResponse(response, 507 _testParseResponse(response,
433 200, 508 200,
434 "OK", 509 "OK",
435 expectedContentLength: -1, 510 expectedContentLength: -1,
436 expectedBytesReceived: 57, 511 expectedBytesReceived: 57,
437 chunked: true); 512 chunked: true);
438 513
439 // Test HTTP response without any transfer length indications 514 // Test HTTP response without any transfer length indications
440 // where closing the connections indicated end of body. 515 // where closing the connections indicates end of body.
441 response = """ 516 response = """
442 HTTP/1.1 200 OK\r 517 HTTP/1.1 200 OK\r
443 \r 518 \r
444 01234567890123456789012345 519 01234567890123456789012345
445 0123456789012345678901234567890 520 0123456789012345678901234567890
446 """; 521 """;
447 _testParseResponse(response, 522 _testParseResponse(response,
448 200, 523 200,
449 "OK", 524 "OK",
450 expectedContentLength: -1, 525 expectedContentLength: -1,
451 expectedBytesReceived: 59, 526 expectedBytesReceived: 59,
452 close: true); 527 close: true);
453 } 528 }
529
530 static void testParseInvalidRequest() {
531 String request;
532 request = "GET /\r\n\r\n";
533 _testParseInvalidRequest(request);
534
535 request = "GET / \r\n\r\n";
536 _testParseInvalidRequest(request);
537
538 request = "/ HTTP/1.1\r\n\r\n";
539 _testParseInvalidRequest(request);
540
541 request = "GET HTTP/1.1\r\n\r\n";
542 _testParseInvalidRequest(request);
543
544 request = " / HTTP/1.1\r\n\r\n";
545 _testParseInvalidRequest(request);
546
547 request = "@ / HTTP/1.1\r\n\r\n";
548 _testParseInvalidRequest(request);
549
550 request = "GET / TTP/1.1\r\n\r\n";
551 _testParseInvalidRequest(request);
552
553 request = "GET / HTTP/1.\r\n\r\n";
554 _testParseInvalidRequest(request);
555
556 // Currently no HTTP 1.0 support.
557 request = "GET / HTTP/1.0\r\n\r\n";
558 _testParseInvalidRequest(request);
559 }
560
561 static void testParseInvalidResponse() {
562 String response;
563
564 response = "HTTP/1.1\r\nContent-Length: 0\r\n\r\n";
565 _testParseInvalidResponse(response);
566
567 response = "HTTP/1.1 \r\nContent-Length: 0\r\n\r\n";
568 _testParseInvalidResponse(response);
569
570 response = "HTTP/1.1 200\r\nContent-Length: 0\r\n\r\n";
571 _testParseInvalidResponse(response);
572
573 response = "HTTP/1.1 200 \r\nContent-Length: 0\r\n\r\n";
574 _testParseInvalidResponse(response);
575
576 response = "HTTP/1.1 OK\r\nContent-Length: 0\r\n\r\n";
577 _testParseInvalidResponse(response);
578
579 response = "200 OK\r\nContent-Length: 0\r\n\r\n";
580 _testParseInvalidResponse(response);
581
582 response = "HTTP/1. 200 OK\r\nContent-Length: 0\r\n\r\n";
583 _testParseInvalidResponse(response);
584
585 response = "HTTP/1.1 200 O\rK\r\nContent-Length: 0\r\n\r\n";
586 _testParseInvalidResponse(response);
587
588 response = "HTTP/1.1 000 OK\r\nContent-Length: 0\r\n\r\n";
589 _testParseInvalidResponse(response);
590
591 response = "HTTP/1.1 999 Server Error\r\nContent-Length: 0\r\n\r\n";
592 _testParseInvalidResponse(response);
593
594 response = "HTTP/1.1 200 OK\r\nContent-Length: x\r\n\r\n";
595 _testParseInvalidResponse(response);
596
597 response = """
598 HTTP/1.1 200 OK\r
599 Transfer-Encoding: chunked\r
600 \r
601 1A\r
602 01234567890123456789012345\r
603 1g\r
604 0123456789012345678901234567890\r
605 0\r\n\r\n""";
606 _testParseInvalidResponse(response);
607 }
454 } 608 }
455 609
456 610
457 void main() { 611 void main() {
458 HttpParserTest.runAllTests(); 612 HttpParserTest.runAllTests();
459 } 613 }
OLDNEW
« runtime/bin/http_parser.dart ('K') | « runtime/bin/socket_stream_impl.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698