From 27d739864c3d9bf9c8f2aa97389b8751cbe17438 Mon Sep 17 00:00:00 2001 From: thomashii Date: Tue, 18 May 2021 19:43:58 +0800 Subject: [PATCH] Updated test --- packages/test/CHANGELOG.md | 3 +++ packages/test/README.md | 2 +- packages/test/analysis_options.yaml | 1 + packages/test/example/main.dart | 6 +++--- packages/test/lib/src/client.dart | 30 ++++++++++++++++------------- packages/test/lib/src/matchers.dart | 30 ++++++++++++++++++----------- packages/test/pubspec.yaml | 3 ++- packages/test/test/simple_test.dart | 4 ++-- 8 files changed, 48 insertions(+), 31 deletions(-) diff --git a/packages/test/CHANGELOG.md b/packages/test/CHANGELOG.md index e13bbc45..c42cc46d 100644 --- a/packages/test/CHANGELOG.md +++ b/packages/test/CHANGELOG.md @@ -1,3 +1,6 @@ +# 4.0.2 +* Resolved static analysis warnings + # 4.0.1 * Updated AUTHORS and LICENSE diff --git a/packages/test/README.md b/packages/test/README.md index 82c7d3cc..f3fcdcac 100644 --- a/packages/test/README.md +++ b/packages/test/README.md @@ -1,5 +1,5 @@ # angel3_test -[![version](https://img.shields.io/badge/pub-v4.0.1-brightgreen)](https://pub.dartlang.org/packages/angel3_test) +[![version](https://img.shields.io/badge/pub-v4.0.2-brightgreen)](https://pub.dartlang.org/packages/angel3_test) [![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) [![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) diff --git a/packages/test/analysis_options.yaml b/packages/test/analysis_options.yaml index eae1e42a..c230cee7 100644 --- a/packages/test/analysis_options.yaml +++ b/packages/test/analysis_options.yaml @@ -1,3 +1,4 @@ +include: package:pedantic/analysis_options.yaml analyzer: strong-mode: implicit-casts: false \ No newline at end of file diff --git a/packages/test/example/main.dart b/packages/test/example/main.dart index 0bb3d82e..78827a76 100644 --- a/packages/test/example/main.dart +++ b/packages/test/example/main.dart @@ -34,8 +34,8 @@ void main() { ..get('/gzip', (req, res) async { res ..headers['content-encoding'] = 'gzip' - ..add(gzip.encode('Poop'.codeUnits)) - ..close(); + ..add(gzip.encode('Poop'.codeUnits)); + await res.close(); }) ..use( '/foo', @@ -135,7 +135,7 @@ void main() { test('websocket', () async { var ws = await client.websocket(); var foo = ws.service('foo'); - foo.create({}); + await foo.create({}); var result = await foo.onCreated.first; expect(result.data, equals({'foo': 'bar'})); }); diff --git a/packages/test/lib/src/client.dart b/packages/test/lib/src/client.dart index a7c5344f..a780e572 100644 --- a/packages/test/lib/src/client.dart +++ b/packages/test/lib/src/client.dart @@ -14,7 +14,7 @@ import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/io.dart'; //import 'package:uuid/uuid.dart'; -final RegExp _straySlashes = RegExp(r"(^/)|(/+$)"); +final RegExp _straySlashes = RegExp(r'(^/)|(/+$)'); /*const Map _readHeaders = const {'Accept': 'application/json'}; const Map _writeHeaders = const { 'Accept': 'application/json', @@ -25,15 +25,15 @@ final Uuid _uuid = Uuid();*/ /// Shorthand for bootstrapping a [TestClient]. Future connectTo(Angel app, {Map? initialSession, - bool autoDecodeGzip: true, - bool useZone: false}) async { - print("Load configuration"); + bool autoDecodeGzip = true, + bool useZone = false}) async { + print('Load configuration'); if (!app.environment.isProduction) { app.configuration.putIfAbsent('testMode', () => true); } for (var plugin in app.startupHooks) { - print("Load plugins"); + print('Load plugins'); await plugin(app); } return TestClient(app, @@ -62,11 +62,12 @@ class TestClient extends client.BaseAngelClient { late AngelHttp _http; - TestClient(this.server, {this.autoDecodeGzip: true, bool useZone: false}) + TestClient(this.server, {this.autoDecodeGzip = true, bool useZone = false}) : super(http.IOClient(), '/') { _http = AngelHttp(server, useZone: useZone); } + @override Future close() { this.client!.close(); return server.close(); @@ -75,7 +76,7 @@ class TestClient extends client.BaseAngelClient { /// Opens a WebSockets connection to the server. This will automatically bind the server /// over HTTP, if it is not already listening. Unfortunately, WebSockets cannot be mocked (yet!). Future websocket( - {String path: '/ws', Duration? timeout}) async { + {String path = '/ws', Duration? timeout}) async { if (_http.server == null) await _http.startServer(); var url = _http.uri.replace(scheme: 'ws', path: path); var ws = _MockWebSockets(this, url.toString()); @@ -83,6 +84,7 @@ class TestClient extends client.BaseAngelClient { return ws; } + @override Future send(http.BaseRequest request) async { var rq = MockHttpRequest(request.method, request.url); request.headers.forEach(rq.headers.add); @@ -115,7 +117,7 @@ class TestClient extends client.BaseAngelClient { ..clear() ..addAll(rq.session); - Map extractedHeaders = {}; + var extractedHeaders = {}; rs.headers.forEach((k, v) { extractedHeaders[k] = v.join(','); @@ -128,7 +130,7 @@ class TestClient extends client.BaseAngelClient { stream = stream.transform(gzip.decoder); } - // TODO: Calling persistentConnection causes LateInitialization Exception + // Calling persistentConnection causes LateInitialization Exception //var keepAliveState = rq.headers?.persistentConnection; //if (keepAliveState == null) { // keepAliveState = false; @@ -149,7 +151,8 @@ class TestClient extends client.BaseAngelClient { late String basePath; @override - Stream authenticateViaPopup(String url, {String eventName: 'token'}) { + Stream authenticateViaPopup(String url, + {String eventName = 'token'}) { throw UnsupportedError( 'MockClient does not support authentication via popup.'); } @@ -161,7 +164,7 @@ class TestClient extends client.BaseAngelClient { @override client.Service service(String path, {Type? type, client.AngelDeserializer? deserializer}) { - String uri = path.toString().replaceAll(_straySlashes, ""); + var uri = path.toString().replaceAll(_straySlashes, ''); return _services.putIfAbsent(uri, () => _MockService(this, uri, deserializer: deserializer)) as client.Service; @@ -192,10 +195,11 @@ class _MockWebSockets extends client.WebSockets { @override Future getConnectedWebSocket() async { - Map headers = {}; + var headers = {}; - if (app.authToken?.isNotEmpty == true) + if (app.authToken?.isNotEmpty == true) { headers['authorization'] = 'Bearer ${app.authToken}'; + } var socket = await WebSocket.connect(baseUrl.toString(), headers: headers); return IOWebSocketChannel(socket); diff --git a/packages/test/lib/src/matchers.dart b/packages/test/lib/src/matchers.dart index f9b9d7dd..86114b9f 100644 --- a/packages/test/lib/src/matchers.dart +++ b/packages/test/lib/src/matchers.dart @@ -11,7 +11,7 @@ import 'package:angel3_validate/angel3_validate.dart'; Matcher isAngelHttpException( {String? message, int? statusCode, - Iterable errors: const []}) => + Iterable errors = const []}) => _IsAngelHttpException( message: message, statusCode: statusCode, errors: errors); @@ -71,10 +71,11 @@ class _HasBody extends Matcher { bool matches(item, Map matchState) { if (item is http.Response) { if (body == true) return isNotEmpty.matches(item.bodyBytes, matchState); - if (body is List) + if (body is List) { return equals(body).matches(item.bodyBytes, matchState); - else + } else { return equals(body.toString()).matches(item.body, matchState); + } } else { return false; } @@ -121,10 +122,11 @@ class _HasHeader extends Matcher { @override Description describe(Description description) { - if (value == true) + if (value == true) { return description.add('contains header $key'); - else + } else { return description.add('contains header $key with value(s) $value'); + } } @override @@ -135,7 +137,7 @@ class _HasHeader extends Matcher { .matches(item.headers.keys, matchState); } else { if (!item.headers.containsKey(key.toLowerCase())) return false; - Iterable v = value is Iterable ? (value as Iterable) : [value]; + var v = value is Iterable ? (value as Iterable) : [value]; return v .map((x) => x.toString()) .every(item.headers[key.toLowerCase()]!.split(',').contains); @@ -189,7 +191,7 @@ class _IsAngelHttpException extends Matcher { final List errors = []; _IsAngelHttpException( - {this.message, this.statusCode, Iterable errors: const []}) { + {this.message, this.statusCode, Iterable errors = const []}) { this.errors.addAll(errors); } @@ -237,11 +239,17 @@ class _IsAngelHttpException extends Matcher { errors.isEmpty) { return true; } else { - if (statusCode != null) if (!equals(statusCode) - .matches(exc.statusCode, matchState)) return false; + if (statusCode != null) { + if (!equals(statusCode).matches(exc.statusCode, matchState)) { + return false; + } + } - if (message?.isNotEmpty == true) if (!equals(message) - .matches(exc.message, matchState)) return false; + if (message?.isNotEmpty == true) { + if (!equals(message).matches(exc.message, matchState)) { + return false; + } + } if (errors.isNotEmpty) { if (!errors.every( diff --git a/packages/test/pubspec.yaml b/packages/test/pubspec.yaml index 962c9f4f..3786cd6c 100644 --- a/packages/test/pubspec.yaml +++ b/packages/test/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_test -version: 4.0.1 +version: 4.0.2 description: Testing utility library for the Angel framework. Use with package:test. homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/test environment: @@ -17,4 +17,5 @@ dependencies: web_socket_channel: ^2.0.0 dev_dependencies: test: ^1.17.4 + pedantic: ^1.11.0 diff --git a/packages/test/test/simple_test.dart b/packages/test/test/simple_test.dart index 079173d2..1aad7ad1 100644 --- a/packages/test/test/simple_test.dart +++ b/packages/test/test/simple_test.dart @@ -35,8 +35,8 @@ void main() { ..get('/gzip', (req, res) async { res ..headers['content-encoding'] = 'gzip' - ..add(gzip.encode('Poop'.codeUnits)) - ..close(); + ..add(gzip.encode('Poop'.codeUnits)); + await res.close(); }) ..use( '/foo',