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.test_util; | 5 library shelf.test_util; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:shelf/shelf.dart'; | 9 import 'package:shelf/shelf.dart'; |
10 import 'package:shelf/src/util.dart'; | 10 import 'package:shelf/src/util.dart'; |
11 | 11 |
12 // "hello," | 12 // "hello," |
13 const HELLO_BYTES = const [104, 101, 108, 108, 111, 44]; | 13 const HELLO_BYTES = const [104, 101, 108, 108, 111, 44]; |
14 | 14 |
15 // " world" | 15 // " world" |
16 const WORLD_BYTES = const [32, 119, 111, 114, 108, 100]; | 16 const WORLD_BYTES = const [32, 119, 111, 114, 108, 100]; |
17 | 17 |
18 /// A simple, synchronous handler for [Request]. | 18 /// A simple, synchronous handler for [Request]. |
19 /// | 19 /// |
20 /// By default, replies with a status code 200, empty headers, and | 20 /// By default, replies with a status code 200, empty headers, and |
21 /// `Hello from ${request.url.path}`. | 21 /// `Hello from ${request.url.path}`. |
22 Response syncHandler(Request request, | 22 Response syncHandler(Request request, |
23 {int statusCode, Map<String, String> headers}) { | 23 {int statusCode, Map<String, String> headers}) { |
24 if (statusCode == null) statusCode = 200; | 24 if (statusCode == null) statusCode = 200; |
25 return new Response(statusCode, | 25 return new Response(statusCode, |
26 headers: headers, body: 'Hello from ${request.url.path}'); | 26 headers: headers, body: 'Hello from ${request.requestedUri.path}'); |
27 } | 27 } |
28 | 28 |
29 /// Calls [syncHandler] and wraps the response in a [Future]. | 29 /// Calls [syncHandler] and wraps the response in a [Future]. |
30 Future<Response> asyncHandler(Request request) => | 30 Future<Response> asyncHandler(Request request) => |
31 new Future(() => syncHandler(request)); | 31 new Future(() => syncHandler(request)); |
32 | 32 |
33 /// Makes a simple GET request to [handler] and returns the result. | 33 /// Makes a simple GET request to [handler] and returns the result. |
34 Future<Response> makeSimpleRequest(Handler handler) => | 34 Future<Response> makeSimpleRequest(Handler handler) => |
35 syncFuture(() => handler(_request)); | 35 syncFuture(() => handler(_request)); |
36 | 36 |
37 final _request = new Request('GET', LOCALHOST_URI); | 37 final _request = new Request('GET', LOCALHOST_URI); |
38 | 38 |
39 final LOCALHOST_URI = Uri.parse('http://localhost/'); | 39 final LOCALHOST_URI = Uri.parse('http://localhost/'); |
OLD | NEW |