expectStatus method
Send request and assert both status code and optional JSON content.
Convenience method that combines status code and JSON validation. First checks the status code, then optionally validates JSON content.
Example:
// Just check status
await testApp.get('/health').expectStatus(200);
// Check status and JSON
await testApp.post('/api/users')
.json({'name': 'John'})
.expectStatus(201, {
'id': 123,
'name': 'John',
'created': true
});
statusCode Expected HTTP status code.
json Optional expected JSON structure.
Returns the TestResponse if all assertions pass.
Throws TestFailure if status or JSON doesn't match.
Implementation
Future<TestResponse> expectStatus(int statusCode,
[Map<String, dynamic>? json]) async {
final response = await expect(statusCode);
if (json != null) {
try {
final actual = jsonDecode(response.body) as Map<String, dynamic>;
if (!_deepEquals(actual, json)) {
throw TestFailure(
'JSON mismatch:\nExpected: $json\nActual: $actual',
);
}
} catch (e) {
if (e is TestFailure) rethrow;
throw TestFailure('Failed to parse response as JSON: $e');
}
}
return response;
}