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 28 matching lines...) Expand all Loading... |
39 ..maxRedirects = baseRequest.maxRedirects | 39 ..maxRedirects = baseRequest.maxRedirects |
40 ..headers.addAll(baseRequest.headers) | 40 ..headers.addAll(baseRequest.headers) |
41 ..bodyBytes = bodyBytes | 41 ..bodyBytes = bodyBytes |
42 ..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 contentLength: response.contentLength, |
50 request: baseRequest, | 50 request: baseRequest, |
51 headers: response.headers, | 51 headers: response.headers, |
52 isRedirect: response.isRedirect, | 52 isRedirect: response.isRedirect, |
53 persistentConnection: response.persistentConnection, | 53 persistentConnection: response.persistentConnection, |
54 reasonPhrase: response.reasonPhrase); | 54 reasonPhrase: response.reasonPhrase); |
55 }); | 55 }); |
56 }); | 56 }); |
57 | 57 |
58 /// Creates a [MockClient] with a handler that receives [StreamedRequest]s and | 58 /// Creates a [MockClient] with a handler that receives [StreamedRequest]s and |
59 /// sends [StreamedResponse]s. | 59 /// sends [StreamedResponse]s. |
60 MockClient.streaming(MockClientStreamHandler fn) | 60 MockClient.streaming(MockClientStreamHandler fn) |
61 : this._((request, bodyStream) { | 61 : this._((request, bodyStream) { |
62 return fn(request, bodyStream).then((response) { | 62 return fn(request, bodyStream).then((response) { |
63 return new StreamedResponse( | 63 return new StreamedResponse( |
64 response.stream, | 64 response.stream, |
65 response.statusCode, | 65 response.statusCode, |
66 response.contentLength, | 66 contentLength: response.contentLength, |
67 request: request, | 67 request: request, |
68 headers: response.headers, | 68 headers: response.headers, |
69 isRedirect: response.isRedirect, | 69 isRedirect: response.isRedirect, |
70 persistentConnection: response.persistentConnection, | 70 persistentConnection: response.persistentConnection, |
71 reasonPhrase: response.reasonPhrase); | 71 reasonPhrase: response.reasonPhrase); |
72 }); | 72 }); |
73 }); | 73 }); |
74 | 74 |
75 /// Sends a request. | 75 /// Sends a request. |
76 Future<StreamedResponse> send(BaseRequest request) { | 76 Future<StreamedResponse> send(BaseRequest request) { |
77 var bodyStream = request.finalize(); | 77 var bodyStream = request.finalize(); |
78 return async.then((_) => _handler(request, bodyStream)); | 78 return async.then((_) => _handler(request, bodyStream)); |
79 } | 79 } |
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 |