Fixed hasValidBody exception
This commit is contained in:
parent
1496d61a1c
commit
e657b2070d
5 changed files with 93 additions and 16 deletions
|
@ -1,5 +1,11 @@
|
||||||
# Change Log
|
# Change Log
|
||||||
|
|
||||||
|
## 8.0.1
|
||||||
|
|
||||||
|
* Added `describeMismatch` to `hasStatus`
|
||||||
|
* Added `describeMismatch` to `hasContentType`
|
||||||
|
* Added `describeMismatch` to `hasValidBody`
|
||||||
|
|
||||||
## 8.0.0
|
## 8.0.0
|
||||||
|
|
||||||
* Require Dart >= 3.0
|
* Require Dart >= 3.0
|
||||||
|
|
|
@ -41,6 +41,8 @@ Matcher hasStatus(int status) => _HasStatus(status);
|
||||||
/// Expects a response to have a JSON body that is a `Map` and satisfies the given [validator] schema.
|
/// Expects a response to have a JSON body that is a `Map` and satisfies the given [validator] schema.
|
||||||
Matcher hasValidBody(Validator validator) => _HasValidBody(validator);
|
Matcher hasValidBody(Validator validator) => _HasValidBody(validator);
|
||||||
|
|
||||||
|
String notHttpResponse = "expected http.Response but got none\n";
|
||||||
|
|
||||||
class _IsJson extends Matcher {
|
class _IsJson extends Matcher {
|
||||||
dynamic value;
|
dynamic value;
|
||||||
|
|
||||||
|
@ -97,19 +99,35 @@ class _HasContentType extends Matcher {
|
||||||
@override
|
@override
|
||||||
bool matches(item, Map matchState) {
|
bool matches(item, Map matchState) {
|
||||||
if (item is http.Response) {
|
if (item is http.Response) {
|
||||||
if (!item.headers.containsKey('content-type')) return false;
|
//if (!item.headers.containsKey('content-type')) return false;
|
||||||
|
|
||||||
|
var headerContentType = item.headers['content-type'];
|
||||||
|
if (headerContentType == null) return false;
|
||||||
|
|
||||||
if (contentType is ContentType) {
|
if (contentType is ContentType) {
|
||||||
var compare = ContentType.parse(item.headers['content-type']!);
|
var compare = ContentType.parse(headerContentType);
|
||||||
return equals(contentType.mimeType)
|
return equals(contentType.mimeType)
|
||||||
.matches(compare.mimeType, matchState);
|
.matches(compare.mimeType, matchState);
|
||||||
} else {
|
} else {
|
||||||
return equals(contentType.toString())
|
return equals(contentType.toString())
|
||||||
.matches(item.headers['content-type'], matchState);
|
.matches(headerContentType, matchState);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Description describeMismatch(Object? item, Description mismatchDescription,
|
||||||
|
Map matchState, bool verbose) {
|
||||||
|
if (item is http.Response) {
|
||||||
|
var headerContentType = item.headers['content-type'] ?? 'none';
|
||||||
|
mismatchDescription
|
||||||
|
.add("expected '$contentType' but got '$headerContentType'\n");
|
||||||
|
} else {
|
||||||
|
mismatchDescription.add(notHttpResponse);
|
||||||
|
}
|
||||||
|
return mismatchDescription;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,6 +163,17 @@ class _HasHeader extends Matcher {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Description describeMismatch(Object? item, Description mismatchDescription,
|
||||||
|
Map matchState, bool verbose) {
|
||||||
|
if (item is http.Response) {
|
||||||
|
mismatchDescription.add("expected '$key' but got none\n");
|
||||||
|
} else {
|
||||||
|
mismatchDescription.add(notHttpResponse);
|
||||||
|
}
|
||||||
|
return mismatchDescription;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _HasStatus extends Matcher {
|
class _HasStatus extends Matcher {
|
||||||
|
@ -161,11 +190,24 @@ class _HasStatus extends Matcher {
|
||||||
bool matches(item, Map matchState) =>
|
bool matches(item, Map matchState) =>
|
||||||
item is http.Response &&
|
item is http.Response &&
|
||||||
equals(status).matches(item.statusCode, matchState);
|
equals(status).matches(item.statusCode, matchState);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Description describeMismatch(Object? item, Description mismatchDescription,
|
||||||
|
Map matchState, bool verbose) {
|
||||||
|
if (item is http.Response) {
|
||||||
|
mismatchDescription.add('expected $status but got ${item.statusCode}\n');
|
||||||
|
} else {
|
||||||
|
mismatchDescription.add(notHttpResponse);
|
||||||
|
}
|
||||||
|
return mismatchDescription;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _HasValidBody extends Matcher {
|
class _HasValidBody extends Matcher {
|
||||||
final Validator validator;
|
final Validator validator;
|
||||||
|
|
||||||
|
final _errors = <String>[];
|
||||||
|
|
||||||
_HasValidBody(this.validator);
|
_HasValidBody(this.validator);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -176,11 +218,32 @@ class _HasValidBody extends Matcher {
|
||||||
bool matches(item, Map matchState) {
|
bool matches(item, Map matchState) {
|
||||||
if (item is http.Response) {
|
if (item is http.Response) {
|
||||||
final jsons = json.decode(item.body);
|
final jsons = json.decode(item.body);
|
||||||
if (jsons is! Map) return false;
|
if (jsons is Map) {
|
||||||
return validator.matches(jsons, matchState);
|
try {
|
||||||
} else {
|
return validator.matches(jsons, matchState);
|
||||||
return false;
|
} catch (e) {
|
||||||
|
_errors.addAll((e as ValidationException).errors);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Description describeMismatch(Object? item, Description mismatchDescription,
|
||||||
|
Map matchState, bool verbose) {
|
||||||
|
if (item is http.Response) {
|
||||||
|
if (_errors.isEmpty) {
|
||||||
|
mismatchDescription.add("expected JSON but got invalid JSON\n");
|
||||||
|
} else {
|
||||||
|
for (var err in _errors) {
|
||||||
|
mismatchDescription.add("$err\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mismatchDescription.add(notHttpResponse);
|
||||||
|
}
|
||||||
|
return mismatchDescription;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
name: angel3_test
|
name: angel3_test
|
||||||
version: 8.0.0
|
version: 8.0.1
|
||||||
description: Testing utility library for the Angel3 framework. Use with package:test.
|
description: Testing utility library for the Angel3 framework. Use with package:test.
|
||||||
homepage: https://angel3-framework.web.app/
|
homepage: https://angel3-framework.web.app/
|
||||||
repository: https://github.com/dukefirehawk/angel/tree/master/packages/test
|
repository: https://github.com/dukefirehawk/angel/tree/master/packages/test
|
||||||
|
@ -19,9 +19,9 @@ dependencies:
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
test: ^1.24.0
|
test: ^1.24.0
|
||||||
lints: ^2.1.0
|
lints: ^2.1.0
|
||||||
# dependency_overrides:
|
dependency_overrides:
|
||||||
# angel3_container:
|
angel3_validate:
|
||||||
# path: ../container/angel_container
|
path: ../validate
|
||||||
# angel3_framework:
|
# angel3_framework:
|
||||||
# path: ../framework
|
# path: ../framework
|
||||||
# angel3_http_exception:
|
# angel3_http_exception:
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
## 8.0.1
|
## 8.0.1
|
||||||
|
|
||||||
* Fixed null check throwing exception
|
* Fixed missing mismatch errors
|
||||||
|
|
||||||
## 8.0.0
|
## 8.0.0
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,9 @@ class Validator extends Matcher {
|
||||||
/// Fields that must be present for data to be considered valid.
|
/// Fields that must be present for data to be considered valid.
|
||||||
final List<String> requiredFields = [];
|
final List<String> requiredFields = [];
|
||||||
|
|
||||||
|
/// Validation error messages.
|
||||||
|
final List<String> errorMessages = [];
|
||||||
|
|
||||||
void _importSchema(Map<String, dynamic> schema) {
|
void _importSchema(Map<String, dynamic> schema) {
|
||||||
for (var keys in schema.keys) {
|
for (var keys in schema.keys) {
|
||||||
for (var key in keys.split(',').map((s) => s.trim())) {
|
for (var key in keys.split(',').map((s) => s.trim())) {
|
||||||
|
@ -205,7 +208,12 @@ class Validator extends Matcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
errors.add(e.toString());
|
if (e is ValidationException) {
|
||||||
|
errors.add(e.errors.first);
|
||||||
|
} else {
|
||||||
|
errors.add(e.toString());
|
||||||
|
}
|
||||||
|
|
||||||
valid = false;
|
valid = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -401,7 +409,7 @@ class ValidationException extends AngelHttpException {
|
||||||
statusCode: 400,
|
statusCode: 400,
|
||||||
errors: (errors).toSet().toList(),
|
errors: (errors).toSet().toList(),
|
||||||
stackTrace: StackTrace.current) {
|
stackTrace: StackTrace.current) {
|
||||||
this.errors.addAll(errors.toSet());
|
//this.errors.addAll(errors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
Loading…
Reference in a new issue