MockHTTPServer class

This class is used as a 'mock' implementation of another HTTP server.

Create instances of these types during testing to simulate responses for HTTP requests your application makes to another server. All requests your application makes to another server will be sent to this object. Your tests can then verify the correct request was sent and your application's behavior can be validated against the possible responses from the other server.

An instance of this type listens on 'localhost' on port. Your application should use configuration values to provide the base URL and port of another server. During testing, your application should use the base URL 'http://localhost:

By default, an instance of this type returns a 503 error response, indicating that the service can't be reached. Different responses can be returned via defaultResponse, queueResponse, queueHandler, and queueOutage.

Example usages: test("POST /nest/pair associates account with Nest service", () async { var nestMockServer = MockHTTPServer(7777); await nestMockServer.open();

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

      // Verify the path of the HTTP request sent to Nest server.
      var requestSentToNest = await nestMockServer.next();
      expect(requestSentToNest.path.segments[1, response["id"]);

      await nestMockServer.close();
    });
Inheritance

Constructors

MockHTTPServer(int port)

Properties

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

Methods

add(Request value) → void
Adds an event to this server.
inherited
clear() → void
inherited
close() Future?
Shuts down the server listening for HTTP requests.
override
next() Future<Request>
Returns an event that has been added to this server.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
open() Future
Begins listening for HTTP requests on port.
override
queueHandler(Response handler(Request request), {Duration? delay}) → void
Enqueues a function that creates a response for the next request.
queueOutage({int count = 1}) → void
Enqueues an outage; the next request will not receive a response.
queueResponse(Response resp, {Duration? delay}) → void
Enqueues a response for the next request.
toString() String
A string representation of this object.
inherited

Operators

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