| OLD | NEW |
| 1 // Copyright (c) 2011, 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 #library("HttpParserTest.dart"); | 5 #import("dart:io"); |
| 6 #import("../../../../chat/http.dart"); | |
| 7 | |
| 8 | 6 |
| 9 class HttpParserTest { | 7 class HttpParserTest { |
| 10 static void runAllTests() { | 8 static void runAllTests() { |
| 11 testParseRequest(); | 9 testParseRequest(); |
| 12 testParseResponse(); | 10 testParseResponse(); |
| 13 } | 11 } |
| 14 | 12 |
| 15 static void _testParseRequest(String request, | 13 static void _testParseRequest(String request, |
| 16 String expectedMethod, | 14 String expectedMethod, |
| 17 String expectedUri, | 15 String expectedUri, |
| 18 [int expectedContentLength = 0, | 16 [int expectedContentLength = 0, |
| 19 int expectedBytesReceived = 0, | 17 int expectedBytesReceived = 0, |
| 20 Map expectedHeaders = null, | 18 Map expectedHeaders = null, |
| 21 bool chunked = false]) { | 19 bool chunked = false]) { |
| 22 HttpParser httpParser; | 20 HttpParser httpParser; |
| 23 bool headersCompleteCalled; | 21 bool headersCompleteCalled; |
| 24 bool dataEndCalled; | 22 bool dataEndCalled; |
| 25 String method; | 23 String method; |
| 26 String uri; | 24 String uri; |
| 27 Map headers; | 25 Map headers; |
| 28 int contentLength; | 26 int contentLength; |
| 29 int bytesReceived; | 27 int bytesReceived; |
| 30 | 28 |
| 31 void reset() { | 29 void reset() { |
| 32 httpParser = new HttpParser(); | 30 httpParser = new HttpParser(); |
| 33 httpParser.requestStart = (m, u) { method = m; uri = u; }; | 31 httpParser.requestStart = (m, u) { method = m; uri = u; }; |
| 34 httpParser.responseStart = (s, r) { Expect.fail("Expected request"); }; | 32 httpParser.responseStart = (s, r) { Expect.fail("Expected request"); }; |
| 35 httpParser.headerReceived = | 33 httpParser.headerReceived = (f, v) { |
| 36 (f, v) { | 34 Expect.isFalse(headersCompleteCalled); |
| 37 Expect.isFalse(headersCompleteCalled); | 35 headers[f] = v; |
| 38 headers[f] = v; | 36 }; |
| 39 }; | 37 httpParser.headersComplete = () { |
| 40 httpParser.headersComplete = | 38 Expect.isFalse(headersCompleteCalled); |
| 41 () { | 39 if (!chunked) { |
| 42 Expect.isFalse(headersCompleteCalled); | 40 Expect.equals(expectedContentLength, httpParser.contentLength); |
| 43 if (!chunked) { | 41 } else { |
| 44 Expect.equals(expectedContentLength, httpParser.contentLength); | 42 Expect.equals(-1, httpParser.contentLength); |
| 45 } else { | 43 } |
| 46 Expect.equals(-1, httpParser.contentLength); | 44 if (expectedHeaders != null) { |
| 47 } | 45 expectedHeaders.forEach( |
| 48 if (expectedHeaders != null) { | 46 (String name, String value) => |
| 49 expectedHeaders.forEach( | 47 Expect.equals(value, headers[name])); |
| 50 (String name, String value) => | 48 } |
| 51 Expect.equals(value, headers[name])); | 49 headersCompleteCalled = true; |
| 52 } | 50 }; |
| 53 headersCompleteCalled = true; | 51 httpParser.dataReceived = (List<int> data) { |
| 54 }; | 52 Expect.isTrue(headersCompleteCalled); |
| 55 httpParser.dataReceived = | 53 bytesReceived += data.length; |
| 56 (List<int> data) { | 54 }; |
| 57 Expect.isTrue(headersCompleteCalled); | 55 httpParser.dataEnd = () => dataEndCalled = true; |
| 58 bytesReceived += data.length; | |
| 59 }; | |
| 60 httpParser.dataEnd = () { dataEndCalled = true; }; | |
| 61 | 56 |
| 62 headersCompleteCalled = false; | 57 headersCompleteCalled = false; |
| 63 dataEndCalled = false; | 58 dataEndCalled = false; |
| 64 method = null; | 59 method = null; |
| 65 uri = null; | 60 uri = null; |
| 66 headers = new Map(); | 61 headers = new Map(); |
| 67 bytesReceived = 0; | 62 bytesReceived = 0; |
| 68 } | 63 } |
| 69 | 64 |
| 70 void checkExpectations() { | 65 void checkExpectations() { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 bool headersCompleteCalled; | 106 bool headersCompleteCalled; |
| 112 bool dataEndCalled; | 107 bool dataEndCalled; |
| 113 int statusCode; | 108 int statusCode; |
| 114 String reasonPhrase; | 109 String reasonPhrase; |
| 115 Map headers; | 110 Map headers; |
| 116 int contentLength; | 111 int contentLength; |
| 117 int bytesReceived; | 112 int bytesReceived; |
| 118 | 113 |
| 119 void reset() { | 114 void reset() { |
| 120 httpParser = new HttpParser(); | 115 httpParser = new HttpParser(); |
| 121 httpParser.requestStart = (m, u) { Expect.fail("Expected response"); }; | 116 httpParser.requestStart = (m, u) => Expect.fail("Expected response"); |
| 122 httpParser.responseStart = (s, r) { statusCode = s; reasonPhrase = r; }; | 117 httpParser.responseStart = (s, r) { |
| 123 httpParser.headerReceived = | 118 statusCode = s; |
| 124 (f, v) { | 119 reasonPhrase = r; |
| 125 Expect.isFalse(headersCompleteCalled); | 120 }; |
| 126 headers[f] = v; | 121 httpParser.headerReceived = (f, v) { |
| 127 }; | 122 Expect.isFalse(headersCompleteCalled); |
| 128 httpParser.headersComplete = | 123 headers[f] = v; |
| 129 () { | 124 }; |
| 130 Expect.isFalse(headersCompleteCalled); | 125 httpParser.headersComplete = () { |
| 131 if (!chunked) { | 126 Expect.isFalse(headersCompleteCalled); |
| 132 Expect.equals(expectedContentLength, httpParser.contentLength); | 127 if (!chunked) { |
| 133 } else { | 128 Expect.equals(expectedContentLength, httpParser.contentLength); |
| 134 Expect.equals(-1, httpParser.contentLength); | 129 } else { |
| 135 } | 130 Expect.equals(-1, httpParser.contentLength); |
| 136 if (expectedHeaders != null) { | 131 } |
| 137 expectedHeaders.forEach( | 132 if (expectedHeaders != null) { |
| 138 (String name, String value) => | 133 expectedHeaders.forEach((String name, String value) { |
| 139 Expect.equals(value, headers[name])); | 134 Expect.equals(value, headers[name]); |
| 140 } | 135 }); |
| 141 headersCompleteCalled = true; | 136 } |
| 142 }; | 137 headersCompleteCalled = true; |
| 143 httpParser.dataReceived = | 138 }; |
| 144 (List<int> data) { | 139 httpParser.dataReceived = (List<int> data) { |
| 145 Expect.isTrue(headersCompleteCalled); | 140 Expect.isTrue(headersCompleteCalled); |
| 146 bytesReceived += data.length; | 141 bytesReceived += data.length; |
| 147 }; | 142 }; |
| 148 httpParser.dataEnd = () { dataEndCalled = true; }; | 143 httpParser.dataEnd = () => dataEndCalled = true; |
| 149 | 144 |
| 150 headersCompleteCalled = false; | 145 headersCompleteCalled = false; |
| 151 dataEndCalled = false; | 146 dataEndCalled = false; |
| 152 statusCode = -1; | 147 statusCode = -1; |
| 153 reasonPhrase = null; | 148 reasonPhrase = null; |
| 154 headers = new Map(); | 149 headers = new Map(); |
| 155 bytesReceived = 0; | 150 bytesReceived = 0; |
| 156 } | 151 } |
| 157 | 152 |
| 158 void checkExpectations() { | 153 void checkExpectations() { |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 0123456789012345678901234567890\r | 352 0123456789012345678901234567890\r |
| 358 0\r\n\r\n"""; | 353 0\r\n\r\n"""; |
| 359 _testParseResponse(response, 200, "OK", -1, 57, null, true); | 354 _testParseResponse(response, 200, "OK", -1, 57, null, true); |
| 360 } | 355 } |
| 361 } | 356 } |
| 362 | 357 |
| 363 | 358 |
| 364 void main() { | 359 void main() { |
| 365 HttpParserTest.runAllTests(); | 360 HttpParserTest.runAllTests(); |
| 366 } | 361 } |
| OLD | NEW |