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 | 9 |
| 10 import 'package:crypto/crypto.dart'; |
9 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
| 12 import 'package:shelf/shelf.dart' as shelf; |
10 import 'package:stack_trace/stack_trace.dart'; | 13 import 'package:stack_trace/stack_trace.dart'; |
11 | 14 |
12 import 'backend/operating_system.dart'; | 15 import 'backend/operating_system.dart'; |
| 16 import 'util/path_handler.dart'; |
13 | 17 |
14 /// A typedef for a possibly-asynchronous function. | 18 /// A typedef for a possibly-asynchronous function. |
15 /// | 19 /// |
16 /// The return type should only ever by [Future] or void. | 20 /// The return type should only ever by [Future] or void. |
17 typedef AsyncFunction(); | 21 typedef AsyncFunction(); |
18 | 22 |
19 /// A typedef for a zero-argument callback function. | 23 /// A typedef for a zero-argument callback function. |
20 typedef void Callback(); | 24 typedef void Callback(); |
21 | 25 |
22 /// A regular expression to match the exception prefix that some exceptions' | 26 /// A regular expression to match the exception prefix that some exceptions' |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 subscription.resume(); | 176 subscription.resume(); |
173 } | 177 } |
174 }, onCancel: () { | 178 }, onCancel: () { |
175 for (var subscription in subscriptions) { | 179 for (var subscription in subscriptions) { |
176 subscription.cancel(); | 180 subscription.cancel(); |
177 } | 181 } |
178 }); | 182 }); |
179 | 183 |
180 return controller.stream; | 184 return controller.stream; |
181 } | 185 } |
| 186 |
| 187 /// Returns a random base64 string containing [bytes] bytes of data. |
| 188 /// |
| 189 /// [seed] is passed to [math.Random]; [urlSafe] and [addLineSeparator] are |
| 190 /// passed to [CryptoUtils.bytesToBase64]. |
| 191 String randomBase64(int bytes, {int seed, bool urlSafe: false, |
| 192 bool addLineSeparator: false}) { |
| 193 var random = new math.Random(seed); |
| 194 var data = []; |
| 195 for (var i = 0; i < bytes; i++) { |
| 196 data.add(random.nextInt(256)); |
| 197 } |
| 198 return CryptoUtils.bytesToBase64(data, |
| 199 urlSafe: urlSafe, addLineSeparator: addLineSeparator); |
| 200 } |
| 201 |
| 202 // TODO(nweiz): Remove this and [shelfChange] once Shelf 0.6.0 has been out for |
| 203 // six months or so. |
| 204 /// Returns `request.url` in a cross-version way. |
| 205 /// |
| 206 /// This follows the semantics of Shelf 0.6.x, even when using Shelf 0.5.x: the |
| 207 /// returned URL never starts with "/". |
| 208 Uri shelfUrl(shelf.Request request) { |
| 209 var url = request.url; |
| 210 if (!url.path.startsWith("/")) return url; |
| 211 return url.replace(path: url.path.replaceFirst("/", "")); |
| 212 } |
| 213 |
| 214 /// Like [shelf.Request.change], but cross-version. |
| 215 /// |
| 216 /// This follows the semantics of Shelf 0.6.x, even when using Shelf 0.5.x. |
| 217 shelf.Request shelfChange(shelf.Request typedRequest, {String path}) { |
| 218 // Explicitly make the request dynamic since we're calling methods here that |
| 219 // aren't defined in all support Shelf versions, and we don't want the |
| 220 // analyzer to complain. |
| 221 var request = typedRequest as dynamic; |
| 222 |
| 223 try { |
| 224 return request.change(path: path); |
| 225 } on NoSuchMethodError catch (_) { |
| 226 var newScriptName = p.url.join(request.scriptName, path); |
| 227 if (request.scriptName.isEmpty) newScriptName = "/" + newScriptName; |
| 228 |
| 229 var newUrlPath = p.url.relative(request.url.path.replaceFirst("/", ""), |
| 230 from: path); |
| 231 newUrlPath = newUrlPath == "." ? "" : "/" + newUrlPath; |
| 232 |
| 233 return request.change( |
| 234 scriptName: newScriptName, url: request.url.replace(path: newUrlPath)); |
| 235 } |
| 236 } |
| 237 |
| 238 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. |
| 239 shelf.Middleware nestingMiddleware(String beneath) { |
| 240 return (handler) { |
| 241 var pathHandler = new PathHandler()..add(beneath, handler); |
| 242 return pathHandler.handler; |
| 243 }; |
| 244 } |
OLD | NEW |