| 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 library test.utils; | 5 library test.utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
| 9 | 9 |
| 10 import 'package:crypto/crypto.dart'; | 10 import 'package:crypto/crypto.dart'; |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 } | 177 } |
| 178 }, onCancel: () { | 178 }, onCancel: () { |
| 179 for (var subscription in subscriptions) { | 179 for (var subscription in subscriptions) { |
| 180 subscription.cancel(); | 180 subscription.cancel(); |
| 181 } | 181 } |
| 182 }); | 182 }); |
| 183 | 183 |
| 184 return controller.stream; | 184 return controller.stream; |
| 185 } | 185 } |
| 186 | 186 |
| 187 /// Returns the first value [stream] emits, or `null` if [stream] closes before |
| 188 /// emitting a value. |
| 189 Future maybeFirst(Stream stream) { |
| 190 var completer = new Completer(); |
| 191 |
| 192 var subscription; |
| 193 subscription = stream.listen((data) { |
| 194 completer.complete(data); |
| 195 subscription.cancel(); |
| 196 }, onError: (error, stackTrace) { |
| 197 completer.completeError(error, stackTrace); |
| 198 subscription.cancel(); |
| 199 }, onDone: () { |
| 200 completer.complete(); |
| 201 }); |
| 202 |
| 203 return completer.future; |
| 204 } |
| 205 |
| 187 /// Returns a random base64 string containing [bytes] bytes of data. | 206 /// Returns a random base64 string containing [bytes] bytes of data. |
| 188 /// | 207 /// |
| 189 /// [seed] is passed to [math.Random]; [urlSafe] and [addLineSeparator] are | 208 /// [seed] is passed to [math.Random]; [urlSafe] and [addLineSeparator] are |
| 190 /// passed to [CryptoUtils.bytesToBase64]. | 209 /// passed to [CryptoUtils.bytesToBase64]. |
| 191 String randomBase64(int bytes, {int seed, bool urlSafe: false, | 210 String randomBase64(int bytes, {int seed, bool urlSafe: false, |
| 192 bool addLineSeparator: false}) { | 211 bool addLineSeparator: false}) { |
| 193 var random = new math.Random(seed); | 212 var random = new math.Random(seed); |
| 194 var data = []; | 213 var data = []; |
| 195 for (var i = 0; i < bytes; i++) { | 214 for (var i = 0; i < bytes; i++) { |
| 196 data.add(random.nextInt(256)); | 215 data.add(random.nextInt(256)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 } | 254 } |
| 236 } | 255 } |
| 237 | 256 |
| 238 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. | 257 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. |
| 239 shelf.Middleware nestingMiddleware(String beneath) { | 258 shelf.Middleware nestingMiddleware(String beneath) { |
| 240 return (handler) { | 259 return (handler) { |
| 241 var pathHandler = new PathHandler()..add(beneath, handler); | 260 var pathHandler = new PathHandler()..add(beneath, handler); |
| 242 return pathHandler.handler; | 261 return pathHandler.handler; |
| 243 }; | 262 }; |
| 244 } | 263 } |
| OLD | NEW |