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:unittest/unittest.dart"; |
| 6 import "package:mime/mime.dart"; |
6 import 'dart:async'; | 7 import 'dart:async'; |
7 import 'dart:math'; | 8 import 'dart:math'; |
8 import 'dart:io'; | |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 | 10 |
11 void testParse(String message, | 11 void testParse(String message, |
12 String boundary, | 12 String boundary, |
13 [List<Map> expectedHeaders, | 13 [List<Map> expectedHeaders, |
14 List expectedParts, | 14 List expectedParts, |
15 bool expectError = false]) { | 15 bool expectError = false]) { |
16 void testWrite(List<int> data, [int chunkSize = -1]) { | 16 Future testWrite(List<int> data, [int chunkSize = -1]) { |
17 StreamController controller = new StreamController(sync: true); | 17 StreamController controller = new StreamController(sync: true); |
18 | 18 |
19 var stream = controller.stream.transform( | 19 var stream = controller.stream.transform( |
20 new MimeMultipartTransformer(boundary)); | 20 new MimeMultipartTransformer(boundary)); |
21 int i = 0; | 21 int i = 0; |
22 var port = new ReceivePort(); | 22 var completer = new Completer(); |
| 23 var futures = []; |
23 stream.listen((multipart) { | 24 stream.listen((multipart) { |
24 int part = i++; | 25 int part = i++; |
25 if (expectedHeaders != null) { | 26 if (expectedHeaders != null) { |
26 Expect.mapEquals(expectedHeaders[part], multipart.headers); | 27 expect(multipart.headers, equals(expectedHeaders[part])); |
27 } | 28 } |
28 var partPort = new ReceivePort(); | 29 futures.add(multipart.fold([], (buffer, data) => buffer..addAll(data)) |
29 multipart.fold([], (buffer, data) => buffer..addAll(data)) | |
30 .then((data) { | 30 .then((data) { |
31 if (expectedParts[part] != null) { | 31 if (expectedParts[part] != null) { |
32 Expect.listEquals(expectedParts[part].codeUnits, data); | 32 expect(data, equals(expectedParts[part].codeUnits)); |
33 } | 33 } |
34 partPort.close(); | 34 })); |
35 }); | |
36 }, onError: (error) { | 35 }, onError: (error) { |
37 if (!expectError) throw error; | 36 if (!expectError) throw error; |
38 }, onDone: () { | 37 }, onDone: () { |
39 if (expectedParts != null) { | 38 if (expectedParts != null) { |
40 Expect.equals(expectedParts.length, i); | 39 expect(i, equals(expectedParts.length)); |
41 } | 40 } |
42 port.close(); | 41 Future.wait(futures).then(completer.complete); |
43 }); | 42 }); |
44 | 43 |
45 if (chunkSize == -1) chunkSize = data.length; | 44 if (chunkSize == -1) chunkSize = data.length; |
46 | 45 |
47 int written = 0; | 46 int written = 0; |
48 for (int pos = 0; pos < data.length; pos += chunkSize) { | 47 for (int pos = 0; pos < data.length; pos += chunkSize) { |
49 int remaining = data.length - pos; | 48 int remaining = data.length - pos; |
50 int writeLength = min(chunkSize, remaining); | 49 int writeLength = min(chunkSize, remaining); |
51 controller.add(data.sublist(pos, pos + writeLength)); | 50 controller.add(data.sublist(pos, pos + writeLength)); |
52 written += writeLength; | 51 written += writeLength; |
53 } | 52 } |
54 controller.close(); | 53 controller.close(); |
| 54 |
| 55 return completer.future; |
55 } | 56 } |
56 | 57 |
57 // Test parsing the data three times delivering the data in | 58 // Test parsing the data three times delivering the data in |
58 // different chunks. | 59 // different chunks. |
59 List<int> data = message.codeUnits; | 60 List<int> data = message.codeUnits; |
60 testWrite(data); | 61 expect(Future.wait([ |
61 testWrite(data, 10); | 62 testWrite(data), |
62 testWrite(data, 2); | 63 testWrite(data, 10), |
63 testWrite(data, 1); | 64 testWrite(data, 2), |
| 65 testWrite(data, 1)]), |
| 66 completes); |
64 } | 67 } |
65 | 68 |
66 void testParseValid() { | 69 void testParseValid() { |
67 String message; | 70 String message; |
68 Map headers; | 71 Map headers; |
69 Map headers1; | 72 Map headers1; |
70 Map headers2; | 73 Map headers2; |
71 Map headers3; | 74 Map headers3; |
72 Map headers4; | 75 Map headers4; |
73 String body1; | 76 String body1; |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 \r | 312 \r |
310 Body2\r | 313 Body2\r |
311 --xxx\r\n"""; | 314 --xxx\r\n"""; |
312 testParse(message, "xxx", null, [null, null], true); | 315 testParse(message, "xxx", null, [null, null], true); |
313 } | 316 } |
314 | 317 |
315 void main() { | 318 void main() { |
316 testParseValid(); | 319 testParseValid(); |
317 testParseInvalid(); | 320 testParseInvalid(); |
318 } | 321 } |
OLD | NEW |