TestClient class
A client for testing HTTP endpoints with different transport modes.
This is the main entry point for testing HTTP-based applications. It provides methods for sending HTTP requests and handling responses with a fluent API. The client can operate in different transport modes, allowing tests to be run either in-memory (for speed) or using real HTTP servers (for integration testing).
Basic Usage
// Create a client with your request handler
final client = TestClient(yourRequestHandler);
// Send a GET request
final response = await client.get('/users');
// Make assertions on the response
response
.assertStatus(200)
.assertJsonPath('users.0.name', 'Alice');
Transport Modes
The client supports two transport modes:
- TransportMode.inMemory: Requests are handled in memory without network I/O
- TransportMode.ephemeralServer: A real HTTP server is started for testing
Multipart Requests
For file uploads and form submissions, use the multipart method:
final response = await client.multipart('/upload', (builder) {
builder.addField('description', 'Test file');
builder.addFileFromBytes(
name: 'file',
bytes: imageBytes,
filename: 'test.jpg',
contentType: MediaType('image', 'jpeg'),
);
});
Constructors
- TestClient(RequestHandler handler, {TransportMode mode = TransportMode.inMemory, TransportOptions? options})
-
Factory constructor to create an instance of TestClient.
factory
- TestClient.ephemeralServer(RequestHandler handler, [TransportOptions? options])
- Constructor for creating an ephemeral server transport client.
- TestClient.inMemory(RequestHandler handler, [TransportOptions? options])
- Constructor for creating an in-memory transport client.
Properties
-
baseUrlFuture
→ Future<
String> -
Sends a PUT request with a JSON body to the specified
uri.no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
close(
) → Future< void> - Closes the transport client, releasing any resources.
-
delete(
String uri, {Map< String, List< ? headers}) → Future<String> >TestResponse> -
Sends a DELETE request to the specified
uri. -
deleteJson(
String uri, {Map< String, List< ? headers}) → Future<String> >TestResponse> -
Sends a DELETE request to the specified
uriexpecting a JSON response. -
get(
String uri, {Map< String, List< ? headers}) → Future<String> >TestResponse> -
Sends a GET request to the specified
uri. -
getJson(
String uri, {Map< String, List< ? headers}) → Future<String> >TestResponse> -
Sends a GET request to the specified
uriexpecting a JSON response. -
head(
String uri, {Map< String, List< ? headers}) → Future<String> >TestResponse> -
Sends a HEAD request to the specified
uri. -
multipart(
String path, void builder(MultipartRequestBuilder)) → Future< TestResponse> -
Sends a multipart POST request to the specified
path. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
options(
String uri, {Map< String, List< ? headers, dynamic body}) → Future<String> >TestResponse> -
Sends an OPTIONS request to the specified
uri. -
patch(
String uri, dynamic body, {Map< String, List< ? headers}) → Future<String> >TestResponse> -
Sends a PATCH request to the specified
uri. -
patchJson(
String uri, dynamic body, {Map< String, List< ? headers}) → Future<String> >TestResponse> -
Sends a PATCH request with a JSON body to the specified
uri. -
post(
String uri, dynamic body, {Map< String, List< ? headers}) → Future<String> >TestResponse> -
Sends a POST request to the specified
uri. -
postJson(
String uri, dynamic body, {Map< String, List< ? headers}) → Future<String> >TestResponse> -
Sends a POST request with a JSON body to the specified
uri. -
put(
String uri, dynamic body, {Map< String, List< ? headers}) → Future<String> >TestResponse> -
Sends a PUT request to the specified
uri. -
putJson(
String uri, dynamic body, {Map< String, List< ? headers}) → Future<String> >TestResponse> - Example:
-
request(
String method, String uri, [Map< String, List< ? headers]) → Future<String> >TestResponse> -
Sends a request with the specified
methodto the specifieduri. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited