Chromium Code Reviews| 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 StreamController<List<int>> controller = new StreamController(sync: true); |
|
kevmoo
2016/12/09 02:30:26
make the left side var and add a generic to the ri
keertip
2016/12/09 02:39:14
Done.
| |
| 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 29 matching lines...) Expand all Loading... | |
| 74 completer.complete(); | 74 completer.complete(); |
| 75 }); | 75 }); |
| 76 break; | 76 break; |
| 77 } | 77 } |
| 78 }, onError: (error) { | 78 }, onError: (error) { |
| 79 if (!expectError) throw error; | 79 if (!expectError) throw error; |
| 80 }, onDone: () { | 80 }, onDone: () { |
| 81 if (expectedParts != null) { | 81 if (expectedParts != null) { |
| 82 expect(i, equals(expectedParts.length)); | 82 expect(i, equals(expectedParts.length)); |
| 83 } | 83 } |
| 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 StreamController<List<int>> controller = new StreamController(sync: true); |
|
kevmoo
2016/12/09 02:30:26
ditto
keertip
2016/12/09 02:39:14
Done.
| |
| 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 StreamController<List<int>> controller = new StreamController(sync: true); |
|
kevmoo
2016/12/09 02:30:26
ditto
keertip
2016/12/09 02:39:14
Done.
| |
| 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 } |
| 136 futures.add((multipart.fold([], (b, d) => b..addAll(d)).then((data) { | 136 futures.add((multipart.fold([], (b, d) => b..addAll(d)).then((data) { |
| 137 if (expectedParts != null && expectedParts[partIndex] != null) { | 137 if (expectedParts != null && expectedParts[partIndex] != null) { |
| 138 expect(data, equals(expectedParts[partIndex].codeUnits)); | 138 expect(data, equals(expectedParts[partIndex].codeUnits)); |
| 139 } | 139 } |
| 140 }))); | 140 }))); |
| 141 | 141 |
| 142 if (partIndex == (parts - 1)) { | 142 if (partIndex == (parts - 1)) { |
| 143 subscription.cancel(); | 143 subscription.cancel(); |
| 144 Future.wait(futures).then(completer.complete); | 144 Future.wait(futures).then((_) => completer.complete); |
|
kevmoo
2016/12/09 02:30:26
If you put a generic type on the completer does th
keertip
2016/12/09 02:39:14
That doesn't work. The original error is ERROR: Th
kevmoo
2016/12/09 02:53:32
So typing the completer as `var completer = new Co
| |
| 145 } | 145 } |
| 146 i++; | 146 i++; |
| 147 }); | 147 }); |
| 148 | 148 |
| 149 _writeInChunks(data, chunkSize, controller); | 149 _writeInChunks(data, chunkSize, controller); |
| 150 | 150 |
| 151 return completer.future; | 151 return completer.future; |
| 152 } | 152 } |
| 153 | 153 |
| 154 // Test parsing the data three times delivering the data in | 154 // Test parsing the data three times delivering the data in |
| (...skipping 289 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 |