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