From fe12fc72ff9f79b1e3bed4c1d9acdb00c788d201 Mon Sep 17 00:00:00 2001 From: Don Date: Fri, 23 Jun 2017 13:59:28 -0700 Subject: [PATCH] Remove MockClient --- lib/src/mock_client.dart | 86 ---------------------------------------- lib/testing.dart | 25 ------------ 2 files changed, 111 deletions(-) delete mode 100644 lib/src/mock_client.dart delete mode 100644 lib/testing.dart diff --git a/lib/src/mock_client.dart b/lib/src/mock_client.dart deleted file mode 100644 index acda5fde7a..0000000000 --- a/lib/src/mock_client.dart +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -import 'dart:async'; - -import 'base_client.dart'; -import 'base_request.dart'; -import 'byte_stream.dart'; -import 'request.dart'; -import 'response.dart'; -import 'streamed_response.dart'; - -// TODO(nweiz): once Dart has some sort of Rack- or WSGI-like standard for -// server APIs, MockClient should conform to it. - -/// A mock HTTP client designed for use when testing code that uses -/// [BaseClient]. This client allows you to define a handler callback for all -/// requests that are made through it so that you can mock a server without -/// having to send real HTTP requests. -class MockClient extends BaseClient { - /// The handler for receiving [StreamedRequest]s and sending - /// [StreamedResponse]s. - final MockClientStreamHandler _handler; - - MockClient._(this._handler); - - /// Creates a [MockClient] with a handler that receives [Request]s and sends - /// [Response]s. - MockClient(MockClientHandler fn) - : this._((baseRequest, bodyStream) { - return bodyStream.toBytes().then((bodyBytes) { - var request = new Request(baseRequest.method, baseRequest.url) - ..persistentConnection = baseRequest.persistentConnection - ..followRedirects = baseRequest.followRedirects - ..maxRedirects = baseRequest.maxRedirects - ..headers.addAll(baseRequest.headers) - ..bodyBytes = bodyBytes - ..finalize(); - - return fn(request); - }).then((response) { - return new StreamedResponse( - new ByteStream.fromBytes(response.bodyBytes), - response.statusCode, - contentLength: response.contentLength, - request: baseRequest, - headers: response.headers, - isRedirect: response.isRedirect, - persistentConnection: response.persistentConnection, - reasonPhrase: response.reasonPhrase); - }); - }); - - /// Creates a [MockClient] with a handler that receives [StreamedRequest]s and - /// sends [StreamedResponse]s. - MockClient.streaming(MockClientStreamHandler fn) - : this._((request, bodyStream) { - return fn(request, bodyStream).then((response) { - return new StreamedResponse( - response.stream, - response.statusCode, - contentLength: response.contentLength, - request: request, - headers: response.headers, - isRedirect: response.isRedirect, - persistentConnection: response.persistentConnection, - reasonPhrase: response.reasonPhrase); - }); - }); - - /// Sends a request. - Future send(BaseRequest request) async { - var bodyStream = request.finalize(); - return await _handler(request, bodyStream); - } -} - -/// A handler function that receives [StreamedRequest]s and sends -/// [StreamedResponse]s. Note that [request] will be finalized. -typedef Future MockClientStreamHandler( - BaseRequest request, ByteStream bodyStream); - -/// A handler function that receives [Request]s and sends [Response]s. Note that -/// [request] will be finalized. -typedef Future MockClientHandler(Request request); diff --git a/lib/testing.dart b/lib/testing.dart deleted file mode 100644 index d5a7874201..0000000000 --- a/lib/testing.dart +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -/// This library contains testing classes for the HTTP library. -/// -/// The [MockClient] class is a drop-in replacement for `http.Client` that -/// allows test code to set up a local request handler in order to fake a server -/// that responds to HTTP requests: -/// -/// import 'dart:convert'; -/// import 'package:http/testing.dart'; -/// -/// var client = new MockClient((request) async { -/// if (request.url.path != "/data.json") { -/// return new Response("", 404); -/// } -/// return new Response( -/// JSON.encode({ -/// 'numbers': [1, 4, 15, 19, 214] -/// }), -/// 200, -/// headers: {'content-type': 'application/json'}); -/// }); -export 'src/mock_client.dart';