| 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 #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(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 expectedHeaders.forEach( | 47 expectedHeaders.forEach( |
| 48 (String name, String value) => | 48 (String name, String value) => |
| 49 Expect.equals(value, headers[name])); | 49 Expect.equals(value, headers[name])); |
| 50 } | 50 } |
| 51 headersCompleteCalled = true; | 51 headersCompleteCalled = true; |
| 52 }; | 52 }; |
| 53 httpParser.dataReceived = (List<int> data) { | 53 httpParser.dataReceived = (List<int> data) { |
| 54 Expect.isTrue(headersCompleteCalled); | 54 Expect.isTrue(headersCompleteCalled); |
| 55 bytesReceived += data.length; | 55 bytesReceived += data.length; |
| 56 }; | 56 }; |
| 57 httpParser.dataEnd = () => dataEndCalled = true; | 57 httpParser.dataEnd = (close) { |
| 58 Expect.isFalse(close); |
| 59 dataEndCalled = true; |
| 60 }; |
| 58 | 61 |
| 59 headersCompleteCalled = false; | 62 headersCompleteCalled = false; |
| 60 dataEndCalled = false; | 63 dataEndCalled = false; |
| 61 method = null; | 64 method = null; |
| 62 uri = null; | 65 uri = null; |
| 63 headers = new Map(); | 66 headers = new Map(); |
| 64 bytesReceived = 0; | 67 bytesReceived = 0; |
| 65 } | 68 } |
| 66 | 69 |
| 67 void testWrite(List<int> requestData, [int chunkSize = -1]) { | 70 void testWrite(List<int> requestData, [int chunkSize = -1]) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 } | 124 } |
| 122 | 125 |
| 123 static void _testParseResponse(String response, | 126 static void _testParseResponse(String response, |
| 124 int expectedStatusCode, | 127 int expectedStatusCode, |
| 125 String expectedReasonPhrase, | 128 String expectedReasonPhrase, |
| 126 [int expectedContentLength = -1, | 129 [int expectedContentLength = -1, |
| 127 int expectedBytesReceived = 0, | 130 int expectedBytesReceived = 0, |
| 128 Map expectedHeaders = null, | 131 Map expectedHeaders = null, |
| 129 bool chunked = false, | 132 bool chunked = false, |
| 130 bool close = false, | 133 bool close = false, |
| 131 String responseToMethod = null]) { | 134 String responseToMethod = null, |
| 135 bool connectionClose = false]) { |
| 132 _HttpParser httpParser; | 136 _HttpParser httpParser; |
| 133 bool headersCompleteCalled; | 137 bool headersCompleteCalled; |
| 134 bool dataEndCalled; | 138 bool dataEndCalled; |
| 139 bool dataEndClose; |
| 135 int statusCode; | 140 int statusCode; |
| 136 String reasonPhrase; | 141 String reasonPhrase; |
| 137 Map headers; | 142 Map headers; |
| 138 int contentLength; | 143 int contentLength; |
| 139 int bytesReceived; | 144 int bytesReceived; |
| 140 | 145 |
| 141 void reset() { | 146 void reset() { |
| 142 httpParser = new _HttpParser(); | 147 httpParser = new _HttpParser(); |
| 143 if (responseToMethod != null) { | 148 if (responseToMethod != null) { |
| 144 httpParser.responseToMethod = responseToMethod; | 149 httpParser.responseToMethod = responseToMethod; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 163 expectedHeaders.forEach((String name, String value) { | 168 expectedHeaders.forEach((String name, String value) { |
| 164 Expect.equals(value, headers[name]); | 169 Expect.equals(value, headers[name]); |
| 165 }); | 170 }); |
| 166 } | 171 } |
| 167 headersCompleteCalled = true; | 172 headersCompleteCalled = true; |
| 168 }; | 173 }; |
| 169 httpParser.dataReceived = (List<int> data) { | 174 httpParser.dataReceived = (List<int> data) { |
| 170 Expect.isTrue(headersCompleteCalled); | 175 Expect.isTrue(headersCompleteCalled); |
| 171 bytesReceived += data.length; | 176 bytesReceived += data.length; |
| 172 }; | 177 }; |
| 173 httpParser.dataEnd = () => dataEndCalled = true; | 178 httpParser.dataEnd = (close) { |
| 179 dataEndCalled = true; |
| 180 dataEndClose = close; |
| 181 }; |
| 174 | 182 |
| 175 headersCompleteCalled = false; | 183 headersCompleteCalled = false; |
| 176 dataEndCalled = false; | 184 dataEndCalled = false; |
| 185 dataEndClose = null; |
| 177 statusCode = -1; | 186 statusCode = -1; |
| 178 reasonPhrase = null; | 187 reasonPhrase = null; |
| 179 headers = new Map(); | 188 headers = new Map(); |
| 180 bytesReceived = 0; | 189 bytesReceived = 0; |
| 181 } | 190 } |
| 182 | 191 |
| 183 void testWrite(List<int> requestData, [int chunkSize = -1]) { | 192 void testWrite(List<int> requestData, [int chunkSize = -1]) { |
| 184 if (chunkSize == -1) chunkSize = requestData.length; | 193 if (chunkSize == -1) chunkSize = requestData.length; |
| 185 reset(); | 194 reset(); |
| 186 for (int pos = 0; pos < requestData.length; pos += chunkSize) { | 195 for (int pos = 0; pos < requestData.length; pos += chunkSize) { |
| 187 int remaining = requestData.length - pos; | 196 int remaining = requestData.length - pos; |
| 188 int writeLength = Math.min(chunkSize, remaining); | 197 int writeLength = Math.min(chunkSize, remaining); |
| 189 httpParser.writeList(requestData, pos, writeLength); | 198 httpParser.writeList(requestData, pos, writeLength); |
| 190 } | 199 } |
| 191 if (close) httpParser.connectionClosed(); | 200 if (close) httpParser.connectionClosed(); |
| 192 Expect.equals(expectedStatusCode, statusCode); | 201 Expect.equals(expectedStatusCode, statusCode); |
| 193 Expect.equals(expectedReasonPhrase, reasonPhrase); | 202 Expect.equals(expectedReasonPhrase, reasonPhrase); |
| 194 Expect.isTrue(headersCompleteCalled); | 203 Expect.isTrue(headersCompleteCalled); |
| 195 Expect.equals(expectedBytesReceived, bytesReceived); | 204 Expect.equals(expectedBytesReceived, bytesReceived); |
| 196 Expect.isTrue(dataEndCalled); | 205 Expect.isTrue(dataEndCalled); |
| 206 if (close) Expect.isTrue(dataEndClose); |
| 207 Expect.equals(dataEndClose, connectionClose); |
| 197 } | 208 } |
| 198 | 209 |
| 199 // Test parsing the request three times delivering the data in | 210 // Test parsing the request three times delivering the data in |
| 200 // different chunks. | 211 // different chunks. |
| 201 List<int> responseData = response.charCodes(); | 212 List<int> responseData = response.charCodes(); |
| 202 testWrite(responseData); | 213 testWrite(responseData); |
| 203 testWrite(responseData, 10); | 214 testWrite(responseData, 10); |
| 204 testWrite(responseData, 1); | 215 testWrite(responseData, 1); |
| 205 } | 216 } |
| 206 | 217 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 POST /test HTTP/1.1\r | 327 POST /test HTTP/1.1\r |
| 317 Content-Length: 10\r | 328 Content-Length: 10\r |
| 318 \r | 329 \r |
| 319 0123456789"""; | 330 0123456789"""; |
| 320 _testParseRequest(request, | 331 _testParseRequest(request, |
| 321 "POST", | 332 "POST", |
| 322 "/test", | 333 "/test", |
| 323 expectedContentLength: 10, | 334 expectedContentLength: 10, |
| 324 expectedBytesReceived: 10); | 335 expectedBytesReceived: 10); |
| 325 | 336 |
| 337 // Test connection close header. |
| 338 request = """ |
| 339 GET /test HTTP/1.1\r |
| 340 Connection: close\r |
| 341 \r |
| 342 """; |
| 343 _testParseRequest(request, "GET", "/test"); |
| 344 |
| 326 // Test chunked encoding. | 345 // Test chunked encoding. |
| 327 request = """ | 346 request = """ |
| 328 POST /test HTTP/1.1\r | 347 POST /test HTTP/1.1\r |
| 329 Transfer-Encoding: chunked\r | 348 Transfer-Encoding: chunked\r |
| 330 \r | 349 \r |
| 331 5\r | 350 5\r |
| 332 01234\r | 351 01234\r |
| 333 5\r | 352 5\r |
| 334 56789\r | 353 56789\r |
| 335 0\r\n\r\n"""; | 354 0\r\n\r\n"""; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 1f\r | 507 1f\r |
| 489 0123456789012345678901234567890\r | 508 0123456789012345678901234567890\r |
| 490 0\r\n\r\n"""; | 509 0\r\n\r\n"""; |
| 491 _testParseResponse(response, | 510 _testParseResponse(response, |
| 492 200, | 511 200, |
| 493 "OK", | 512 "OK", |
| 494 expectedContentLength: -1, | 513 expectedContentLength: -1, |
| 495 expectedBytesReceived: 57, | 514 expectedBytesReceived: 57, |
| 496 chunked: true); | 515 chunked: true); |
| 497 | 516 |
| 517 // Test connection close header. |
| 518 response = """ |
| 519 HTTP/1.1 200 OK\r |
| 520 Content-Length: 0\r |
| 521 Connection: close\r |
| 522 \r |
| 523 """; |
| 524 _testParseResponse(response, |
| 525 200, |
| 526 "OK", |
| 527 expectedContentLength: 0, |
| 528 connectionClose: true); |
| 529 |
| 498 // Test HTTP response without any transfer length indications | 530 // Test HTTP response without any transfer length indications |
| 499 // where closing the connections indicates end of body. | 531 // where closing the connections indicates end of body. |
| 500 response = """ | 532 response = """ |
| 501 HTTP/1.1 200 OK\r | 533 HTTP/1.1 200 OK\r |
| 502 \r | 534 \r |
| 503 01234567890123456789012345 | 535 01234567890123456789012345 |
| 504 0123456789012345678901234567890 | 536 0123456789012345678901234567890 |
| 505 """; | 537 """; |
| 506 _testParseResponse(response, | 538 _testParseResponse(response, |
| 507 200, | 539 200, |
| 508 "OK", | 540 "OK", |
| 509 expectedContentLength: -1, | 541 expectedContentLength: -1, |
| 510 expectedBytesReceived: 59, | 542 expectedBytesReceived: 59, |
| 511 close: true); | 543 close: true, |
| 544 connectionClose: true); |
| 512 } | 545 } |
| 513 | 546 |
| 514 static void testParseInvalidRequest() { | 547 static void testParseInvalidRequest() { |
| 515 String request; | 548 String request; |
| 516 request = "GET /\r\n\r\n"; | 549 request = "GET /\r\n\r\n"; |
| 517 _testParseInvalidRequest(request); | 550 _testParseInvalidRequest(request); |
| 518 | 551 |
| 519 request = "GET / \r\n\r\n"; | 552 request = "GET / \r\n\r\n"; |
| 520 _testParseInvalidRequest(request); | 553 _testParseInvalidRequest(request); |
| 521 | 554 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 0123456789012345678901234567890\r | 627 0123456789012345678901234567890\r |
| 595 0\r\n\r\n"""; | 628 0\r\n\r\n"""; |
| 596 _testParseInvalidResponse(response); | 629 _testParseInvalidResponse(response); |
| 597 } | 630 } |
| 598 } | 631 } |
| 599 | 632 |
| 600 | 633 |
| 601 void main() { | 634 void main() { |
| 602 HttpParserTest.runAllTests(); | 635 HttpParserTest.runAllTests(); |
| 603 } | 636 } |
| OLD | NEW |