From e657b2070d1b33633d5ec287cd6923c5e7151908 Mon Sep 17 00:00:00 2001 From: "thomashii@dukefirehawk.com" Date: Sat, 4 Nov 2023 10:18:44 +0800 Subject: [PATCH] Fixed hasValidBody exception --- packages/test/CHANGELOG.md | 6 ++ packages/test/lib/src/matchers.dart | 81 +++++++++++++++++++++--- packages/test/pubspec.yaml | 8 +-- packages/validate/CHANGELOG.md | 2 +- packages/validate/lib/src/validator.dart | 12 +++- 5 files changed, 93 insertions(+), 16 deletions(-) diff --git a/packages/test/CHANGELOG.md b/packages/test/CHANGELOG.md index 462cbed6..e0abebd7 100644 --- a/packages/test/CHANGELOG.md +++ b/packages/test/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## 8.0.1 + +* Added `describeMismatch` to `hasStatus` +* Added `describeMismatch` to `hasContentType` +* Added `describeMismatch` to `hasValidBody` + ## 8.0.0 * Require Dart >= 3.0 diff --git a/packages/test/lib/src/matchers.dart b/packages/test/lib/src/matchers.dart index 7334caab..15627247 100644 --- a/packages/test/lib/src/matchers.dart +++ b/packages/test/lib/src/matchers.dart @@ -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. Matcher hasValidBody(Validator validator) => _HasValidBody(validator); +String notHttpResponse = "expected http.Response but got none\n"; + class _IsJson extends Matcher { dynamic value; @@ -97,19 +99,35 @@ class _HasContentType extends Matcher { @override bool matches(item, Map matchState) { 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) { - var compare = ContentType.parse(item.headers['content-type']!); + var compare = ContentType.parse(headerContentType); return equals(contentType.mimeType) .matches(compare.mimeType, matchState); } else { 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; } } + + @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 { @@ -161,11 +190,24 @@ class _HasStatus extends Matcher { bool matches(item, Map matchState) => item is http.Response && 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 { final Validator validator; + final _errors = []; + _HasValidBody(this.validator); @override @@ -176,11 +218,32 @@ class _HasValidBody extends Matcher { bool matches(item, Map matchState) { if (item is http.Response) { final jsons = json.decode(item.body); - if (jsons is! Map) return false; - return validator.matches(jsons, matchState); - } else { - return false; + if (jsons is Map) { + try { + return validator.matches(jsons, matchState); + } 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; } } diff --git a/packages/test/pubspec.yaml b/packages/test/pubspec.yaml index fcc3ae92..dd39ac50 100644 --- a/packages/test/pubspec.yaml +++ b/packages/test/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_test -version: 8.0.0 +version: 8.0.1 description: Testing utility library for the Angel3 framework. Use with package:test. homepage: https://angel3-framework.web.app/ repository: https://github.com/dukefirehawk/angel/tree/master/packages/test @@ -19,9 +19,9 @@ dependencies: dev_dependencies: test: ^1.24.0 lints: ^2.1.0 -# dependency_overrides: -# angel3_container: -# path: ../container/angel_container +dependency_overrides: + angel3_validate: + path: ../validate # angel3_framework: # path: ../framework # angel3_http_exception: diff --git a/packages/validate/CHANGELOG.md b/packages/validate/CHANGELOG.md index b4d8b308..c7d69333 100644 --- a/packages/validate/CHANGELOG.md +++ b/packages/validate/CHANGELOG.md @@ -2,7 +2,7 @@ ## 8.0.1 -* Fixed null check throwing exception +* Fixed missing mismatch errors ## 8.0.0 diff --git a/packages/validate/lib/src/validator.dart b/packages/validate/lib/src/validator.dart index c7092e7f..f39470db 100644 --- a/packages/validate/lib/src/validator.dart +++ b/packages/validate/lib/src/validator.dart @@ -63,6 +63,9 @@ class Validator extends Matcher { /// Fields that must be present for data to be considered valid. final List requiredFields = []; + /// Validation error messages. + final List errorMessages = []; + void _importSchema(Map schema) { for (var keys in schema.keys) { for (var key in keys.split(',').map((s) => s.trim())) { @@ -205,7 +208,12 @@ class Validator extends Matcher { } } } catch (e) { - errors.add(e.toString()); + if (e is ValidationException) { + errors.add(e.errors.first); + } else { + errors.add(e.toString()); + } + valid = false; break; } @@ -401,7 +409,7 @@ class ValidationException extends AngelHttpException { statusCode: 400, errors: (errors).toSet().toList(), stackTrace: StackTrace.current) { - this.errors.addAll(errors.toSet()); + //this.errors.addAll(errors.toSet()); } @override