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:convert'; | 8 import 'dart:convert'; |
9 import 'dart:math' as math; | 9 import 'dart:math' as math; |
10 | 10 |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 bool addLineSeparator: false}) { | 389 bool addLineSeparator: false}) { |
390 var random = new math.Random(seed); | 390 var random = new math.Random(seed); |
391 var data = []; | 391 var data = []; |
392 for (var i = 0; i < bytes; i++) { | 392 for (var i = 0; i < bytes; i++) { |
393 data.add(random.nextInt(256)); | 393 data.add(random.nextInt(256)); |
394 } | 394 } |
395 return CryptoUtils.bytesToBase64(data, | 395 return CryptoUtils.bytesToBase64(data, |
396 urlSafe: urlSafe, addLineSeparator: addLineSeparator); | 396 urlSafe: urlSafe, addLineSeparator: addLineSeparator); |
397 } | 397 } |
398 | 398 |
399 // TODO(nweiz): Remove this and [shelfChange] once Shelf 0.6.0 has been out for | |
400 // six months or so. | |
401 /// Returns `request.url` in a cross-version way. | |
402 /// | |
403 /// This follows the semantics of Shelf 0.6.x, even when using Shelf 0.5.x: the | |
404 /// returned URL never starts with "/". | |
405 Uri shelfUrl(shelf.Request request) { | |
406 var url = request.url; | |
407 if (!url.path.startsWith("/")) return url; | |
408 return url.replace(path: url.path.replaceFirst("/", "")); | |
409 } | |
410 | |
411 /// Like [shelf.Request.change], but cross-version. | |
412 /// | |
413 /// This follows the semantics of Shelf 0.6.x, even when using Shelf 0.5.x. | |
414 shelf.Request shelfChange(shelf.Request typedRequest, {String path}) { | |
415 // Explicitly make the request dynamic since we're calling methods here that | |
416 // aren't defined in all support Shelf versions, and we don't want the | |
417 // analyzer to complain. | |
418 var request = typedRequest as dynamic; | |
419 | |
420 try { | |
421 return request.change(path: path); | |
422 } on NoSuchMethodError catch (_) { | |
423 var newScriptName = p.url.join(request.scriptName, path); | |
424 if (request.scriptName.isEmpty) newScriptName = "/" + newScriptName; | |
425 | |
426 var newUrlPath = p.url.relative(request.url.path.replaceFirst("/", ""), | |
427 from: path); | |
428 newUrlPath = newUrlPath == "." ? "" : "/" + newUrlPath; | |
429 | |
430 return request.change( | |
431 scriptName: newScriptName, url: request.url.replace(path: newUrlPath)); | |
432 } | |
433 } | |
434 | |
435 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. | 399 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. |
436 shelf.Middleware nestingMiddleware(String beneath) { | 400 shelf.Middleware nestingMiddleware(String beneath) { |
437 return (handler) { | 401 return (handler) { |
438 var pathHandler = new PathHandler()..add(beneath, handler); | 402 var pathHandler = new PathHandler()..add(beneath, handler); |
439 return pathHandler.handler; | 403 return pathHandler.handler; |
440 }; | 404 }; |
441 } | 405 } |
OLD | NEW |