mock_http 0.3.0 copy "mock_http: ^0.3.0" to clipboard
mock_http: ^0.3.0 copied to clipboard

A library for easily and declaratively creating a mock HTTP client.

example/mock_http_example.dart

import 'package:http/http.dart';
import 'package:mock_http/mock_http.dart';
import 'package:mock_http/src/is_request.dart';
import 'package:test/test.dart';

class TestApi {
  Client client;

  TestApi(this.client);

  Future<String> googleSearch() async {
    var response = await client.get(Uri.https("google.com", ""));
    return response.body;
  }
}

void main() {
  test("allows mocking a response", () async {
    var googleSearchSpec = Endpoint(matchRequest: isRequest(url: isUri(host: "google.com")), func: (request) {
      return Response("Hello from Google.com!", 200);
    });
    var mock = MockHttp([
      googleSearchSpec
    ]);

    // Note that we wrap the test in mock.wrap. This resets the history of the mock.
    // This isn't necessary unless we want to reuse the same mock for multiple tests.
    await mock.wrap(() async {
      var api = TestApi(mock.client);
      expect(await api.googleSearch(), equals("Hello from Google.com!"));
      mock.expectSpec(googleSearchSpec);
    });
  });
}
1
likes
120
points
47
downloads

Publisher

verified publishersamw.dev

Weekly Downloads

A library for easily and declaratively creating a mock HTTP client.

Documentation

API reference

License

MIT (license)

Dependencies

http, indent, matcher, test

More

Packages that depend on mock_http