Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(422)

Unified Diff: third_party/pkg/route_hierarchical/test/server_test.dart

Issue 124053002: Adding Angular and dependent packages for testing (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/pkg/route_hierarchical/test/server_test.dart
diff --git a/third_party/pkg/route_hierarchical/test/server_test.dart b/third_party/pkg/route_hierarchical/test/server_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..aaf351a77565c4a307d9ddfa83273cb2cf382e1f
--- /dev/null
+++ b/third_party/pkg/route_hierarchical/test/server_test.dart
@@ -0,0 +1,135 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+
+import 'package:unittest/unittest.dart';
+import 'package:unittest/mock.dart';
+import 'package:route_hierarchical/server.dart';
+import 'dart:async';
+import 'dart:collection';
+import 'dart:io';
+
+class HttpRequestMock extends Mock implements HttpRequest {
+ Uri uri;
+ String method;
+ HttpResponseMock response = new HttpResponseMock();
+
+ HttpRequestMock(this.uri, {this.method});
+}
+
+class HttpResponseMock extends Mock implements HttpResponse {
+ int statusCode;
+ var _onClose;
+
+ Future close() {
+ if (_onClose != null) {
+ _onClose();
+ }
+ return new Future.value();
+ }
+}
+
+main() {
+ test ('http method can be used to distinguish route', (){
+ var controller = new StreamController<HttpRequest>();
+ var router = new Router(controller.stream);
+ var testReq = new HttpRequestMock(Uri.parse('/foo'),method:'GET');
+ router.serve('/foo', method:'GET').listen(expectAsync1((req) {
+ expect(req, testReq);
+ }));
+ router.serve('/foo', method:'POST').listen(expectAsync1((_) {}, count:0));
+ controller.add(testReq);
+ });
+
+ test ('if no http method provided, all methods match', (){
+ var controller = new StreamController<HttpRequest>();
+ var router = new Router(controller.stream);
+ var testGetReq = new HttpRequestMock(Uri.parse('/foo'), method:'GET');
+ var testPostReq = new HttpRequestMock(Uri.parse('/foo'), method:'POST');
+ var requests = <HttpRequest>[];
+ router.serve('/foo').listen(expectAsync1((request) {
+ requests.add(request);
+ if (requests.length == 2){
+ expect(requests, [testGetReq, testPostReq]);
+ }
+ }, count: 2));
+ controller.add(testGetReq);
+ controller.add(testPostReq);
+
+ });
+
+ test('serve 1', () {
+ var controller = new StreamController<HttpRequest>();
+ var router = new Router(controller.stream);
+ var testReq = new HttpRequestMock(Uri.parse('/foo'));
+ router.serve('/foo').listen(expectAsync1((req) {
+ expect(req, testReq);
+ }));
+ router.serve('/bar').listen(expectAsync1((req) {}, count: 0));
+ controller.add(testReq);
+ });
+
+ test('serve 2', () {
+ var controller = new StreamController<HttpRequest>();
+ var router = new Router(controller.stream);
+ var testReq = new HttpRequestMock(Uri.parse('/bar'));
+ router.serve('/foo').listen(expectAsync1((req) {}, count: 0));
+ router.serve('/bar').listen(expectAsync1((req) {
+ expect(req, testReq);
+ }));
+ controller.add(testReq);
+ });
+
+ test('404', () {
+ var controller = new StreamController<HttpRequest>();
+ var router = new Router(controller.stream);
+ var testReq = new HttpRequestMock(Uri.parse('/bar'));
+ testReq.response._onClose = expectAsync0(() {
+ expect(testReq.response.statusCode, 404);
+ });
+ router.serve('/foo').listen(expectAsync1((req) {}, count: 0));
+ controller.add(testReq);
+ });
+
+ test('default', () {
+ var controller = new StreamController<HttpRequest>();
+ var router = new Router(controller.stream);
+ var testReq = new HttpRequestMock(Uri.parse('/bar'));
+ testReq.response._onClose = expectAsync0(() {
+ expect(testReq.response.statusCode, 200);
+ });
+ router.defaultStream.listen(expectAsync1((HttpRequest req) {
+ req.response.statusCode = 200;
+ req.response.close();
+ }));
+ controller.add(testReq);
+ });
+
+ test('filter pass', () {
+ var controller = new StreamController<HttpRequest>();
+ var router = new Router(controller.stream);
+ var testReq = new HttpRequestMock(Uri.parse('/foo'));
+ router.filter('/foo', expectAsync1((req) {
+ expect(req, testReq);
+ return new Future.value(true);
+ }));
+ router.serve('/foo').listen(expectAsync1((req) {
+ expect(req, testReq);
+ }));
+ controller.add(testReq);
+ });
+
+ test('filter no-pass', () {
+ var controller = new StreamController<HttpRequest>();
+ var router = new Router(controller.stream);
+ var testReq = new HttpRequestMock(Uri.parse('/foo'));
+ router.filter('/foo', expectAsync1((req) {
+ expect(req, testReq);
+ return new Future.value(false);
+ }));
+ router.serve('/foo').listen(expectAsync1((req) {}, count: 0));
+ controller.add(testReq);
+ });
+
+}

Powered by Google App Engine
This is Rietveld 408576698