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 | 5 |
6 import 'package:unittest/unittest.dart'; | 6 import 'package:unittest/unittest.dart'; |
7 import 'package:unittest/mock.dart'; | 7 import 'package:unittest/mock.dart'; |
8 import 'package:route/server.dart'; | 8 import 'package:route/server.dart'; |
9 import 'dart:async'; | 9 import 'dart:async'; |
10 import 'dart:collection'; | 10 import 'dart:collection'; |
11 import 'dart:io'; | 11 import 'dart:io'; |
12 import 'dart:uri'; | 12 import 'dart:uri'; |
13 | 13 |
14 class HttpRequestMock extends Mock implements HttpRequest { | 14 class HttpRequestMock extends Mock implements HttpRequest { |
15 Uri uri; | 15 Uri uri; |
16 String method; | |
16 HttpResponseMock response = new HttpResponseMock(); | 17 HttpResponseMock response = new HttpResponseMock(); |
17 HttpRequestMock(this.uri); | 18 HttpRequestMock(this.uri, {this.method}); |
18 } | 19 } |
19 | 20 |
20 class HttpResponseMock extends Mock implements HttpResponse { | 21 class HttpResponseMock extends Mock implements HttpResponse { |
21 int statusCode; | 22 int statusCode; |
22 var _onClose; | 23 var _onClose; |
23 void close() { | 24 void close() { |
24 if (_onClose != null) { | 25 if (_onClose != null) { |
25 _onClose(); | 26 _onClose(); |
26 } | 27 } |
27 } | 28 } |
28 } | 29 } |
29 | 30 |
30 main() { | 31 main() { |
32 test ('http method can be used to distinguish route', (){ | |
33 //should be able to specify a single uri route which invokes | |
justinfagnani
2013/04/19 19:20:51
add space after //
adambender
2013/04/19 19:43:11
Done.
| |
34 //different handlers depending on http method name | |
35 var controller = new StreamController<HttpRequest>(); | |
36 var router = new Router(controller.stream); | |
37 var testReq = new HttpRequestMock(new Uri('/foo'),method:'GET'); | |
38 router.serve('/foo', method:'GET').listen(expectAsync1((req) { | |
39 expect(req, testReq); | |
40 })); | |
41 router.serve('/foo', method:'POST').listen(expectAsync1((_) {}, count:0)); | |
42 controller.add(testReq); | |
43 }); | |
44 | |
45 test ('if no http method provided, all methods match', (){ | |
46 //should be able to specify a single uri route which invokes | |
47 //different handlers depending on http method name | |
48 var controller = new StreamController<HttpRequest>(); | |
49 var router = new Router(controller.stream); | |
50 var testGetReq = new HttpRequestMock(new Uri('/foo'), method:'GET'); | |
51 var testPostReq = new HttpRequestMock(new Uri('/foo'), method:'POST'); | |
52 var requests = <HttpRequest>[]; | |
53 router.serve('/foo').listen(expectAsync1((request) { | |
54 requests.add(request); | |
55 if (requests.length == 2){ | |
56 expect(requests, [testGetReq, testPostReq]); | |
57 } | |
58 }, count: 2)); | |
59 controller.add(testGetReq); | |
60 controller.add(testPostReq); | |
61 | |
62 }); | |
63 | |
31 test('serve 1', () { | 64 test('serve 1', () { |
32 var controller = new StreamController<HttpRequest>(); | 65 var controller = new StreamController<HttpRequest>(); |
33 var router = new Router(controller.stream); | 66 var router = new Router(controller.stream); |
34 var testReq = new HttpRequestMock(new Uri('/foo')); | 67 var testReq = new HttpRequestMock(new Uri('/foo')); |
35 router.serve('/foo').listen(expectAsync1((req) { | 68 router.serve('/foo').listen(expectAsync1((req) { |
36 expect(req, testReq); | 69 expect(req, testReq); |
37 })); | 70 })); |
38 router.serve('/bar').listen(expectAsync1((req) {}, count: 0)); | 71 router.serve('/bar').listen(expectAsync1((req) {}, count: 0)); |
39 controller.add(testReq); | 72 controller.add(testReq); |
40 }); | 73 }); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 var testReq = new HttpRequestMock(new Uri('/foo')); | 128 var testReq = new HttpRequestMock(new Uri('/foo')); |
96 router.filter('/foo', expectAsync1((req) { | 129 router.filter('/foo', expectAsync1((req) { |
97 expect(req, testReq); | 130 expect(req, testReq); |
98 return new Future.value(false); | 131 return new Future.value(false); |
99 })); | 132 })); |
100 router.serve('/foo').listen(expectAsync1((req) {}, count: 0)); | 133 router.serve('/foo').listen(expectAsync1((req) {}, count: 0)); |
101 controller.add(testReq); | 134 controller.add(testReq); |
102 }); | 135 }); |
103 | 136 |
104 } | 137 } |
OLD | NEW |