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:unittest/src/util/one_off_handler.dart'; | |
9 import 'package:unittest/unittest.dart'; | |
10 | |
11 void main() { | |
12 var handler; | |
13 setUp(() => handler = new OneOffHandler()); | |
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 unhandled URL", () { | |
25 var request = new shelf.Request("GET", Uri.parse("http://localhost/1")); | |
26 return _handle(request).then((response) { | |
27 expect(response.statusCode, equals(404)); | |
28 }); | |
29 }); | |
30 | |
31 test("passes a request to a handler only once", () { | |
32 var path = handler.create(expectAsync((request) { | |
33 expect(request.method, equals("GET")); | |
34 return new shelf.Response.ok("good job!"); | |
35 })); | |
36 | |
37 var request = new shelf.Request("GET", Uri.parse("http://localhost/$path")); | |
38 return _handle(request).then((response) { | |
39 expect(response.statusCode, equals(200)); | |
40 expect(response.readAsString(), completion(equals("good job!"))); | |
41 | |
42 request = new shelf.Request("GET", Uri.parse("http://localhost/$path")); | |
43 return _handle(request); | |
44 }).then((response) { | |
45 expect(response.statusCode, equals(404)); | |
46 }); | |
47 }); | |
48 | |
49 test("passes requests to the correct handlers", () { | |
50 var path1 = handler.create(expectAsync((request) { | |
51 expect(request.method, equals("GET")); | |
52 return new shelf.Response.ok("one"); | |
53 })); | |
54 | |
55 var path2 = handler.create(expectAsync((request) { | |
56 expect(request.method, equals("GET")); | |
57 return new shelf.Response.ok("two"); | |
58 })); | |
59 | |
60 var path3 = handler.create(expectAsync((request) { | |
61 expect(request.method, equals("GET")); | |
62 return new shelf.Response.ok("three"); | |
63 })); | |
64 | |
65 var request = new shelf.Request( | |
66 "GET", Uri.parse("http://localhost/$path2")); | |
67 return _handle(request).then((response) { | |
68 expect(response.statusCode, equals(200)); | |
69 expect(response.readAsString(), completion(equals("two"))); | |
70 | |
71 request = new shelf.Request("GET", Uri.parse("http://localhost/$path1")); | |
72 return _handle(request); | |
73 }).then((response) { | |
74 expect(response.statusCode, equals(200)); | |
75 expect(response.readAsString(), completion(equals("one"))); | |
76 | |
77 request = new shelf.Request("GET", Uri.parse("http://localhost/$path3")); | |
78 return _handle(request); | |
79 }).then((response) { | |
80 expect(response.statusCode, equals(200)); | |
81 expect(response.readAsString(), completion(equals("three"))); | |
82 }); | |
83 }); | |
84 } | |
OLD | NEW |