OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 shelf_io_test; | 5 library shelf_io_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
11 import 'package:http/http.dart' as http; | 11 import 'package:http/http.dart' as http; |
| 12 import 'package:http_parser/http_parser.dart' as parser; |
12 import 'package:scheduled_test/scheduled_test.dart'; | 13 import 'package:scheduled_test/scheduled_test.dart'; |
13 import 'package:shelf/shelf.dart'; | 14 import 'package:shelf/shelf.dart'; |
14 import 'package:shelf/shelf_io.dart' as shelf_io; | 15 import 'package:shelf/shelf_io.dart' as shelf_io; |
15 | 16 |
16 import 'test_util.dart'; | 17 import 'test_util.dart'; |
17 | 18 |
18 void main() { | 19 void main() { |
19 test('sync handler returns a value to the client', () { | 20 test('sync handler returns a value to the client', () { |
20 _scheduleServer(syncHandler); | 21 _scheduleServer(syncHandler); |
21 | 22 |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 return syncHandler(request); | 243 return syncHandler(request); |
243 }, 'localhost', 0).then((server) { | 244 }, 'localhost', 0).then((server) { |
244 return http.get('http://localhost:${server.port}').then((response) { | 245 return http.get('http://localhost:${server.port}').then((response) { |
245 expect(response.statusCode, HttpStatus.OK); | 246 expect(response.statusCode, HttpStatus.OK); |
246 expect(response.body, 'Hello from /'); | 247 expect(response.body, 'Hello from /'); |
247 server.close(); | 248 server.close(); |
248 }); | 249 }); |
249 }); | 250 }); |
250 }); | 251 }); |
251 }); | 252 }); |
| 253 |
| 254 group('date header', () { |
| 255 test('is sent by default', () { |
| 256 _scheduleServer(syncHandler); |
| 257 |
| 258 // Update beforeRequest to be one second earlier. HTTP dates only have |
| 259 // second-level granularity and the request will likely take less than a |
| 260 // second. |
| 261 var beforeRequest = new DateTime.now().subtract(new Duration(seconds: 1)); |
| 262 |
| 263 return _scheduleGet().then((response) { |
| 264 expect(response.headers, contains('date')); |
| 265 var responseDate = parser.parseHttpDate(response.headers['date']); |
| 266 |
| 267 expect(responseDate.isAfter(beforeRequest), isTrue); |
| 268 expect(responseDate.isBefore(new DateTime.now()), isTrue); |
| 269 }); |
| 270 }); |
| 271 |
| 272 test('defers to header in response', () { |
| 273 var date = new DateTime.utc(1981, 6, 5); |
| 274 _scheduleServer((request) { |
| 275 return new Response.ok('test', headers: { |
| 276 HttpHeaders.DATE: parser.formatHttpDate(date) |
| 277 }); |
| 278 }); |
| 279 |
| 280 return _scheduleGet().then((response) { |
| 281 expect(response.headers, contains('date')); |
| 282 var responseDate = parser.parseHttpDate(response.headers['date']); |
| 283 expect(responseDate, date); |
| 284 }); |
| 285 }); |
| 286 }); |
252 } | 287 } |
253 | 288 |
254 int _serverPort; | 289 int _serverPort; |
255 | 290 |
256 Future _scheduleServer(Handler handler) { | 291 Future _scheduleServer(Handler handler) { |
257 return schedule(() => shelf_io.serve(handler, 'localhost', 0).then((server) { | 292 return schedule(() => shelf_io.serve(handler, 'localhost', 0).then((server) { |
258 currentSchedule.onComplete.schedule(() { | 293 currentSchedule.onComplete.schedule(() { |
259 _serverPort = null; | 294 _serverPort = null; |
260 return server.close(force: true); | 295 return server.close(force: true); |
261 }); | 296 }); |
(...skipping 16 matching lines...) Expand all Loading... |
278 | 313 |
279 var request = new http.Request('POST', | 314 var request = new http.Request('POST', |
280 Uri.parse('http://localhost:$_serverPort/')); | 315 Uri.parse('http://localhost:$_serverPort/')); |
281 | 316 |
282 if (headers != null) request.headers.addAll(headers); | 317 if (headers != null) request.headers.addAll(headers); |
283 if (body != null) request.body = body; | 318 if (body != null) request.body = body; |
284 | 319 |
285 return request.send(); | 320 return request.send(); |
286 }); | 321 }); |
287 } | 322 } |
OLD | NEW |