TestClient class final

Drives a Router through either an in-process handler or real HTTP.

// In-process — no socket, sub-millisecond.
final client = TestClient(router);

// Real HTTP on port 0 — catches wire-level bugs.
final client = await TestClient.serve(router);
final response = await (client.post('/orders')
    ..bearer('tok')
    ..json({'item': 'shirt', 'quantity': 2})
    ..expectSuccess())
    .send();
response
    ..assertCreated()
    ..assertJsonContains({'item': 'shirt'});

Constructors

TestClient(Router router, {bool saveCookies = false})
No socket — requests go through router.handler directly.
TestClient.origin(String origin, {bool saveCookies = false})
Connects to a server already running at origin.

Properties

hashCode → int
The hash code for this object.
no setterinherited
origin → String
The base URL of the running server. Throws in handler mode.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

close() → Future<void>
Stops the server (in serve mode) and closes the HTTP client.
delete(String path) → TestRequest
DELETE path.
expectFailure() → void
Every response must be non-2xx. Override per-request with TestRequest.expectSuccess.
expectSuccess() → void
Every response must be 2xx. Override per-request with TestRequest.expectFailure.
get(String path) → TestRequest
GET path.
HEAD path.
method(String verb, String path) → TestRequest
Arbitrary verb on path.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patch(String path) → TestRequest
PATCH path.
post(String path) → TestRequest
POST path.
put(String path) → TestRequest
PUT path.
toString() → String
A string representation of this object.
inherited

Operators

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

Static Methods

serve(Router router, {bool saveCookies = false}) → Future<TestClient>
Binds port 0 on loopback and drives with dart:io HttpClient.