TestResponse class
Test response wrapper for HTTP responses with convenient assertion methods.
Provides a comprehensive interface for examining and asserting on HTTP responses in tests. Includes helpers for common response patterns, content type checking, and cookie handling.
Example:
void main() {
test('API returns user data', () async {
final response = await testApp.get('/api/users/123').send();
expect(response.isSuccess, true);
expect(response.statusCode, 200);
expect(response.isJson, true);
final user = response.json;
expect(user['id'], 123);
expect(user['name'], 'John Doe');
});
test('Error handling', () async {
final response = await testApp.get('/api/users/999').send();
expect(response.isClientError, true);
expect(response.statusCode, 404);
expect(response.json['error'], 'User not found');
});
test('Cookie handling', () async {
final response = await testApp.post('/api/login')
.form({'username': 'user', 'password': 'pass'})
.send();
expect(response.isSuccess, true);
final sessionCookie = response.cookie('session_id');
expect(sessionCookie, isNotNull);
});
}
Features:
- Status code classification (success, error, redirect)
- Content type detection and helpers
- JSON parsing with error handling
- Cookie extraction and management
- Header access methods
- Response body content access
Constructors
- TestResponse(HttpClientResponse _response, String body)
- Creates a test response wrapper around an HTTP response.
Properties
- body → String
-
The complete response body as a string.
final
- contentLength → int?
-
Get response Content-Length in bytes.
no setter
- contentType → ContentType?
-
Get the parsed Content-Type header.
no setter
-
Get all cookies from Set-Cookie headers.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- headers → HttpHeaders
-
All HTTP headers from the response.
no setter
- isClientError → bool
-
Check if response indicates a client error (4xx status codes).
no setter
- isHtml → bool
-
Check if response Content-Type is HTML.
no setter
- isJson → bool
-
Check if response Content-Type is JSON.
no setter
- isRedirect → bool
-
Check if response indicates a redirect (3xx status codes).
no setter
- isServerError → bool
-
Check if response indicates a server error (5xx status codes).
no setter
- isSuccess → bool
-
Check if response indicates success (2xx status codes).
no setter
- isText → bool
-
Check if response Content-Type is any text type.
no setter
- json → dynamic
-
Parse response body as JSON.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- statusCode → int
-
HTTP status code of the response.
no setter
- text → String
-
Get response body as raw text.
no setter
Methods
- Get a specific cookie by name from the response.
-
hasStatus(
int expectedStatusCode) → bool - Check if response has a specific status code.
-
header(
String name) → String? - Get the value of a specific response header.
-
headerValues(
String name) → List< String> ? - Get all values for a response header that may have multiple values.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
String representation of the test response.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited