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