fake_http_client 0.2.0
fake_http_client: ^0.2.0 copied to clipboard

A package for faking Dart/Flutter HttpClient's responses. It is handy for integration tests. Also it may help a lot during app development with an unstable back-end.

fake_http_client #

A package for faking Dart/Flutter HttpClient's responses.

It is handy for integration tests. Also it may help a lot during app development with an unstable back-end.

Example #

The following example forces all HTTP requests to return a successful empty response in a test environment. No actual HTTP requests will be performed.

class _EmptyResponseHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(_) {
    return FakeHttpClient((request, client) {
      // The default response is an empty 200.
      return FakeHttpResponse();
    });
  }
}

void main() {
  group('HttpClient', () {
    setUp(() {
      // Overrides all HttpClients.
      HttpOverrides.global = _EmptyResponseHttpOverrides();
    });

    test('returns faked OK empty response for non-existing website', () async {
      // This is actually an instance of [FakeHttpClient].
      final client = HttpClient();
      final request = await client.getUrl(Uri.parse('https://non-existing-site.com'));
      final response = await request.close();

      expect(response.statusCode, HttpStatus.ok);
      expect(response.contentLength, 0);
    });
  });
}
2
likes
120
pub points
47%
popularity

Publisher

pasul.dev

A package for faking Dart/Flutter HttpClient's responses. It is handy for integration tests. Also it may help a lot during app development with an unstable back-end.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

More

Packages that depend on fake_http_client