FakeHttpClient class

A fake HttpClient for testing Flutter or Dart VM applications.

Using HttpOverrides.global and an FakeHttpClient, you can test code which uses HttpClient() without dependency injection. All you need to do is create a test client and specify how you want it to respond using a RequestCallback.

Currently the test client only supports the following HTTP methods:

Any of the non *Url methods will throw. The other members from the http client can be read in the RequestCallback but won't be used otherwise. Currently close, addCredentials and addProxyCredentials do nothing.

The following example forces all HTTP requests to return a successful empty response.

class MyHttpOverrides extends HttpOverrides {
  HttpClient() createClient(_) {
    return FakeHttpClient((HttpRequest request, FakeHttpClient client) {
      return FakeHttpResponse();
    });
  }
}

void main() {
  // overrides all HttpClients.
  HttpOverrides.global = MyHttpOverrides();

  group('Widget tests', () {
    test('returns 200', () async {
       // this is actually an instance of [FakeHttpClient].
       final client = HttpClient();
       final request = client.getUrl(Uri.https('google.com'));
       final response = await request.close();

       expect(response.statusCode, HttpStatus.ok);
    });
  });
}

If you don't want to override all HttpClients, you can also use HttpOverrides.runZoned. Anything which executes in the provided callback will use the provided http client.

See also:

Implemented types

Constructors

FakeHttpClient(RequestCallback _requestCallback)

Properties

authenticate AuthenticateCallback?
getter/setter pairoverride-setter
authenticateProxy AuthenticateProxyCallback?
getter/setter pairoverride-setter
autoUncompress bool
Gets and sets whether the body of a response will be automatically uncompressed.
getter/setter pairoverride
badCertificateCallback BadCertificateCallback?
getter/setter pairoverride-setter
connectionFactory ← (Future<ConnectionTask<Socket>> Function(Uri url, String? proxyHost, int? proxyPort)?)
Sets the function used to create socket connections.
no getteroverride
connectionTimeout Duration?
Gets and sets the connection timeout.
getter/setter pairoverride
findProxy FindProxyCallback?
getter/setter pairoverride-setter
hashCode int
The hash code for this object.
no setterinherited
idleTimeout Duration
Gets and sets the idle timeout of non-active persistent (keep-alive) connections.
getter/setter pairoverride
keyLog ← (dynamic Function(String line)?)
Sets a callback that will be called when new TLS keys are exchanged with the server. It will receive one line of text in NSS Key Log Format for each call. Writing these lines to a file will allow tools (such as Wireshark) to decrypt communication between the this client and the server. This is meant to allow network-level debugging of secure sockets and should not be used in production code. For example:
no getteroverride
maxConnectionsPerHost int?
Gets and sets the maximum number of live connections, to a single host.
getter/setter pairoverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
userAgent String?
Gets and sets the default value of the User-Agent header for all requests generated by this HttpClient.
getter/setter pairoverride

Methods

addCredentials(Uri url, String realm, HttpClientCredentials credentials) → void
Add credentials to be used for authorizing HTTP requests.
override
addProxyCredentials(String host, int port, String realm, HttpClientCredentials credentials) → void
Add credentials to be used for authorizing HTTP proxies.
override
close({bool force = false}) → void
Shuts down the HTTP client.
override
delete(String host, int port, String path) Future<HttpClientRequest>
Opens a HTTP connection using the DELETE method.
override
deleteUrl(Uri url) Future<HttpClientRequest>
Opens a HTTP connection using the DELETE method.
override
get(String host, int port, String path) Future<HttpClientRequest>
Opens a HTTP connection using the GET method.
override
getUrl(Uri url) Future<HttpClientRequest>
Opens a HTTP connection using the GET method.
override
Opens a HTTP connection using the HEAD method.
override
headUrl(Uri url) Future<HttpClientRequest>
Opens a HTTP connection using the HEAD method.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
open(String method, String host, int port, String path) Future<HttpClientRequest>
Opens a HTTP connection.
override
openUrl(String method, Uri url) Future<HttpClientRequest>
Opens a HTTP connection.
override
patch(String host, int port, String path) Future<HttpClientRequest>
Opens a HTTP connection using the PATCH method.
override
patchUrl(Uri url) Future<HttpClientRequest>
Opens a HTTP connection using the PATCH method.
override
post(String host, int port, String path) Future<HttpClientRequest>
Opens a HTTP connection using the POST method.
override
postUrl(Uri url) Future<HttpClientRequest>
Opens a HTTP connection using the POST method.
override
put(String host, int port, String path) Future<HttpClientRequest>
Opens a HTTP connection using the PUT method.
override
putUrl(Uri url) Future<HttpClientRequest>
Opens a HTTP connection using the PUT method.
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited