assertJsonPath method

TestResponse assertJsonPath(
  1. String path,
  2. Object? expected
)

Implementation

TestResponse assertJsonPath(String path, Object? expected) {
  dynamic node = json;
  for (final segment in path.split('.')) {
    if (node is Map && node.containsKey(segment)) {
      node = node[segment];
    } else if (node is List &&
        int.tryParse(segment) != null &&
        int.parse(segment) < node.length) {
      node = node[int.parse(segment)];
    } else {
      throw TestClientAssertionError('JSON path [$path] not found in $body');
    }
  }
  return _check(
    _deepEquals(node, expected),
    'JSON path [$path] expected ${jsonEncode(expected)} but got ${jsonEncode(node)}',
  );
}