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 var partCompleter = new Completer(); |
Bill Hesse
2013/07/12 11:43:33
partCompleter is unused.
Anders Johnsen
2013/07/15 08:21:32
Done.
| |
29 multipart.fold([], (buffer, data) => buffer..addAll(data)) | 30 futures.add(multipart.fold([], (buffer, data) => buffer..addAll(data)) |
30 .then((data) { | 31 .then((data) { |
31 if (expectedParts[part] != null) { | 32 if (expectedParts[part] != null) { |
Bill Hesse
2013/07/12 11:43:33
Is this really needed? I see only one test with e
Anders Johnsen
2013/07/15 08:21:32
Testing for 'null' is a way to specify in the test
| |
32 Expect.listEquals(expectedParts[part].codeUnits, data); | 33 expect(data, equals(expectedParts[part].codeUnits)); |
33 } | 34 } |
34 partPort.close(); | 35 })); |
35 }); | |
36 }, onError: (error) { | 36 }, onError: (error) { |
37 if (!expectError) throw error; | 37 if (!expectError) throw error; |
38 }, onDone: () { | 38 }, onDone: () { |
Bill Hesse
2013/07/12 11:43:33
expect(expectError, isFalse);
Anders Johnsen
2013/07/15 08:21:32
No, I still get close (cancelOnError is not set).
| |
39 if (expectedParts != null) { | 39 if (expectedParts != null) { |
40 Expect.equals(expectedParts.length, i); | 40 expect(i, equals(expectedParts.length)); |
41 } | 41 } |
42 port.close(); | 42 Future.wait(futures).then(completer.complete); |
43 }); | 43 }); |
44 | 44 |
45 if (chunkSize == -1) chunkSize = data.length; | 45 if (chunkSize == -1) chunkSize = data.length; |
46 | 46 |
47 int written = 0; | 47 int written = 0; |
48 for (int pos = 0; pos < data.length; pos += chunkSize) { | 48 for (int pos = 0; pos < data.length; pos += chunkSize) { |
49 int remaining = data.length - pos; | 49 int remaining = data.length - pos; |
50 int writeLength = min(chunkSize, remaining); | 50 int writeLength = min(chunkSize, remaining); |
51 controller.add(data.sublist(pos, pos + writeLength)); | 51 controller.add(data.sublist(pos, pos + writeLength)); |
52 written += writeLength; | 52 written += writeLength; |
53 } | 53 } |
54 controller.close(); | 54 controller.close(); |
55 | |
56 return completer.future; | |
55 } | 57 } |
56 | 58 |
57 // Test parsing the data three times delivering the data in | 59 // Test parsing the data three times delivering the data in |
58 // different chunks. | 60 // different chunks. |
59 List<int> data = message.codeUnits; | 61 List<int> data = message.codeUnits; |
60 testWrite(data); | 62 expect(Future.wait([ |
61 testWrite(data, 10); | 63 testWrite(data), |
62 testWrite(data, 2); | 64 testWrite(data, 10), |
63 testWrite(data, 1); | 65 testWrite(data, 2), |
66 testWrite(data, 1)]), | |
67 completion(anything)); | |
Bill Hesse
2013/07/12 11:43:33
completes is the same as completion(anything).
Anders Johnsen
2013/07/15 08:21:32
Done.
| |
64 } | 68 } |
65 | 69 |
66 void testParseValid() { | 70 void testParseValid() { |
67 String message; | 71 String message; |
68 Map headers; | 72 Map headers; |
69 Map headers1; | 73 Map headers1; |
70 Map headers2; | 74 Map headers2; |
71 Map headers3; | 75 Map headers3; |
72 Map headers4; | 76 Map headers4; |
73 String body1; | 77 String body1; |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 \r | 313 \r |
310 Body2\r | 314 Body2\r |
311 --xxx\r\n"""; | 315 --xxx\r\n"""; |
312 testParse(message, "xxx", null, [null, null], true); | 316 testParse(message, "xxx", null, [null, null], true); |
313 } | 317 } |
314 | 318 |
315 void main() { | 319 void main() { |
316 testParseValid(); | 320 testParseValid(); |
317 testParseInvalid(); | 321 testParseInvalid(); |
318 } | 322 } |
OLD | NEW |