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 import 'dart:async'; |
| 6 |
| 7 import 'package:shelf/shelf.dart' as shelf; |
| 8 import 'package:test/src/util/path_handler.dart'; |
| 9 import 'package:test/test.dart'; |
| 10 |
| 11 void main() { |
| 12 var handler; |
| 13 setUp(() => handler = new PathHandler()); |
| 14 |
| 15 _handle(request) => new Future.sync(() => handler.handler(request)); |
| 16 |
| 17 test("returns a 404 for a root URL", () { |
| 18 var request = new shelf.Request("GET", Uri.parse("http://localhost/")); |
| 19 return _handle(request).then((response) { |
| 20 expect(response.statusCode, equals(404)); |
| 21 }); |
| 22 }); |
| 23 |
| 24 test("returns a 404 for an unregistered URL", () { |
| 25 var request = new shelf.Request("GET", Uri.parse("http://localhost/foo")); |
| 26 return _handle(request).then((response) { |
| 27 expect(response.statusCode, equals(404)); |
| 28 }); |
| 29 }); |
| 30 |
| 31 test("runs a handler for an exact URL", () { |
| 32 var request = new shelf.Request("GET", Uri.parse("http://localhost/foo")); |
| 33 handler.add("foo", expectAsync((request) { |
| 34 expect(request.handlerPath, equals('/foo')); |
| 35 expect(request.url.path, isEmpty); |
| 36 return new shelf.Response.ok("good job!"); |
| 37 })); |
| 38 |
| 39 return _handle(request).then((response) { |
| 40 expect(response.statusCode, equals(200)); |
| 41 expect(response.readAsString(), completion(equals("good job!"))); |
| 42 }); |
| 43 }); |
| 44 |
| 45 test("runs a handler for a suffix", () { |
| 46 var request = new shelf.Request( |
| 47 "GET", Uri.parse("http://localhost/foo/bar")); |
| 48 handler.add("foo", expectAsync((request) { |
| 49 expect(request.handlerPath, equals('/foo/')); |
| 50 expect(request.url.path, 'bar'); |
| 51 return new shelf.Response.ok("good job!"); |
| 52 })); |
| 53 |
| 54 return _handle(request).then((response) { |
| 55 expect(response.statusCode, equals(200)); |
| 56 expect(response.readAsString(), completion(equals("good job!"))); |
| 57 }); |
| 58 }); |
| 59 |
| 60 test("runs the longest matching handler", () { |
| 61 var request = new shelf.Request( |
| 62 "GET", Uri.parse("http://localhost/foo/bar/baz")); |
| 63 |
| 64 handler.add("foo", expectAsync((_) {}, count: 0)); |
| 65 handler.add("foo/bar", expectAsync((request) { |
| 66 expect(request.handlerPath, equals('/foo/bar/')); |
| 67 expect(request.url.path, 'baz'); |
| 68 return new shelf.Response.ok("good job!"); |
| 69 })); |
| 70 handler.add("foo/bar/baz/bang", expectAsync((_) {}, count: 0)); |
| 71 |
| 72 return _handle(request).then((response) { |
| 73 expect(response.statusCode, equals(200)); |
| 74 expect(response.readAsString(), completion(equals("good job!"))); |
| 75 }); |
| 76 }); |
| 77 } |
OLD | NEW |