MockHTTPServer class

This class is used as a utility for testing.

Concrete implementations - like MockHTTPServer - are used to validate messages send to remote servers during testing. This allows your tests to verify any messages sent as a side-effect of an endpoint. For example, an application that has an endpoint that allows its user to associate their Nest account would use this class during testing. Instances of this class listen on localhost. You should be sure to close instances of this class during tearDown functions.

By default, any request made to an instance of this type will be responded to with a 200 and no HTTP body. You may add responses to instances of this class with queueResponse. They will be returned in the order they were provided in.

Example usages: test("Associate Nest account", () async { var nestMockServer = new MockHTTPServer(nestPort); await;

      // Expect that POST /nest/pair sends an HTTP request to Nest server.
      var response = await client.authenticatedRequest("/nest/pair", ...).post();
      expect(response, ...);

      // Verify the path of the HTTP request sent to Nest server.
      var requestSentToNest = await;
      expect(requestSentToNest .path, contains("${response["id"]}));

      await nestMockServer.close();

    test("Associate Nest account returns 503 when Nest is unreachable", () async {
      var nestMockServer = new MockHTTPServer(nestPort);

      var response = await client.authenticatedRequest("/nest/pair", ...).post();
      expect(response, hasStatus(503));

      await nestMockServer.close();


MockHTTPServer(int port)


defaultDelay Duration
The delay to be used for responses where a delay is not set [...]
read / write
defaultResponse Response
The response to be returned if there are no queued responses [...]
read / write
port int
The port to listen on.
read / write
queuedResponseCount int
The number of currently queued responses
server HttpServer
The underlying HttpServer listening for requests.
read / write
hashCode int
The hash code for this object. [...]
read-only, inherited
isEmpty bool
Whether or not there are any messages that have been sent to this instance but have yet to be read.
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited


close() Future
Shuts down the server listening for HTTP requests.
open() Future
Begins listening for HTTP requests on port.
queueResponse(Response resp, { Duration delay: null }) → void
Adds an HTTP response to the list of responses to be returned. [...]
add(MockHTTPRequest value) → void
Adds an event to this server.
clear() → void
next() Future<MockHTTPRequest>
Returns an event that has been added to this server. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
toString() String
Returns a string representation of this object.


operator ==(dynamic other) bool
The equality operator. [...]

Static Properties

mockConnectionFailureResponse Response
Used to simulate a failed request. [...]
read / write