TestClient class
HTTP client for testing Chase applications.
Starts the app on an ephemeral port and sends real HTTP requests.
Example:
void main() {
late Chase app;
late TestClient client;
setUp(() async {
app = Chase();
app.get('/users/:id').handle((ctx) {
ctx.res.json({'id': ctx.req.params['id']});
});
client = await TestClient.start(app);
});
tearDown(() async {
await client.close();
});
test('GET /users/:id returns user', () async {
final res = await client.get('/users/123');
expect(res.status, 200);
expect(res.json['id'], '123');
});
}
- Available extensions
Properties
- 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> - Stops the server and closes the HTTP client.
-
delete(
String path, {Map< String, String> ? headers}) → Future<TestResponse> - Sends a DELETE request.
-
get(
String path, {Map< String, String> ? headers}) → Future<TestResponse> - Sends a GET request.
-
getWithAuth(
String path, String token, {Map< String, String> ? headers}) → Future<TestResponse> -
Available on TestClient, provided by the TestClientExtensions extension
Sends a GET request with Bearer authentication. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
patch(
String path, {Object? body, Map< String, String> ? headers}) → Future<TestResponse> - Sends a PATCH request.
-
patchJson(
String path, Object body, {String? token, Map< String, String> ? headers}) → Future<TestResponse> -
Available on TestClient, provided by the TestClientExtensions extension
Sends a PATCH request with JSON body and optional auth. -
post(
String path, {Object? body, Map< String, String> ? headers}) → Future<TestResponse> - Sends a POST request.
-
postJson(
String path, Object body, {String? token, Map< String, String> ? headers}) → Future<TestResponse> -
Available on TestClient, provided by the TestClientExtensions extension
Sends a POST request with JSON body and optional auth. -
postMultipart(
String path, {Map< String, String> ? fields, Map<String, MultipartFileData> ? files, Map<String, String> ? headers}) → Future<TestResponse> - Sends a POST request with multipart/form-data body.
-
put(
String path, {Object? body, Map< String, String> ? headers}) → Future<TestResponse> - Sends a PUT request.
-
putJson(
String path, Object body, {String? token, Map< String, String> ? headers}) → Future<TestResponse> -
Available on TestClient, provided by the TestClientExtensions extension
Sends a PUT request with JSON body and optional auth. -
request(
String method, String path, {Object? body, Map< String, String> ? headers}) → Future<TestResponse> - Sends a request with any HTTP method.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
start(
Chase app) → Future< TestClient> - Starts the app and creates a test client.