MockHTTPServer class Null safety

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:' and instantiate an mock HTTP server with that port.

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
read / write
defaultResponse ↔ Response
The response to be returned if there are no queued responses
read / write
hashCode int
The hash code for this object.
read-onlyinherited
isEmpty bool
Whether or not there are any messages that have been sent to this instance but have yet to be read.
read-onlyinherited
port int
The port to listen on.
read / write
queuedResponseCount int
The number of currently queued responses
read-only
runtimeType Type
A representation of the runtime type of the object.
read-onlyinherited
server HttpServer
The underlying HttpServer listening for requests.
read / write

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 non-existent 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