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 var controller = new StreamController<HttpRequest>(); |
| 34 var router = new Router(controller.stream); |
| 35 var testReq = new HttpRequestMock(new Uri('/foo'),method:'GET'); |
| 36 router.serve('/foo', method:'GET').listen(expectAsync1((req) { |
| 37 expect(req, testReq); |
| 38 })); |
| 39 router.serve('/foo', method:'POST').listen(expectAsync1((_) {}, count:0)); |
| 40 controller.add(testReq); |
| 41 }); |
| 42 |
| 43 test ('if no http method provided, all methods match', (){ |
| 44 var controller = new StreamController<HttpRequest>(); |
| 45 var router = new Router(controller.stream); |
| 46 var testGetReq = new HttpRequestMock(new Uri('/foo'), method:'GET'); |
| 47 var testPostReq = new HttpRequestMock(new Uri('/foo'), method:'POST'); |
| 48 var requests = <HttpRequest>[]; |
| 49 router.serve('/foo').listen(expectAsync1((request) { |
| 50 requests.add(request); |
| 51 if (requests.length == 2){ |
| 52 expect(requests, [testGetReq, testPostReq]); |
| 53 } |
| 54 }, count: 2)); |
| 55 controller.add(testGetReq); |
| 56 controller.add(testPostReq); |
| 57 |
| 58 }); |
| 59 |
31 test('serve 1', () { | 60 test('serve 1', () { |
32 var controller = new StreamController<HttpRequest>(); | 61 var controller = new StreamController<HttpRequest>(); |
33 var router = new Router(controller.stream); | 62 var router = new Router(controller.stream); |
34 var testReq = new HttpRequestMock(new Uri('/foo')); | 63 var testReq = new HttpRequestMock(new Uri('/foo')); |
35 router.serve('/foo').listen(expectAsync1((req) { | 64 router.serve('/foo').listen(expectAsync1((req) { |
36 expect(req, testReq); | 65 expect(req, testReq); |
37 })); | 66 })); |
38 router.serve('/bar').listen(expectAsync1((req) {}, count: 0)); | 67 router.serve('/bar').listen(expectAsync1((req) {}, count: 0)); |
39 controller.add(testReq); | 68 controller.add(testReq); |
40 }); | 69 }); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 var testReq = new HttpRequestMock(new Uri('/foo')); | 124 var testReq = new HttpRequestMock(new Uri('/foo')); |
96 router.filter('/foo', expectAsync1((req) { | 125 router.filter('/foo', expectAsync1((req) { |
97 expect(req, testReq); | 126 expect(req, testReq); |
98 return new Future.value(false); | 127 return new Future.value(false); |
99 })); | 128 })); |
100 router.serve('/foo').listen(expectAsync1((req) {}, count: 0)); | 129 router.serve('/foo').listen(expectAsync1((req) {}, count: 0)); |
101 controller.add(testReq); | 130 controller.add(testReq); |
102 }); | 131 }); |
103 | 132 |
104 } | 133 } |
OLD | NEW |