OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library shelf_static.test_util; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:path/path.dart' as p; |
| 10 import 'package:shelf/shelf.dart'; |
| 11 import 'package:shelf_static/src/util.dart'; |
| 12 import 'package:test/test.dart'; |
| 13 |
| 14 final p.Context _ctx = p.url; |
| 15 |
| 16 /// Makes a simple GET request to [handler] and returns the result. |
| 17 Future<Response> makeRequest(Handler handler, String path, |
| 18 {String handlerPath, Map<String, String> headers}) { |
| 19 var rootedHandler = _rootHandler(handlerPath, handler); |
| 20 return new Future.sync(() => rootedHandler(_fromPath(path, headers))); |
| 21 } |
| 22 |
| 23 Request _fromPath(String path, Map<String, String> headers) => |
| 24 new Request('GET', Uri.parse('http://localhost' + path), headers: headers); |
| 25 |
| 26 Handler _rootHandler(String path, Handler handler) { |
| 27 if (path == null || path.isEmpty) { |
| 28 return handler; |
| 29 } |
| 30 |
| 31 return (Request request) { |
| 32 if (!_ctx.isWithin("/$path", request.requestedUri.path)) { |
| 33 return new Response.notFound('not found'); |
| 34 } |
| 35 assert(request.handlerPath == '/'); |
| 36 |
| 37 var relativeRequest = request.change(path: path); |
| 38 |
| 39 return handler(relativeRequest); |
| 40 }; |
| 41 } |
| 42 |
| 43 Matcher atSameTimeToSecond(value) => |
| 44 new _SecondResolutionDateTimeMatcher(value); |
| 45 |
| 46 class _SecondResolutionDateTimeMatcher extends Matcher { |
| 47 final DateTime _target; |
| 48 |
| 49 _SecondResolutionDateTimeMatcher(DateTime target) |
| 50 : this._target = toSecondResolution(target); |
| 51 |
| 52 bool matches(item, Map matchState) { |
| 53 if (item is! DateTime) return false; |
| 54 |
| 55 return datesEqualToSecond(_target, item); |
| 56 } |
| 57 |
| 58 Description describe(Description descirption) => descirption.add( |
| 59 'Must be at the same moment as $_target with resolution ' |
| 60 'to the second.'); |
| 61 } |
| 62 |
| 63 bool datesEqualToSecond(DateTime d1, DateTime d2) { |
| 64 return toSecondResolution(d1).isAtSameMomentAs(toSecondResolution(d2)); |
| 65 } |
OLD | NEW |