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