expectJsonBody function
Dynamic structural check validating matching key-values inside raw JSON payloads.
Implementation
void expectJsonBody(RecordedRequest request, Map<String, dynamic> expectedJson) {
try {
final bodyNormalized = request.bodyAsJson;
if (bodyNormalized is! Map) {
throw TestFailure('Assertion Failed: Body payload is not a JSON Map structure.');
}
expectedJson.forEach((key, val) {
if (bodyNormalized[key] != val) {
throw TestFailure(
'Assertion Failed: JSON payload key mismatch.\n'
'Key: "$key"\n'
'Expected: $val\n'
'Received: ${bodyNormalized[key]}',
);
}
});
} catch (e) {
if (e is TestFailure) rethrow;
throw TestFailure('Assertion Failed: Failed to parse body payload as JSON: $e');
}
}