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:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'base_client.dart'; | 9 import 'base_client.dart'; |
10 import 'base_request.dart'; | 10 import 'base_request.dart'; |
(...skipping 15 matching lines...) Expand all Loading... |
26 /// [StreamedResponse]s. | 26 /// [StreamedResponse]s. |
27 final MockClientStreamHandler _handler; | 27 final MockClientStreamHandler _handler; |
28 | 28 |
29 MockClient._(this._handler); | 29 MockClient._(this._handler); |
30 | 30 |
31 /// Creates a [MockClient] with a handler that receives [Request]s and sends | 31 /// Creates a [MockClient] with a handler that receives [Request]s and sends |
32 /// [Response]s. | 32 /// [Response]s. |
33 MockClient(MockClientHandler fn) | 33 MockClient(MockClientHandler fn) |
34 : this._((baseRequest, bodyStream) { | 34 : this._((baseRequest, bodyStream) { |
35 return bodyStream.toBytes().then((bodyBytes) { | 35 return bodyStream.toBytes().then((bodyBytes) { |
36 var request = new Request(baseRequest.method, baseRequest.url); | 36 var request = new Request(baseRequest.method, baseRequest.url) |
37 request.persistentConnection = baseRequest.persistentConnection; | 37 ..persistentConnection = baseRequest.persistentConnection |
38 request.followRedirects = baseRequest.followRedirects; | 38 ..followRedirects = baseRequest.followRedirects |
39 request.maxRedirects = baseRequest.maxRedirects; | 39 ..maxRedirects = baseRequest.maxRedirects |
40 request.headers.addAll(baseRequest.headers); | 40 ..headers.addAll(baseRequest.headers) |
41 request.bodyBytes = bodyBytes; | 41 ..bodyBytes = bodyBytes |
42 request.finalize(); | 42 ..finalize(); |
43 | 43 |
44 return fn(request); | 44 return fn(request); |
45 }).then((response) { | 45 }).then((response) { |
46 return new StreamedResponse( | 46 return new StreamedResponse( |
47 new ByteStream.fromBytes(response.bodyBytes), | 47 new ByteStream.fromBytes(response.bodyBytes), |
48 response.statusCode, | 48 response.statusCode, |
49 response.contentLength, | 49 response.contentLength, |
50 request: baseRequest, | 50 request: baseRequest, |
51 headers: response.headers, | 51 headers: response.headers, |
52 isRedirect: response.isRedirect, | 52 isRedirect: response.isRedirect, |
(...skipping 27 matching lines...) Expand all Loading... |
80 } | 80 } |
81 | 81 |
82 /// A handler function that receives [StreamedRequest]s and sends | 82 /// A handler function that receives [StreamedRequest]s and sends |
83 /// [StreamedResponse]s. Note that [request] will be finalized. | 83 /// [StreamedResponse]s. Note that [request] will be finalized. |
84 typedef Future<StreamedResponse> MockClientStreamHandler( | 84 typedef Future<StreamedResponse> MockClientStreamHandler( |
85 BaseRequest request, ByteStream bodyStream); | 85 BaseRequest request, ByteStream bodyStream); |
86 | 86 |
87 /// A handler function that receives [Request]s and sends [Response]s. Note that | 87 /// A handler function that receives [Request]s and sends [Response]s. Note that |
88 /// [request] will be finalized. | 88 /// [request] will be finalized. |
89 typedef Future<Response> MockClientHandler(Request request); | 89 typedef Future<Response> MockClientHandler(Request request); |
OLD | NEW |