Chromium Code Reviews| 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 return _scheduleGet().then((response) { | |
| 259 expect(response.headers, contains('date')); | |
| 260 var responseDate = parser.parseHttpDate(response.headers['date']); | |
| 261 | |
| 262 var nowEpoch = new DateTime.now().toUtc().millisecondsSinceEpoch; | |
| 263 expect(responseDate.millisecondsSinceEpoch, | |
| 264 inInclusiveRange(nowEpoch - 1000, nowEpoch)); | |
|
nweiz
2014/05/27 19:52:44
This is potentially flaky. Instead of assuming the
kevmoo
2014/05/27 20:50:58
Done. I have to add in some rounding to account fo
| |
| 265 }); | |
| 266 }); | |
| 267 | |
| 268 test('defers to header in response', () { | |
| 269 var date = new DateTime.utc(1981, 6, 5); | |
| 270 _scheduleServer((request) { | |
| 271 return new Response.ok('test', headers: { | |
| 272 HttpHeaders.DATE: parser.formatHttpDate(date) | |
|
nweiz
2014/05/27 19:52:44
Nit: I don't like using HttpHeaders over just a he
kevmoo
2014/05/27 20:50:58
We're already using it in shelf_io. Eh...
| |
| 273 }); | |
| 274 }); | |
| 275 | |
| 276 return _scheduleGet().then((response) { | |
| 277 expect(response.headers, contains('date')); | |
| 278 var responseDate = parser.parseHttpDate(response.headers['date']); | |
| 279 expect(responseDate, date); | |
| 280 }); | |
| 281 }); | |
| 282 }); | |
| 252 } | 283 } |
| 253 | 284 |
| 254 int _serverPort; | 285 int _serverPort; |
| 255 | 286 |
| 256 Future _scheduleServer(Handler handler) { | 287 Future _scheduleServer(Handler handler) { |
| 257 return schedule(() => shelf_io.serve(handler, 'localhost', 0).then((server) { | 288 return schedule(() => shelf_io.serve(handler, 'localhost', 0).then((server) { |
| 258 currentSchedule.onComplete.schedule(() { | 289 currentSchedule.onComplete.schedule(() { |
| 259 _serverPort = null; | 290 _serverPort = null; |
| 260 return server.close(force: true); | 291 return server.close(force: true); |
| 261 }); | 292 }); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 278 | 309 |
| 279 var request = new http.Request('POST', | 310 var request = new http.Request('POST', |
| 280 Uri.parse('http://localhost:$_serverPort/')); | 311 Uri.parse('http://localhost:$_serverPort/')); |
| 281 | 312 |
| 282 if (headers != null) request.headers.addAll(headers); | 313 if (headers != null) request.headers.addAll(headers); |
| 283 if (body != null) request.body = body; | 314 if (body != null) request.body = body; |
| 284 | 315 |
| 285 return request.send(); | 316 return request.send(); |
| 286 }); | 317 }); |
| 287 } | 318 } |
| OLD | NEW |