OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 library shelf.cascade_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:shelf/shelf.dart'; |
| 10 import 'package:test/test.dart'; |
| 11 |
| 12 import 'test_util.dart'; |
| 13 |
| 14 void main() { |
| 15 group('a cascade with several handlers', () { |
| 16 var handler; |
| 17 setUp(() { |
| 18 handler = new Cascade().add((request) { |
| 19 if (request.headers['one'] == 'false') { |
| 20 return new Response.notFound('handler 1'); |
| 21 } else { |
| 22 return new Response.ok('handler 1'); |
| 23 } |
| 24 }).add((request) { |
| 25 if (request.headers['two'] == 'false') { |
| 26 return new Response.notFound('handler 2'); |
| 27 } else { |
| 28 return new Response.ok('handler 2'); |
| 29 } |
| 30 }).add((request) { |
| 31 if (request.headers['three'] == 'false') { |
| 32 return new Response.notFound('handler 3'); |
| 33 } else { |
| 34 return new Response.ok('handler 3'); |
| 35 } |
| 36 }).handler; |
| 37 }); |
| 38 |
| 39 test('the first response should be returned if it matches', () { |
| 40 return makeSimpleRequest(handler).then((response) { |
| 41 expect(response.statusCode, equals(200)); |
| 42 expect(response.readAsString(), completion(equals('handler 1'))); |
| 43 }); |
| 44 }); |
| 45 |
| 46 test("the second response should be returned if it matches and the first " |
| 47 "doesn't", () { |
| 48 return new Future.sync(() { |
| 49 return handler( |
| 50 new Request('GET', LOCALHOST_URI, headers: {'one': 'false'})); |
| 51 }).then((response) { |
| 52 expect(response.statusCode, equals(200)); |
| 53 expect(response.readAsString(), completion(equals('handler 2'))); |
| 54 }); |
| 55 }); |
| 56 |
| 57 test("the third response should be returned if it matches and the first " |
| 58 "two don't", () { |
| 59 return new Future.sync(() { |
| 60 return handler(new Request('GET', LOCALHOST_URI, |
| 61 headers: {'one': 'false', 'two': 'false'})); |
| 62 }).then((response) { |
| 63 expect(response.statusCode, equals(200)); |
| 64 expect(response.readAsString(), completion(equals('handler 3'))); |
| 65 }); |
| 66 }); |
| 67 |
| 68 test("the third response should be returned if no response matches", () { |
| 69 return new Future.sync(() { |
| 70 return handler(new Request('GET', LOCALHOST_URI, |
| 71 headers: {'one': 'false', 'two': 'false', 'three': 'false'})); |
| 72 }).then((response) { |
| 73 expect(response.statusCode, equals(404)); |
| 74 expect(response.readAsString(), completion(equals('handler 3'))); |
| 75 }); |
| 76 }); |
| 77 }); |
| 78 |
| 79 test('a 404 response triggers a cascade by default', () { |
| 80 var handler = new Cascade() |
| 81 .add((_) => new Response.notFound('handler 1')) |
| 82 .add((_) => new Response.ok('handler 2')) |
| 83 .handler; |
| 84 |
| 85 return makeSimpleRequest(handler).then((response) { |
| 86 expect(response.statusCode, equals(200)); |
| 87 expect(response.readAsString(), completion(equals('handler 2'))); |
| 88 }); |
| 89 }); |
| 90 |
| 91 test('a 405 response triggers a cascade by default', () { |
| 92 var handler = new Cascade() |
| 93 .add((_) => new Response(405)) |
| 94 .add((_) => new Response.ok('handler 2')) |
| 95 .handler; |
| 96 |
| 97 return makeSimpleRequest(handler).then((response) { |
| 98 expect(response.statusCode, equals(200)); |
| 99 expect(response.readAsString(), completion(equals('handler 2'))); |
| 100 }); |
| 101 }); |
| 102 |
| 103 test('[statusCodes] controls which statuses cause cascading', () { |
| 104 var handler = new Cascade(statusCodes: [302, 403]) |
| 105 .add((_) => new Response.found('/')) |
| 106 .add((_) => new Response.forbidden('handler 2')) |
| 107 .add((_) => new Response.notFound('handler 3')) |
| 108 .add((_) => new Response.ok('handler 4')) |
| 109 .handler; |
| 110 |
| 111 return makeSimpleRequest(handler).then((response) { |
| 112 expect(response.statusCode, equals(404)); |
| 113 expect(response.readAsString(), completion(equals('handler 3'))); |
| 114 }); |
| 115 }); |
| 116 |
| 117 test('[shouldCascade] controls which responses cause cascading', () { |
| 118 var handler = new Cascade( |
| 119 shouldCascade: (response) => response.statusCode % 2 == 1) |
| 120 .add((_) => new Response.movedPermanently('/')) |
| 121 .add((_) => new Response.forbidden('handler 2')) |
| 122 .add((_) => new Response.notFound('handler 3')) |
| 123 .add((_) => new Response.ok('handler 4')) |
| 124 .handler; |
| 125 |
| 126 return makeSimpleRequest(handler).then((response) { |
| 127 expect(response.statusCode, equals(404)); |
| 128 expect(response.readAsString(), completion(equals('handler 3'))); |
| 129 }); |
| 130 }); |
| 131 |
| 132 group('errors', () { |
| 133 test('getting the handler for an empty cascade fails', () { |
| 134 expect(() => new Cascade().handler, throwsStateError); |
| 135 }); |
| 136 |
| 137 test('passing [statusCodes] and [shouldCascade] at the same time fails', |
| 138 () { |
| 139 expect(() => |
| 140 new Cascade(statusCodes: [404, 405], shouldCascade: (_) => false), |
| 141 throwsArgumentError); |
| 142 }); |
| 143 }); |
| 144 } |
OLD | NEW |