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.handlerdirectly. - TestClient.origin(String origin, {bool saveCookies = false})
-
Connects to a server already running at
origin.
Properties
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(
String path) → TestRequest -
HEAD
path. -
method(
String verb, String path) → TestRequest -
Arbitrary
verbonpath. -
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:ioHttpClient.