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) {
  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'
  });
};

Typedefs

MockClientHandler(Request request) → Future<Response>

A handler function that receives Requests and sends Responses. Note that request will be finalized.

MockClientStreamHandler(BaseRequest request, ByteStream bodyStream) → Future<StreamedResponse>

A handler function that receives StreamedRequests and sends StreamedResponses. Note that request will be finalized.

Classes

MockClient

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.