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.create_middleware_test; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:shelf/shelf.dart'; | |
10 import 'package:unittest/unittest.dart'; | |
11 | |
12 import 'test_util.dart'; | |
13 | |
14 void main() { | |
15 test('forwards the request and response if both handlers are null', () { | |
16 var handler = const Pipeline() | |
17 .addMiddleware(createMiddleware()) | |
18 .addHandler((request) { | |
19 return syncHandler(request, headers: {'from' : 'innerHandler'}); | |
20 }); | |
21 | |
22 return makeSimpleRequest(handler).then((response) { | |
23 expect(response.headers['from'], 'innerHandler'); | |
24 }); | |
25 }); | |
26 | |
27 group('requestHandler', () { | |
28 test('sync null response forwards to inner handler', () { | |
29 var handler = const Pipeline() | |
30 .addMiddleware(createMiddleware(requestHandler: (request) => null)) | |
31 .addHandler(syncHandler); | |
32 | |
33 return makeSimpleRequest(handler).then((response) { | |
34 expect(response.headers['from'], isNull); | |
35 }); | |
36 }); | |
37 | |
38 test('async null response forwards to inner handler', () { | |
39 var handler = const Pipeline() | |
40 .addMiddleware(createMiddleware( | |
41 requestHandler: (request) => new Future.value(null))) | |
42 .addHandler(syncHandler); | |
43 | |
44 return makeSimpleRequest(handler).then((response) { | |
45 expect(response.headers['from'], isNull); | |
46 }); | |
47 }); | |
48 | |
49 test('sync response is returned', () { | |
50 var handler = const Pipeline() | |
51 .addMiddleware(createMiddleware( | |
52 requestHandler: (request) => _middlewareResponse)) | |
53 .addHandler(_failHandler); | |
54 | |
55 return makeSimpleRequest(handler).then((response) { | |
56 expect(response.headers['from'], 'middleware'); | |
57 }); | |
58 }); | |
59 | |
60 test('async response is returned', () { | |
61 var handler = const Pipeline() | |
62 .addMiddleware(createMiddleware(requestHandler: (request) => | |
63 new Future.value(_middlewareResponse))) | |
64 .addHandler(_failHandler); | |
65 | |
66 return makeSimpleRequest(handler).then((response) { | |
67 expect(response.headers['from'], 'middleware'); | |
68 }); | |
69 }); | |
70 | |
71 group('with responseHandler', () { | |
72 test('with sync result, responseHandler is not called', () { | |
73 var middleware = createMiddleware( | |
74 requestHandler: (request) => _middlewareResponse, | |
75 responseHandler: (response) => fail('should not be called')); | |
76 | |
77 var handler = const Pipeline() | |
78 .addMiddleware(middleware) | |
79 .addHandler(syncHandler); | |
80 | |
81 return makeSimpleRequest(handler).then((response) { | |
82 expect(response.headers['from'], 'middleware'); | |
83 }); | |
84 }); | |
85 | |
86 test('with async result, responseHandler is not called', () { | |
87 var middleware = createMiddleware( | |
88 requestHandler: (request) => new Future.value(_middlewareResponse), | |
89 responseHandler: (response) => fail('should not be called')); | |
90 var handler = const Pipeline() | |
91 .addMiddleware(middleware) | |
92 .addHandler(syncHandler); | |
93 | |
94 return makeSimpleRequest(handler).then((response) { | |
95 expect(response.headers['from'], 'middleware'); | |
96 }); | |
97 }); | |
98 }); | |
99 }); | |
100 | |
101 group('responseHandler', () { | |
102 test('innerHandler sync response is seen, replaced value continues', () { | |
103 var handler = const Pipeline().addMiddleware(createMiddleware( | |
104 responseHandler: (response) { | |
105 expect(response.headers['from'], 'handler'); | |
106 return _middlewareResponse; | |
107 })).addHandler((request) { | |
108 return syncHandler(request, headers: {'from' : 'handler'} ); | |
109 }); | |
110 | |
111 return makeSimpleRequest(handler).then((response) { | |
112 expect(response.headers['from'], 'middleware'); | |
113 }); | |
114 }); | |
115 | |
116 test('innerHandler async response is seen, async value continues', () { | |
117 var handler = const Pipeline().addMiddleware( | |
118 createMiddleware(responseHandler: (response) { | |
119 expect(response.headers['from'], 'handler'); | |
120 return new Future.value(_middlewareResponse); | |
121 })).addHandler((request) { | |
122 return new Future(() => syncHandler( | |
123 request, headers: {'from' : 'handler'} )); | |
124 }); | |
125 | |
126 return makeSimpleRequest(handler).then((response) { | |
127 expect(response.headers['from'], 'middleware'); | |
128 }); | |
129 }); | |
130 }); | |
131 | |
132 group('error handling', () { | |
133 test('sync error thrown by requestHandler bubbles down', () { | |
134 var handler = const Pipeline() | |
135 .addMiddleware(createMiddleware( | |
136 requestHandler: (request) => throw 'middleware error')) | |
137 .addHandler(_failHandler); | |
138 | |
139 expect(makeSimpleRequest(handler), throwsA('middleware error')); | |
140 }); | |
141 | |
142 test('async error thrown by requestHandler bubbles down', () { | |
143 var handler = const Pipeline() | |
144 .addMiddleware(createMiddleware(requestHandler: (request) => | |
145 new Future.error('middleware error'))) | |
146 .addHandler(_failHandler); | |
147 | |
148 expect(makeSimpleRequest(handler), throwsA('middleware error')); | |
149 }); | |
150 | |
151 test('throw from responseHandler does not hit error handler', () { | |
152 var middleware = createMiddleware(responseHandler: (response) { | |
153 throw 'middleware error'; | |
154 }, errorHandler: (e, s) => fail('should never get here')); | |
155 | |
156 var handler = const Pipeline().addMiddleware(middleware) | |
157 .addHandler(syncHandler); | |
158 | |
159 expect(makeSimpleRequest(handler), throwsA('middleware error')); | |
160 }); | |
161 | |
162 test('requestHandler throw does not hit errorHandlers', () { | |
163 var middleware = createMiddleware( | |
164 requestHandler: (request) { | |
165 throw 'middleware error'; | |
166 }, | |
167 errorHandler: (e, s) => fail('should never get here')); | |
168 | |
169 var handler = const Pipeline().addMiddleware(middleware) | |
170 .addHandler(syncHandler); | |
171 | |
172 expect(makeSimpleRequest(handler), throwsA('middleware error')); | |
173 }); | |
174 | |
175 test('inner handler throws, is caught by errorHandler with response', () { | |
176 var middleware = createMiddleware( | |
177 errorHandler: (error, stack) { | |
178 expect(error, 'bad handler'); | |
179 return _middlewareResponse; | |
180 }); | |
181 | |
182 var handler = const Pipeline().addMiddleware(middleware) | |
183 .addHandler((request) { | |
184 throw 'bad handler'; | |
185 }); | |
186 | |
187 return makeSimpleRequest(handler).then((response) { | |
188 expect(response.headers['from'], 'middleware'); | |
189 }); | |
190 }); | |
191 | |
192 test('inner handler throws, is caught by errorHandler and rethrown', () { | |
193 var middleware = createMiddleware(errorHandler: (error, stack) { | |
194 expect(error, 'bad handler'); | |
195 throw error; | |
196 }); | |
197 | |
198 var handler = const Pipeline().addMiddleware(middleware) | |
199 .addHandler((request) { | |
200 throw 'bad handler'; | |
201 }); | |
202 | |
203 expect(makeSimpleRequest(handler), throwsA('bad handler')); | |
204 }); | |
205 | |
206 test('error thrown by inner handler without a middleware errorHandler is ' | |
207 'rethrown', () { | |
208 var middleware = createMiddleware(); | |
209 | |
210 var handler = const Pipeline().addMiddleware(middleware) | |
211 .addHandler((request) { | |
212 throw 'bad handler'; | |
213 }); | |
214 | |
215 expect(makeSimpleRequest(handler), throwsA('bad handler')); | |
216 }); | |
217 | |
218 test("doesn't handle HijackException", () { | |
219 var middleware = createMiddleware(errorHandler: (error, stack) { | |
220 fail("error handler shouldn't be called"); | |
221 }); | |
222 | |
223 var handler = const Pipeline().addMiddleware(middleware) | |
224 .addHandler((request) => throw const HijackException()); | |
225 | |
226 expect(makeSimpleRequest(handler), | |
227 throwsA(new isInstanceOf<HijackException>())); | |
228 }); | |
229 }); | |
230 } | |
231 | |
232 _failHandler(Request request) => fail('should never get here'); | |
233 | |
234 final Response _middlewareResponse = | |
235 new Response.ok('middleware content', headers: {'from': 'middleware'}); | |
OLD | NEW |