OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 mock_client; | 5 library mock_client; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import 'base_client.dart'; | 9 import 'base_client.dart'; |
10 import 'base_request.dart'; | 10 import 'base_request.dart'; |
11 import 'request.dart'; | 11 import 'request.dart'; |
12 import 'response.dart'; | 12 import 'response.dart'; |
13 import 'streamed_response.dart'; | 13 import 'streamed_response.dart'; |
14 import 'utils.dart'; | 14 import 'utils.dart'; |
15 | 15 |
16 // TODO(nweiz): once Dart has some sort of Rack- or WSGI-like standard for | 16 // TODO(nweiz): once Dart has some sort of Rack- or WSGI-like standard for |
17 // server APIs, MockClient should conform to it. | 17 // server APIs, MockClient should conform to it. |
18 | 18 |
19 /// A mock HTTP client designed for use when testing code that uses | 19 /// A mock HTTP client designed for use when testing code that uses |
20 /// [BaseClient]. This client allows you to define a handler callback for all | 20 /// [BaseClient]. This client allows you to define a handler callback for all |
21 /// requests that are made through it so that you can mock a server without | 21 /// requests that are made through it so that you can mock a server without |
22 /// having to send real HTTP requests. | 22 /// having to send real HTTP requests. |
23 class MockClient extends BaseClient { | 23 class MockClient extends BaseClient { |
24 /// The handler for receiving [StreamedRequest]s and sending | 24 /// The handler for receiving [StreamedRequest]s and sending |
25 /// [StreamedResponse]s. | 25 /// [StreamedResponse]s. |
26 final MockClientStreamHandler _handler; | 26 final MockClientStreamHandler _handler; |
27 | 27 |
| 28 MockClient._(this._handler); |
| 29 |
28 /// Creates a [MockClient] with a handler that receives [Request]s and sends | 30 /// Creates a [MockClient] with a handler that receives [Request]s and sends |
29 /// [Response]s. | 31 /// [Response]s. |
30 MockClient(MockClientHandler fn) | 32 MockClient(MockClientHandler fn) |
31 : this.streaming((baseRequest, bodyStream) { | 33 : this._((baseRequest, bodyStream) { |
32 return consumeInputStream(bodyStream).chain((bodyBytes) { | 34 return consumeInputStream(bodyStream).chain((bodyBytes) { |
33 var request = new Request(baseRequest.method, baseRequest.url); | 35 var request = new Request(baseRequest.method, baseRequest.url); |
34 request.persistentConnection = baseRequest.persistentConnection; | 36 request.persistentConnection = baseRequest.persistentConnection; |
35 request.followRedirects = baseRequest.followRedirects; | 37 request.followRedirects = baseRequest.followRedirects; |
36 request.maxRedirects = baseRequest.maxRedirects; | 38 request.maxRedirects = baseRequest.maxRedirects; |
37 mapAddAll(request.headers, baseRequest.headers); | 39 mapAddAll(request.headers, baseRequest.headers); |
38 request.bodyBytes = bodyBytes; | 40 request.bodyBytes = bodyBytes; |
39 request.finalize(); | 41 request.finalize(); |
40 | 42 |
41 return fn(request); | 43 return fn(request); |
42 }).transform((response) { | 44 }).transform((response) { |
43 var stream = new ListInputStream(); | 45 var stream = new ListInputStream(); |
44 stream.write(response.bodyBytes); | 46 stream.write(response.bodyBytes); |
45 stream.markEndOfStream(); | 47 stream.markEndOfStream(); |
46 | 48 |
47 return new StreamedResponse( | 49 return new StreamedResponse( |
48 stream, | 50 stream, |
49 response.statusCode, | 51 response.statusCode, |
50 response.contentLength, | 52 response.contentLength, |
| 53 request: baseRequest, |
51 headers: response.headers, | 54 headers: response.headers, |
52 isRedirect: response.isRedirect, | 55 isRedirect: response.isRedirect, |
53 persistentConnection: response.persistentConnection, | 56 persistentConnection: response.persistentConnection, |
54 reasonPhrase: response.reasonPhrase); | 57 reasonPhrase: response.reasonPhrase); |
55 }); | 58 }); |
56 }); | 59 }); |
57 | 60 |
58 /// Creates a [MockClient] with a handler that receives [StreamedRequest]s and | 61 /// Creates a [MockClient] with a handler that receives [StreamedRequest]s and |
59 /// sends [StreamedResponse]s. | 62 /// sends [StreamedResponse]s. |
60 MockClient.streaming(MockClientStreamHandler this._handler); | 63 MockClient.streaming(MockClientStreamHandler fn) |
| 64 : this._((request, bodyStream) { |
| 65 return fn(request, bodyStream).transform((response) { |
| 66 return new StreamedResponse( |
| 67 response.stream, |
| 68 response.statusCode, |
| 69 response.contentLength, |
| 70 request: request, |
| 71 headers: response.headers, |
| 72 isRedirect: response.isRedirect, |
| 73 persistentConnection: response.persistentConnection, |
| 74 reasonPhrase: response.reasonPhrase); |
| 75 }); |
| 76 }); |
61 | 77 |
62 /// Sends a request. | 78 /// Sends a request. |
63 Future<StreamedResponse> send(BaseRequest request) { | 79 Future<StreamedResponse> send(BaseRequest request) { |
64 var bodyStream = request.finalize(); | 80 var bodyStream = request.finalize(); |
65 return async.chain((_) => _handler(request, bodyStream)); | 81 return async.chain((_) => _handler(request, bodyStream)); |
66 } | 82 } |
67 } | 83 } |
68 | 84 |
69 /// A handler function that receives [StreamedRequest]s and sends | 85 /// A handler function that receives [StreamedRequest]s and sends |
70 /// [StreamedResponse]s. Note that [request] will be finalized. | 86 /// [StreamedResponse]s. Note that [request] will be finalized. |
71 typedef Future<StreamedResponse> MockClientStreamHandler( | 87 typedef Future<StreamedResponse> MockClientStreamHandler( |
72 BaseRequest request, InputStream bodyStream); | 88 BaseRequest request, InputStream bodyStream); |
73 | 89 |
74 /// A handler function that receives [Request]s and sends [Response]s. Note that | 90 /// A handler function that receives [Request]s and sends [Response]s. Note that |
75 /// [request] will be finalized. | 91 /// [request] will be finalized. |
76 typedef Future<Response> MockClientHandler(Request request); | 92 typedef Future<Response> MockClientHandler(Request request); |
OLD | NEW |