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(); |
30 futures.add(partCompleter.future); | |
Bill Hesse
2013/07/03 11:30:27
Why not
futures.add(multipart.fold( ... ).then((da
Anders Johnsen
2013/07/12 10:45:57
Done.
| |
29 multipart.fold([], (buffer, data) => buffer..addAll(data)) | 31 multipart.fold([], (buffer, data) => buffer..addAll(data)) |
30 .then((data) { | 32 .then((data) { |
31 if (expectedParts[part] != null) { | 33 if (expectedParts[part] != null) { |
32 Expect.listEquals(expectedParts[part].codeUnits, data); | 34 expect(data, equals(expectedParts[part].codeUnits)); |
33 } | 35 } |
34 partPort.close(); | 36 partCompleter.complete(true); |
35 }); | 37 }); |
36 }, onError: (error) { | 38 }, onError: (error) { |
37 if (!expectError) throw error; | 39 if (!expectError) throw error; |
38 }, onDone: () { | 40 }, onDone: () { |
39 if (expectedParts != null) { | 41 if (expectedParts != null) { |
40 Expect.equals(expectedParts.length, i); | 42 expect(i, equals(expectedParts.length)); |
41 } | 43 } |
42 port.close(); | 44 Future.wait(futures) |
45 .then((values) => !values.any((b) => !b)) | |
Bill Hesse
2013/07/03 11:30:27
Is this stray code? All the values are true by co
Anders Johnsen
2013/07/12 10:45:57
Done.
| |
46 .then(completer.complete); | |
43 }); | 47 }); |
44 | 48 |
45 if (chunkSize == -1) chunkSize = data.length; | 49 if (chunkSize == -1) chunkSize = data.length; |
46 | 50 |
47 int written = 0; | 51 int written = 0; |
48 for (int pos = 0; pos < data.length; pos += chunkSize) { | 52 for (int pos = 0; pos < data.length; pos += chunkSize) { |
49 int remaining = data.length - pos; | 53 int remaining = data.length - pos; |
50 int writeLength = min(chunkSize, remaining); | 54 int writeLength = min(chunkSize, remaining); |
51 controller.add(data.sublist(pos, pos + writeLength)); | 55 controller.add(data.sublist(pos, pos + writeLength)); |
52 written += writeLength; | 56 written += writeLength; |
53 } | 57 } |
54 controller.close(); | 58 controller.close(); |
59 | |
60 return completer.future; | |
55 } | 61 } |
56 | 62 |
57 // Test parsing the data three times delivering the data in | 63 // Test parsing the data three times delivering the data in |
58 // different chunks. | 64 // different chunks. |
59 List<int> data = message.codeUnits; | 65 List<int> data = message.codeUnits; |
60 testWrite(data); | 66 expect(Future.wait([ |
61 testWrite(data, 10); | 67 testWrite(data), |
62 testWrite(data, 2); | 68 testWrite(data, 10), |
63 testWrite(data, 1); | 69 testWrite(data, 2), |
70 testWrite(data, 1)]) | |
71 .then((values) => !values.any((b) => !b)), | |
Bill Hesse
2013/07/03 11:30:27
All the values are true anyway.
completion(anythi
Anders Johnsen
2013/07/12 10:45:57
Done.
| |
72 completion(equals(true))); | |
64 } | 73 } |
65 | 74 |
66 void testParseValid() { | 75 void testParseValid() { |
67 String message; | 76 String message; |
68 Map headers; | 77 Map headers; |
69 Map headers1; | 78 Map headers1; |
70 Map headers2; | 79 Map headers2; |
71 Map headers3; | 80 Map headers3; |
72 Map headers4; | 81 Map headers4; |
73 String body1; | 82 String body1; |
74 String body2; | 83 String body2; |
75 String body3; | 84 String body3; |
76 String body4; | 85 String body4; |
77 | 86 |
78 // Sample from Wikipedia. | |
79 message = """ | 87 message = """ |
80 This is a message with multiple parts in MIME format.\r | 88 This is a message with multiple parts in MIME format.\r |
81 --frontier\r | 89 --frontier\r |
82 Content-Type: text/plain\r | 90 Content-Type: text/plain\r |
83 \r | 91 \r |
84 This is the body of the message.\r | 92 This is the body of the message.\r |
85 --frontier\r | 93 --frontier\r |
86 Content-Type: application/octet-stream\r | 94 Content-Type: application/octet-stream\r |
87 Content-Transfer-Encoding: base64\r | 95 Content-Transfer-Encoding: base64\r |
88 \r | 96 \r |
89 PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg | 97 PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg |
90 Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg=\r | 98 Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg=\r |
91 --frontier--\r\n"""; | 99 --frontier--\r\n"""; |
92 headers1 = <String, String>{"content-type": "text/plain"}; | 100 headers1 = <String, String>{"content-type": "text/plain"}; |
93 headers2 = <String, String>{"content-type": "application/octet-stream", | 101 headers2 = <String, String>{"content-type": "application/octet-stream", |
94 "content-transfer-encoding": "base64"}; | 102 "content-transfer-encoding": "base64"}; |
Bill Hesse
2013/07/03 11:30:27
Indentation
Anders Johnsen
2013/07/12 10:45:57
Done.
| |
95 body1 = "This is the body of the message."; | 103 body1 = "This is the body of the message."; |
96 body2 = """ | 104 body2 = """ |
97 PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg | 105 PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg |
98 Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg="""; | 106 Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg="""; |
99 testParse(message, "frontier", [headers1, headers2], [body1, body2]); | 107 testParse(message, "frontier", [headers1, headers2], [body1, body2]); |
100 | 108 |
101 // Sample from HTML 4.01 Specification. | |
102 message = """ | 109 message = """ |
103 \r\n--AaB03x\r | 110 \r\n--AaB03x\r |
104 Content-Disposition: form-data; name=\"submit-name\"\r | 111 Content-Disposition: form-data; name=\"submit-name\"\r |
105 \r | 112 \r |
106 Larry\r | 113 Larry\r |
107 --AaB03x\r | 114 --AaB03x\r |
108 Content-Disposition: form-data; name=\"files\"; filename=\"file1.txt\"\r | 115 Content-Disposition: form-data; name=\"files\"; filename=\"file1.txt\"\r |
109 Content-Type: text/plain\r | 116 Content-Type: text/plain\r |
110 \r | 117 \r |
111 ... contents of file1.txt ...\r | 118 ... contents of file1.txt ...\r |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
309 \r | 316 \r |
310 Body2\r | 317 Body2\r |
311 --xxx\r\n"""; | 318 --xxx\r\n"""; |
312 testParse(message, "xxx", null, [null, null], true); | 319 testParse(message, "xxx", null, [null, null], true); |
313 } | 320 } |
314 | 321 |
315 void main() { | 322 void main() { |
316 testParseValid(); | 323 testParseValid(); |
317 testParseInvalid(); | 324 testParseInvalid(); |
318 } | 325 } |
OLD | NEW |