| 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 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:math'; | 6 import 'dart:math'; |
| 7 | 7 |
| 8 import "package:test/test.dart"; | 8 import "package:test/test.dart"; |
| 9 import "package:mime/mime.dart"; | 9 import "package:mime/mime.dart"; |
| 10 | 10 |
| 11 void _writeInChunks( | 11 void _writeInChunks( |
| 12 List<int> data, int chunkSize, StreamController<List<int>> controller) { | 12 List<int> data, int chunkSize, StreamController<List<int>> controller) { |
| 13 if (chunkSize == -1) chunkSize = data.length; | 13 if (chunkSize == -1) chunkSize = data.length; |
| 14 | 14 |
| 15 for (int pos = 0; pos < data.length; pos += chunkSize) { | 15 for (int pos = 0; pos < data.length; pos += chunkSize) { |
| 16 int remaining = data.length - pos; | 16 int remaining = data.length - pos; |
| 17 int writeLength = min(chunkSize, remaining); | 17 int writeLength = min(chunkSize, remaining); |
| 18 controller.add(data.sublist(pos, pos + writeLength)); | 18 controller.add(data.sublist(pos, pos + writeLength)); |
| 19 } | 19 } |
| 20 controller.close(); | 20 controller.close(); |
| 21 } | 21 } |
| 22 | 22 |
| 23 enum TestMode { IMMEDIATE_LISTEN, DELAY_LISTEN, PAUSE_RESUME } | 23 enum TestMode { IMMEDIATE_LISTEN, DELAY_LISTEN, PAUSE_RESUME } |
| 24 | 24 |
| 25 void _runParseTest(String message, String boundary, TestMode mode, | 25 void _runParseTest(String message, String boundary, TestMode mode, |
| 26 [List<Map> expectedHeaders, List expectedParts, bool expectError = false]) { | 26 [List<Map> expectedHeaders, List expectedParts, bool expectError = false]) { |
| 27 Future testWrite(List<int> data, [int chunkSize = -1]) { | 27 Future testWrite(List<int> data, [int chunkSize = -1]) { |
| 28 StreamController controller = new StreamController(sync: true); | 28 var controller = new StreamController<List<int>>(sync: true); |
| 29 | 29 |
| 30 var stream = | 30 var stream = |
| 31 controller.stream.transform(new MimeMultipartTransformer(boundary)); | 31 controller.stream.transform(new MimeMultipartTransformer(boundary)); |
| 32 int i = 0; | 32 int i = 0; |
| 33 var completer = new Completer(); | 33 var completer = new Completer(); |
| 34 var futures = []; | 34 var futures = <Future>[]; |
| 35 stream.listen((multipart) { | 35 stream.listen((multipart) { |
| 36 int part = i++; | 36 int part = i++; |
| 37 if (expectedHeaders != null) { | 37 if (expectedHeaders != null) { |
| 38 expect(multipart.headers, equals(expectedHeaders[part])); | 38 expect(multipart.headers, equals(expectedHeaders[part])); |
| 39 } | 39 } |
| 40 switch (mode) { | 40 switch (mode) { |
| 41 case TestMode.IMMEDIATE_LISTEN: | 41 case TestMode.IMMEDIATE_LISTEN: |
| 42 futures.add(multipart.fold([], (buffer, data) => buffer..addAll(data)) | 42 futures.add(multipart.fold([], (buffer, data) => buffer..addAll(data)) |
| 43 .then((data) { | 43 .then((data) { |
| 44 if (expectedParts[part] != null) { | 44 if (expectedParts[part] != null) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 Future.wait(futures).then(completer.complete); | 84 Future.wait(futures).then(completer.complete); |
| 85 }); | 85 }); |
| 86 | 86 |
| 87 _writeInChunks(data, chunkSize, controller); | 87 _writeInChunks(data, chunkSize, controller); |
| 88 | 88 |
| 89 return completer.future; | 89 return completer.future; |
| 90 } | 90 } |
| 91 | 91 |
| 92 Future testFirstPartOnly(List<int> data, [int chunkSize = -1]) { | 92 Future testFirstPartOnly(List<int> data, [int chunkSize = -1]) { |
| 93 var completer = new Completer(); | 93 var completer = new Completer(); |
| 94 var controller = new StreamController(sync: true); | 94 var controller = new StreamController<List<int>>(sync: true); |
| 95 | 95 |
| 96 var stream = | 96 var stream = |
| 97 controller.stream.transform(new MimeMultipartTransformer(boundary)); | 97 controller.stream.transform(new MimeMultipartTransformer(boundary)); |
| 98 | 98 |
| 99 stream.first.then((multipart) { | 99 stream.first.then((multipart) { |
| 100 if (expectedHeaders != null) { | 100 if (expectedHeaders != null) { |
| 101 expect(multipart.headers, equals(expectedHeaders[0])); | 101 expect(multipart.headers, equals(expectedHeaders[0])); |
| 102 } | 102 } |
| 103 return (multipart.fold([], (b, d) => b..addAll(d)).then((data) { | 103 return (multipart.fold([], (b, d) => b..addAll(d)).then((data) { |
| 104 if (expectedParts != null && expectedParts[0] != null) { | 104 if (expectedParts != null && expectedParts[0] != null) { |
| 105 expect(data, equals(expectedParts[0].codeUnits)); | 105 expect(data, equals(expectedParts[0].codeUnits)); |
| 106 } | 106 } |
| 107 })); | 107 })); |
| 108 }).then((_) { | 108 }).then((_) { |
| 109 completer.complete(); | 109 completer.complete(); |
| 110 }); | 110 }); |
| 111 | 111 |
| 112 _writeInChunks(data, chunkSize, controller); | 112 _writeInChunks(data, chunkSize, controller); |
| 113 | 113 |
| 114 return completer.future; | 114 return completer.future; |
| 115 } | 115 } |
| 116 | 116 |
| 117 Future testCompletePartAfterCancel(List<int> data, int parts, | 117 Future testCompletePartAfterCancel(List<int> data, int parts, |
| 118 [int chunkSize = -1]) { | 118 [int chunkSize = -1]) { |
| 119 var completer = new Completer(); | 119 var completer = new Completer(); |
| 120 var controller = new StreamController(sync: true); | 120 var controller = new StreamController<List<int>>(sync: true); |
| 121 var stream = | 121 var stream = |
| 122 controller.stream.transform(new MimeMultipartTransformer(boundary)); | 122 controller.stream.transform(new MimeMultipartTransformer(boundary)); |
| 123 var subscription; | 123 var subscription; |
| 124 int i = 0; | 124 int i = 0; |
| 125 var futures = []; | 125 var futures = <Future>[]; |
| 126 subscription = stream.listen((multipart) { | 126 subscription = stream.listen((multipart) { |
| 127 int partIndex = i; | 127 int partIndex = i; |
| 128 | 128 |
| 129 if (partIndex >= parts) { | 129 if (partIndex >= parts) { |
| 130 throw 'Expected no more parts, but got one.'; | 130 throw 'Expected no more parts, but got one.'; |
| 131 } | 131 } |
| 132 | 132 |
| 133 if (expectedHeaders != null) { | 133 if (expectedHeaders != null) { |
| 134 expect(multipart.headers, equals(expectedHeaders[partIndex])); | 134 expect(multipart.headers, equals(expectedHeaders[partIndex])); |
| 135 } | 135 } |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 \r | 444 \r |
| 445 Body2\r | 445 Body2\r |
| 446 --xxx\r\n"""; | 446 --xxx\r\n"""; |
| 447 _testParse(message, "xxx", null, [null, null], true); | 447 _testParse(message, "xxx", null, [null, null], true); |
| 448 } | 448 } |
| 449 | 449 |
| 450 void main() { | 450 void main() { |
| 451 _testParseValid(); | 451 _testParseValid(); |
| 452 _testParseInvalid(); | 452 _testParseInvalid(); |
| 453 } | 453 } |
| OLD | NEW |