| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import 'dart:math'; | 7 import 'dart:math'; |
| 8 | 8 import 'dart:io'; |
| 9 part '../../../sdk/lib/io/io_sink.dart'; | 9 import 'dart:isolate'; |
| 10 part "../../../sdk/lib/io/http.dart"; | |
| 11 part "../../../sdk/lib/io/http_impl.dart"; | |
| 12 part "../../../sdk/lib/io/http_parser.dart"; | |
| 13 part "../../../sdk/lib/io/mime_multipart_parser.dart"; | |
| 14 part '../../../sdk/lib/io/socket.dart'; | |
| 15 | 10 |
| 16 void testParse(String message, | 11 void testParse(String message, |
| 17 String boundary, | 12 String boundary, |
| 18 [List<Map> expectedHeaders, | 13 [List<Map> expectedHeaders, |
| 19 List expectedParts]) { | 14 List expectedParts, |
| 20 _MimeMultipartParser parser; | 15 bool expectError = false]) { |
| 21 int partCount; | 16 void testWrite(List<int> data, [int chunkSize = -1]) { |
| 22 Map headers; | 17 StreamController controller = new StreamController(); |
| 23 List<int> currentPart; | |
| 24 bool lastPartCalled; | |
| 25 | 18 |
| 26 void reset() { | 19 var stream = controller.stream.transform( |
| 27 parser = new _MimeMultipartParser(boundary); | 20 new MimeMultipartTransformer(boundary)); |
| 28 parser.partStart = (f, v) { | 21 int i = 0; |
| 29 }; | 22 var port = new ReceivePort(); |
| 30 parser.headerReceived = (f, v) { | 23 stream.listen((multipart) { |
| 31 headers[f] = v; | 24 int part = i++; |
| 32 }; | |
| 33 parser.headersComplete = () { | |
| 34 if (expectedHeaders != null) { | 25 if (expectedHeaders != null) { |
| 35 expectedHeaders[partCount].forEach( | 26 Expect.mapEquals(expectedHeaders[part], multipart.headers); |
| 36 (String name, String value) { | |
| 37 Expect.equals(value, headers[name]); | |
| 38 }); | |
| 39 } | 27 } |
| 40 }; | 28 var partPort = new ReceivePort(); |
| 41 parser.partDataReceived = (List<int> data) { | 29 multipart.fold([], (buffer, data) => buffer..addAll(data)) |
| 42 if (currentPart == null) currentPart = new List<int>(); | 30 .then((data) { |
| 43 currentPart.addAll(data); | 31 if (expectedParts[part] != null) { |
| 44 }; | 32 Expect.listEquals(expectedParts[part].codeUnits, data); |
| 45 parser.partEnd = (lastPart) { | 33 } |
| 46 Expect.isFalse(lastPartCalled); | 34 partPort.close(); |
| 47 lastPartCalled = lastPart; | 35 }); |
| 48 if (expectedParts[partCount] != null) { | 36 }, onError: (error) { |
| 49 List<int> expectedPart; | 37 if (!expectError) throw error; |
| 50 if (expectedParts[partCount] is String) { | 38 }, onDone: () { |
| 51 expectedPart = expectedParts[partCount].codeUnits; | 39 if (expectedParts != null) { |
| 52 } else { | 40 Expect.equals(expectedParts.length, i); |
| 53 expectedPart = expectedParts[partCount]; | |
| 54 } | |
| 55 Expect.listEquals(expectedPart, currentPart); | |
| 56 } | 41 } |
| 57 currentPart = null; | 42 port.close(); |
| 58 partCount++; | 43 }); |
| 59 if (lastPart) Expect.equals(expectedParts.length, partCount); | |
| 60 }; | |
| 61 | 44 |
| 62 partCount = 0; | 45 if (chunkSize == -1) chunkSize = data.length; |
| 63 headers = new Map(); | |
| 64 currentPart = null; | |
| 65 lastPartCalled = false; | |
| 66 } | |
| 67 | 46 |
| 68 void testWrite(List<int> data, [int chunkSize = -1]) { | |
| 69 if (chunkSize == -1) chunkSize = data.length; | |
| 70 reset(); | |
| 71 int written = 0; | 47 int written = 0; |
| 72 for (int pos = 0; pos < data.length; pos += chunkSize) { | 48 for (int pos = 0; pos < data.length; pos += chunkSize) { |
| 73 int remaining = data.length - pos; | 49 int remaining = data.length - pos; |
| 74 int writeLength = min(chunkSize, remaining); | 50 int writeLength = min(chunkSize, remaining); |
| 75 int parsed = | 51 controller.add(data.sublist(pos, pos + writeLength)); |
| 76 parser.update(data.sublist(pos, pos + writeLength), 0, writeLength); | |
| 77 Expect.equals(writeLength, parsed); | |
| 78 written += writeLength; | 52 written += writeLength; |
| 79 } | 53 } |
| 80 Expect.isTrue(lastPartCalled); | 54 controller.close(); |
| 81 } | 55 } |
| 82 | 56 |
| 83 // Test parsing the data three times delivering the data in | 57 // Test parsing the data three times delivering the data in |
| 84 // different chunks. | 58 // different chunks. |
| 85 List<int> data = message.codeUnits; | 59 List<int> data = message.codeUnits; |
| 86 testWrite(data); | 60 testWrite(data); |
| 87 testWrite(data, 10); | 61 testWrite(data, 10); |
| 88 testWrite(data, 2); | 62 testWrite(data, 2); |
| 89 testWrite(data, 1); | 63 testWrite(data, 1); |
| 90 } | 64 } |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 message = """ | 288 message = """ |
| 315 --xxx\r | 289 --xxx\r |
| 316 \r | 290 \r |
| 317 \r | 291 \r |
| 318 Body 1\r | 292 Body 1\r |
| 319 --xxx\r | 293 --xxx\r |
| 320 \r | 294 \r |
| 321 \r | 295 \r |
| 322 Body2\r | 296 Body2\r |
| 323 --xxx--\r\n"""; | 297 --xxx--\r\n"""; |
| 324 Expect.throws(() => testParse(message, "xxx", null, [null, null])); | 298 testParse(message, "xxx", null, ["\r\nBody2"]); |
| 325 | 299 |
| 326 // Missing end boundary. | 300 // Missing end boundary. |
| 327 message = """ | 301 message = """ |
| 328 \r | 302 \r |
| 329 --xxx\r | 303 --xxx\r |
| 330 \r | 304 \r |
| 331 \r | 305 \r |
| 332 Body 1\r | 306 Body 1\r |
| 333 --xxx\r | 307 --xxx\r |
| 334 \r | 308 \r |
| 335 \r | 309 \r |
| 336 Body2\r | 310 Body2\r |
| 337 --xxx\r\n"""; | 311 --xxx\r\n"""; |
| 338 Expect.throws(() => testParse(message, "xxx", null, [null, null])); | 312 testParse(message, "xxx", null, [null, null], true); |
| 339 } | 313 } |
| 340 | 314 |
| 341 void main() { | 315 void main() { |
| 342 testParseValid(); | 316 testParseValid(); |
| 343 testParseInvalid(); | 317 testParseInvalid(); |
| 344 } | 318 } |
| OLD | NEW |