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