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

Side by Side Diff: test/one_off_handler_test.dart

Issue 962363003: Add a shelf handler for running one-off handlers. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Created 5 years, 9 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 unified diff | Download patch
« no previous file with comments | « lib/src/util/one_off_handler.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « lib/src/util/one_off_handler.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698