| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 |
| 6 import 'package:unittest/unittest.dart'; |
| 7 import 'package:unittest/mock.dart'; |
| 8 import 'package:route_hierarchical/server.dart'; |
| 9 import 'dart:async'; |
| 10 import 'dart:collection'; |
| 11 import 'dart:io'; |
| 12 |
| 13 class HttpRequestMock extends Mock implements HttpRequest { |
| 14 Uri uri; |
| 15 String method; |
| 16 HttpResponseMock response = new HttpResponseMock(); |
| 17 |
| 18 HttpRequestMock(this.uri, {this.method}); |
| 19 } |
| 20 |
| 21 class HttpResponseMock extends Mock implements HttpResponse { |
| 22 int statusCode; |
| 23 var _onClose; |
| 24 |
| 25 Future close() { |
| 26 if (_onClose != null) { |
| 27 _onClose(); |
| 28 } |
| 29 return new Future.value(); |
| 30 } |
| 31 } |
| 32 |
| 33 main() { |
| 34 test ('http method can be used to distinguish route', (){ |
| 35 var controller = new StreamController<HttpRequest>(); |
| 36 var router = new Router(controller.stream); |
| 37 var testReq = new HttpRequestMock(Uri.parse('/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 var controller = new StreamController<HttpRequest>(); |
| 47 var router = new Router(controller.stream); |
| 48 var testGetReq = new HttpRequestMock(Uri.parse('/foo'), method:'GET'); |
| 49 var testPostReq = new HttpRequestMock(Uri.parse('/foo'), method:'POST'); |
| 50 var requests = <HttpRequest>[]; |
| 51 router.serve('/foo').listen(expectAsync1((request) { |
| 52 requests.add(request); |
| 53 if (requests.length == 2){ |
| 54 expect(requests, [testGetReq, testPostReq]); |
| 55 } |
| 56 }, count: 2)); |
| 57 controller.add(testGetReq); |
| 58 controller.add(testPostReq); |
| 59 |
| 60 }); |
| 61 |
| 62 test('serve 1', () { |
| 63 var controller = new StreamController<HttpRequest>(); |
| 64 var router = new Router(controller.stream); |
| 65 var testReq = new HttpRequestMock(Uri.parse('/foo')); |
| 66 router.serve('/foo').listen(expectAsync1((req) { |
| 67 expect(req, testReq); |
| 68 })); |
| 69 router.serve('/bar').listen(expectAsync1((req) {}, count: 0)); |
| 70 controller.add(testReq); |
| 71 }); |
| 72 |
| 73 test('serve 2', () { |
| 74 var controller = new StreamController<HttpRequest>(); |
| 75 var router = new Router(controller.stream); |
| 76 var testReq = new HttpRequestMock(Uri.parse('/bar')); |
| 77 router.serve('/foo').listen(expectAsync1((req) {}, count: 0)); |
| 78 router.serve('/bar').listen(expectAsync1((req) { |
| 79 expect(req, testReq); |
| 80 })); |
| 81 controller.add(testReq); |
| 82 }); |
| 83 |
| 84 test('404', () { |
| 85 var controller = new StreamController<HttpRequest>(); |
| 86 var router = new Router(controller.stream); |
| 87 var testReq = new HttpRequestMock(Uri.parse('/bar')); |
| 88 testReq.response._onClose = expectAsync0(() { |
| 89 expect(testReq.response.statusCode, 404); |
| 90 }); |
| 91 router.serve('/foo').listen(expectAsync1((req) {}, count: 0)); |
| 92 controller.add(testReq); |
| 93 }); |
| 94 |
| 95 test('default', () { |
| 96 var controller = new StreamController<HttpRequest>(); |
| 97 var router = new Router(controller.stream); |
| 98 var testReq = new HttpRequestMock(Uri.parse('/bar')); |
| 99 testReq.response._onClose = expectAsync0(() { |
| 100 expect(testReq.response.statusCode, 200); |
| 101 }); |
| 102 router.defaultStream.listen(expectAsync1((HttpRequest req) { |
| 103 req.response.statusCode = 200; |
| 104 req.response.close(); |
| 105 })); |
| 106 controller.add(testReq); |
| 107 }); |
| 108 |
| 109 test('filter pass', () { |
| 110 var controller = new StreamController<HttpRequest>(); |
| 111 var router = new Router(controller.stream); |
| 112 var testReq = new HttpRequestMock(Uri.parse('/foo')); |
| 113 router.filter('/foo', expectAsync1((req) { |
| 114 expect(req, testReq); |
| 115 return new Future.value(true); |
| 116 })); |
| 117 router.serve('/foo').listen(expectAsync1((req) { |
| 118 expect(req, testReq); |
| 119 })); |
| 120 controller.add(testReq); |
| 121 }); |
| 122 |
| 123 test('filter no-pass', () { |
| 124 var controller = new StreamController<HttpRequest>(); |
| 125 var router = new Router(controller.stream); |
| 126 var testReq = new HttpRequestMock(Uri.parse('/foo')); |
| 127 router.filter('/foo', expectAsync1((req) { |
| 128 expect(req, testReq); |
| 129 return new Future.value(false); |
| 130 })); |
| 131 router.serve('/foo').listen(expectAsync1((req) {}, count: 0)); |
| 132 controller.add(testReq); |
| 133 }); |
| 134 |
| 135 } |
| OLD | NEW |