OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library scheduled_server_test; | 5 library scheduled_server_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:http/http.dart' as http; | 10 import 'package:http/http.dart' as http; |
(...skipping 20 matching lines...) Expand all Loading... |
31 }); | 31 }); |
32 | 32 |
33 var server = new ScheduledServer(); | 33 var server = new ScheduledServer(); |
34 expect(server.url.then((url) => http.read(url.resolve('/hello'))), | 34 expect(server.url.then((url) => http.read(url.resolve('/hello'))), |
35 completion(equals('Hello, test!'))); | 35 completion(equals('Hello, test!'))); |
36 }); | 36 }); |
37 | 37 |
38 test('test 2', () { | 38 test('test 2', () { |
39 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 39 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
40 // TODO(nweiz): There can be three errors due to issue 9151. The | 40 // TODO(nweiz): There can be three errors due to issue 9151. The |
41 // HttpParserException is reported without a stack trace, and so when it's | 41 // HttpException is reported without a stack trace, and so when it's |
42 // wrapped twice it registers as a different exception each time (because | 42 // wrapped twice it registers as a different exception each time (because |
43 // it's given an ad-hoc stack trace). Always expect two exceptions when | 43 // it's given an ad-hoc stack trace). Always expect two exceptions when |
44 // issue 9151 is fixed. | 44 // issue 9151 is fixed. |
45 expect(errors.length, inInclusiveRange(2, 3)); | 45 expect(errors.length, inInclusiveRange(2, 3)); |
46 expect(errors[0].error, equals("'scheduled server 0' received GET /hello " | 46 expect(errors[0].error, equals("'scheduled server 0' received GET /hello " |
47 "when no more requests were expected.")); | 47 "when no more requests were expected.")); |
48 expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 48 expect(errors[1].error, new isInstanceOf<HttpException>()); |
49 if (errors.length > 2) { | 49 if (errors.length > 2) { |
50 expect(errors[2].error, new isInstanceOf<HttpParserException>()); | 50 expect(errors[2].error, new isInstanceOf<HttpException>()); |
51 } | 51 } |
52 }); | 52 }); |
53 }, passing: ['test 2']); | 53 }, passing: ['test 2']); |
54 | 54 |
55 expectTestsPass("a handler runs when it's hit", () { | 55 expectTestsPass("a handler runs when it's hit", () { |
56 test('test', () { | 56 test('test', () { |
57 var server = new ScheduledServer(); | 57 var server = new ScheduledServer(); |
58 expect(server.url.then((url) => http.read(url.resolve('/hello'))), | 58 expect(server.url.then((url) => http.read(url.resolve('/hello'))), |
59 completion(equals('Hello, test!'))); | 59 completion(equals('Hello, test!'))); |
60 | 60 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 schedule(() => null); | 103 schedule(() => null); |
104 | 104 |
105 server.handle('GET', '/hello', (request) { | 105 server.handle('GET', '/hello', (request) { |
106 request.response.write('Hello, test!'); | 106 request.response.write('Hello, test!'); |
107 request.response.close(); | 107 request.response.close(); |
108 }); | 108 }); |
109 }); | 109 }); |
110 | 110 |
111 test('test 2', () { | 111 test('test 2', () { |
112 // TODO(nweiz): There can be three errors due to issue 9151. The | 112 // TODO(nweiz): There can be three errors due to issue 9151. The |
113 // HttpParserException is reported without a stack trace, and so when it's | 113 // HttpException is reported without a stack trace, and so when it's |
114 // wrapped twice it registers as a different exception each time (because | 114 // wrapped twice it registers as a different exception each time (because |
115 // it's given an ad-hoc stack trace). Always expect two exceptions when | 115 // it's given an ad-hoc stack trace). Always expect two exceptions when |
116 // issue 9151 is fixed. | 116 // issue 9151 is fixed. |
117 expect(errors.length, inInclusiveRange(2, 3)); | 117 expect(errors.length, inInclusiveRange(2, 3)); |
118 expect(errors[0].error, equals("'scheduled server 0' received GET /hello " | 118 expect(errors[0].error, equals("'scheduled server 0' received GET /hello " |
119 "earlier than expected.")); | 119 "earlier than expected.")); |
120 expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 120 expect(errors[1].error, new isInstanceOf<HttpException>()); |
121 if (errors.length > 2) { | 121 if (errors.length > 2) { |
122 expect(errors[2].error, new isInstanceOf<HttpParserException>()); | 122 expect(errors[2].error, new isInstanceOf<HttpException>()); |
123 } | 123 } |
124 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); | 124 expect(errors, everyElement(new isInstanceOf<ScheduleError>())); |
125 }); | 125 }); |
126 }, passing: ['test 2']); | 126 }, passing: ['test 2']); |
127 | 127 |
128 expectTestsPass("a handler waits for the immediately prior task to complete " | 128 expectTestsPass("a handler waits for the immediately prior task to complete " |
129 "before checking if it's too early", () { | 129 "before checking if it's too early", () { |
130 test('test', () { | 130 test('test', () { |
131 var server = new ScheduledServer(); | 131 var server = new ScheduledServer(); |
132 expect(server.url.then((url) => http.read(url.resolve('/hello'))), | 132 expect(server.url.then((url) => http.read(url.resolve('/hello'))), |
(...skipping 26 matching lines...) Expand all Loading... |
159 completion(equals('Hello, test!'))); | 159 completion(equals('Hello, test!'))); |
160 | 160 |
161 server.handle('GET', '/goodbye', (request) { | 161 server.handle('GET', '/goodbye', (request) { |
162 request.response.write('Goodbye, test!'); | 162 request.response.write('Goodbye, test!'); |
163 request.response.close(); | 163 request.response.close(); |
164 }); | 164 }); |
165 }); | 165 }); |
166 | 166 |
167 test('test 2', () { | 167 test('test 2', () { |
168 // TODO(nweiz): There can be three errors due to issue 9151. The | 168 // TODO(nweiz): There can be three errors due to issue 9151. The |
169 // HttpParserException is reported without a stack trace, and so when it's | 169 // HttpException is reported without a stack trace, and so when it's |
170 // wrapped twice it registers as a different exception each time (because | 170 // wrapped twice it registers as a different exception each time (because |
171 // it's given an ad-hoc stack trace). Always expect two exceptions when | 171 // it's given an ad-hoc stack trace). Always expect two exceptions when |
172 // issue 9151 is fixed. | 172 // issue 9151 is fixed. |
173 expect(errors.length, inInclusiveRange(2, 3)); | 173 expect(errors.length, inInclusiveRange(2, 3)); |
174 expect(errors[0].error, equals("'scheduled server 0' expected GET " | 174 expect(errors[0].error, equals("'scheduled server 0' expected GET " |
175 "/goodbye, but got GET /hello.")); | 175 "/goodbye, but got GET /hello.")); |
176 expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 176 expect(errors[1].error, new isInstanceOf<HttpException>()); |
177 if (errors.length > 2) { | 177 if (errors.length > 2) { |
178 expect(errors[2].error, new isInstanceOf<HttpParserException>()); | 178 expect(errors[2].error, new isInstanceOf<HttpException>()); |
179 } | 179 } |
180 }); | 180 }); |
181 }, passing: ['test 2']); | 181 }, passing: ['test 2']); |
182 | 182 |
183 expectTestsPass("a handler fails if the method is wrong", () { | 183 expectTestsPass("a handler fails if the method is wrong", () { |
184 var errors; | 184 var errors; |
185 test('test 1', () { | 185 test('test 1', () { |
186 currentSchedule.onException.schedule(() { | 186 currentSchedule.onException.schedule(() { |
187 errors = currentSchedule.errors; | 187 errors = currentSchedule.errors; |
188 }); | 188 }); |
189 | 189 |
190 var server = new ScheduledServer(); | 190 var server = new ScheduledServer(); |
191 expect(server.url.then((url) => http.head(url.resolve('/hello'))), | 191 expect(server.url.then((url) => http.head(url.resolve('/hello'))), |
192 completes); | 192 completes); |
193 | 193 |
194 server.handle('GET', '/hello', (request) { | 194 server.handle('GET', '/hello', (request) { |
195 request.response.write('Hello, test!'); | 195 request.response.write('Hello, test!'); |
196 request.response.close(); | 196 request.response.close(); |
197 }); | 197 }); |
198 }); | 198 }); |
199 | 199 |
200 test('test 2', () { | 200 test('test 2', () { |
201 // TODO(nweiz): There can be three errors due to issue 9151. The | 201 // TODO(nweiz): There can be three errors due to issue 9151. The |
202 // HttpParserException is reported without a stack trace, and so when it's | 202 // HttpException is reported without a stack trace, and so when it's |
203 // wrapped twice it registers as a different exception each time (because | 203 // wrapped twice it registers as a different exception each time (because |
204 // it's given an ad-hoc stack trace). Always expect two exceptions when | 204 // it's given an ad-hoc stack trace). Always expect two exceptions when |
205 // issue 9151 is fixed. | 205 // issue 9151 is fixed. |
206 expect(errors.length, inInclusiveRange(2, 3)); | 206 expect(errors.length, inInclusiveRange(2, 3)); |
207 expect(errors[0].error, equals("'scheduled server 0' expected GET " | 207 expect(errors[0].error, equals("'scheduled server 0' expected GET " |
208 "/hello, but got HEAD /hello.")); | 208 "/hello, but got HEAD /hello.")); |
209 expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 209 expect(errors[1].error, new isInstanceOf<HttpException>()); |
210 if (errors.length > 2) { | 210 if (errors.length > 2) { |
211 expect(errors[2].error, new isInstanceOf<HttpParserException>()); | 211 expect(errors[2].error, new isInstanceOf<HttpException>()); |
212 } | 212 } |
213 }); | 213 }); |
214 }, passing: ['test 2']); | 214 }, passing: ['test 2']); |
215 | 215 |
216 expectTestsPass("a handler times out waiting to be hit", () { | 216 expectTestsPass("a handler times out waiting to be hit", () { |
217 var clock = mock_clock.mock()..run(); | 217 var clock = mock_clock.mock()..run(); |
218 var timeOfException; | 218 var timeOfException; |
219 var errors; | 219 var errors; |
220 test('test 1', () { | 220 test('test 1', () { |
221 currentSchedule.timeout = new Duration(milliseconds: 2); | 221 currentSchedule.timeout = new Duration(milliseconds: 2); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 }); | 297 }); |
298 | 298 |
299 server.handle('GET', '/hello/2', (request) { | 299 server.handle('GET', '/hello/2', (request) { |
300 request.response.write('Hello, request 2!'); | 300 request.response.write('Hello, request 2!'); |
301 request.response.close(); | 301 request.response.close(); |
302 }); | 302 }); |
303 }); | 303 }); |
304 | 304 |
305 test('test 2', () { | 305 test('test 2', () { |
306 // TODO(nweiz): There can be three errors due to issue 9151. The | 306 // TODO(nweiz): There can be three errors due to issue 9151. The |
307 // HttpParserException is reported without a stack trace, and so when it's | 307 // HttpException is reported without a stack trace, and so when it's |
308 // wrapped twice it registers as a different exception each time (because | 308 // wrapped twice it registers as a different exception each time (because |
309 // it's given an ad-hoc stack trace). Always expect two exceptions when | 309 // it's given an ad-hoc stack trace). Always expect two exceptions when |
310 // issue 9151 is fixed. | 310 // issue 9151 is fixed. |
311 expect(errors.length, inInclusiveRange(2, 3)); | 311 expect(errors.length, inInclusiveRange(2, 3)); |
312 expect(errors[0].error, equals("'scheduled server 0' received GET " | 312 expect(errors[0].error, equals("'scheduled server 0' received GET " |
313 "/hello/3 when no more requests were expected.")); | 313 "/hello/3 when no more requests were expected.")); |
314 expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 314 expect(errors[1].error, new isInstanceOf<HttpException>()); |
315 if (errors.length > 2) { | 315 if (errors.length > 2) { |
316 expect(errors[2].error, new isInstanceOf<HttpParserException>()); | 316 expect(errors[2].error, new isInstanceOf<HttpException>()); |
317 } | 317 } |
318 }); | 318 }); |
319 }, passing: ['test 2']); | 319 }, passing: ['test 2']); |
320 | 320 |
321 expectTestsPass("an error in a handler doesn't cause a timeout", () { | 321 expectTestsPass("an error in a handler doesn't cause a timeout", () { |
322 var errors; | 322 var errors; |
323 test('test 1', () { | 323 test('test 1', () { |
324 currentSchedule.onException.schedule(() { | 324 currentSchedule.onException.schedule(() { |
325 errors = currentSchedule.errors; | 325 errors = currentSchedule.errors; |
326 }); | 326 }); |
327 | 327 |
328 var server = new ScheduledServer(); | 328 var server = new ScheduledServer(); |
329 expect(server.url.then((url) => http.read(url.resolve('/hello'))), | 329 expect(server.url.then((url) => http.read(url.resolve('/hello'))), |
330 completion(equals('Hello, test!'))); | 330 completion(equals('Hello, test!'))); |
331 | 331 |
332 server.handle('GET', '/hello', (request) { | 332 server.handle('GET', '/hello', (request) { |
333 throw 'oh no'; | 333 throw 'oh no'; |
334 }); | 334 }); |
335 }); | 335 }); |
336 | 336 |
337 test('test 2', () { | 337 test('test 2', () { |
338 // TODO(nweiz): There can be three errors due to issue 9151. The | 338 // TODO(nweiz): There can be three errors due to issue 9151. The |
339 // HttpParserException is reported without a stack trace, and so when it's | 339 // HttpException is reported without a stack trace, and so when it's |
340 // wrapped twice it registers as a different exception each time (because | 340 // wrapped twice it registers as a different exception each time (because |
341 // it's given an ad-hoc stack trace). Always expect two exceptions when | 341 // it's given an ad-hoc stack trace). Always expect two exceptions when |
342 // issue 9151 is fixed. | 342 // issue 9151 is fixed. |
343 expect(errors.length, inInclusiveRange(2, 3)); | 343 expect(errors.length, inInclusiveRange(2, 3)); |
344 expect(errors[0].error, equals('oh no')); | 344 expect(errors[0].error, equals('oh no')); |
345 expect(errors[1].error, new isInstanceOf<HttpParserException>()); | 345 expect(errors[1].error, new isInstanceOf<HttpException>()); |
346 if (errors.length > 2) { | 346 if (errors.length > 2) { |
347 expect(errors[2].error, new isInstanceOf<HttpParserException>()); | 347 expect(errors[2].error, new isInstanceOf<HttpException>()); |
348 } | 348 } |
349 }); | 349 }); |
350 }, passing: ['test 2']); | 350 }, passing: ['test 2']); |
351 } | 351 } |
OLD | NEW |