Migrated test

This commit is contained in:
thomashii@dukefirehawk.com 2021-04-26 09:05:34 +08:00
parent 7bd8e5a57b
commit 99c0bd9ada
6 changed files with 51 additions and 45 deletions

View file

@ -17,7 +17,7 @@
* Migrated json_god to 4.0.0 (13/13 tests passed) * Migrated json_god to 4.0.0 (13/13 tests passed)
* Migrated angel_client to 4.0.0 (6/13 tests passed) * Migrated angel_client to 4.0.0 (6/13 tests passed)
* Migrated angel_websocket to 4.0.0 (2/3 tests passed) * Migrated angel_websocket to 4.0.0 (2/3 tests passed)
* Updated test to 3.0.0 (in progress) * Updated test to 4.0.0 (1/1 test passed)
* Updated jael to 3.0.0 (in progress) * Updated jael to 3.0.0 (in progress)
* Updated jael_preprocessor to 3.0.0 (in progress) * Updated jael_preprocessor to 3.0.0 (in progress)
* Updated angel_jael to 3.0.0 (in progress) * Updated angel_jael to 3.0.0 (in progress)
@ -41,7 +41,7 @@
* Updated angel_validate to 3.0.0 * Updated angel_validate to 3.0.0
* Added and updated json_god to 3.0.0 * Added and updated json_god to 3.0.0
* Updated angel_client to 3.0.0 * Updated angel_client to 3.0.0
* Updated angel_websocket to 3.0.0 (one issue to be resolved) * Updated angel_websocket to 3.0.0
* Updated jael to 3.0.0 * Updated jael to 3.0.0
* Updated jael_preprocessor to 3.0.0 * Updated jael_preprocessor to 3.0.0
* Updated test to 3.0.0 * Updated test to 3.0.0

View file

@ -6,8 +6,8 @@ import 'package:angel_websocket/server.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
main() { main() {
Angel app; Angel? app;
TestClient client; late TestClient client;
setUp(() async { setUp(() async {
app = new Angel() app = new Angel()
@ -43,15 +43,15 @@ main() {
index: ([params]) async => [ index: ([params]) async => [
{'michael': 'jackson'} {'michael': 'jackson'}
], ],
create: (data, [params]) async => {'foo': 'bar'})); create: (dynamic data, [params]) async => {'foo': 'bar'}));
var ws = new AngelWebSocket(app); var ws = new AngelWebSocket(app);
await app.configure(ws.configureServer); await app!.configure(ws.configureServer);
app.all('/ws', ws.handleRequest); app!.all('/ws', ws.handleRequest);
app.errorHandler = (e, req, res) => e.toJson(); app!.errorHandler = (e, req, res) => e.toJson();
client = await connectTo(app); client = await connectTo(app!);
}); });
tearDown(() async { tearDown(() async {

View file

@ -24,7 +24,7 @@ final Uuid _uuid = new Uuid();*/
/// Shorthand for bootstrapping a [TestClient]. /// Shorthand for bootstrapping a [TestClient].
Future<TestClient> connectTo(Angel app, Future<TestClient> connectTo(Angel app,
{Map initialSession, {Map? initialSession,
bool autoDecodeGzip: true, bool autoDecodeGzip: true,
bool useZone: false}) async { bool useZone: false}) async {
print("Load configuration"); print("Load configuration");
@ -58,9 +58,9 @@ class TestClient extends client.BaseAngelClient {
final Angel server; final Angel server;
@override @override
String authToken; String? authToken;
AngelHttp _http; late AngelHttp _http;
TestClient(this.server, {this.autoDecodeGzip: true, bool useZone: false}) TestClient(this.server, {this.autoDecodeGzip: true, bool useZone: false})
: super(http.IOClient(), '/') { : super(http.IOClient(), '/') {
@ -68,14 +68,14 @@ class TestClient extends client.BaseAngelClient {
} }
Future close() { Future close() {
this.client.close(); this.client!.close();
return server.close(); return server.close();
} }
/// Opens a WebSockets connection to the server. This will automatically bind the server /// 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!). /// over HTTP, if it is not already listening. Unfortunately, WebSockets cannot be mocked (yet!).
Future<client.WebSockets> websocket( Future<client.WebSockets> websocket(
{String path: '/ws', Duration timeout}) async { {String path: '/ws', Duration? timeout}) async {
if (_http.server == null) await _http.startServer(); if (_http.server == null) await _http.startServer();
var url = _http.uri.replace(scheme: 'ws', path: path); var url = _http.uri.replace(scheme: 'ws', path: path);
var ws = _MockWebSockets(this, url.toString()); var ws = _MockWebSockets(this, url.toString());
@ -93,7 +93,7 @@ class TestClient extends client.BaseAngelClient {
rq.headers.add('authorization', 'Basic $encoded'); rq.headers.add('authorization', 'Basic $encoded');
} else if (rq.headers.value('authorization')?.startsWith('Basic ') == } else if (rq.headers.value('authorization')?.startsWith('Basic ') ==
true) { true) {
var encoded = rq.headers.value('authorization').substring(6); var encoded = rq.headers.value('authorization')!.substring(6);
var decoded = utf8.decode(base64Url.decode(encoded)); var decoded = utf8.decode(base64Url.decode(encoded));
var oldRq = rq; var oldRq = rq;
var newRq = MockHttpRequest(rq.method, rq.uri.replace(userInfo: decoded)); var newRq = MockHttpRequest(rq.method, rq.uri.replace(userInfo: decoded));
@ -139,14 +139,14 @@ class TestClient extends client.BaseAngelClient {
isRedirect: rs.headers['location'] != null, isRedirect: rs.headers['location'] != null,
headers: extractedHeaders, headers: extractedHeaders,
persistentConnection: persistentConnection:
rq.headers.value('connection')?.toLowerCase()?.trim() == rq.headers.value('connection')?.toLowerCase().trim() ==
'keep-alive', 'keep-alive',
//|| keepAliveState, //|| keepAliveState,
reasonPhrase: rs.reasonPhrase); reasonPhrase: rs.reasonPhrase);
} }
@override @override
String basePath; late String basePath;
@override @override
Stream<String> authenticateViaPopup(String url, {String eventName: 'token'}) { Stream<String> authenticateViaPopup(String url, {String eventName: 'token'}) {
@ -160,7 +160,7 @@ class TestClient extends client.BaseAngelClient {
@override @override
client.Service<Id, Data> service<Id, Data>(String path, client.Service<Id, Data> service<Id, Data>(String path,
{Type type, client.AngelDeserializer<Data> deserializer}) { {Type? type, client.AngelDeserializer<Data>? deserializer}) {
String uri = path.toString().replaceAll(_straySlashes, ""); String uri = path.toString().replaceAll(_straySlashes, "");
return _services.putIfAbsent(uri, return _services.putIfAbsent(uri,
() => _MockService<Id, Data>(this, uri, deserializer: deserializer)) () => _MockService<Id, Data>(this, uri, deserializer: deserializer))
@ -172,12 +172,12 @@ class _MockService<Id, Data> extends client.BaseAngelService<Id, Data> {
final TestClient _app; final TestClient _app;
_MockService(this._app, String basePath, _MockService(this._app, String basePath,
{client.AngelDeserializer<Data> deserializer}) {client.AngelDeserializer<Data>? deserializer})
: super(null, _app, basePath, deserializer: deserializer); : super(null, _app, basePath, deserializer: deserializer);
@override @override
Future<StreamedResponse> send(http.BaseRequest request) { Future<StreamedResponse> send(http.BaseRequest request) {
if (app.authToken != null && app.authToken.isNotEmpty) { if (app.authToken != null && app.authToken!.isNotEmpty) {
request.headers['authorization'] ??= 'Bearer ${app.authToken}'; request.headers['authorization'] ??= 'Bearer ${app.authToken}';
} }

View file

@ -9,7 +9,9 @@ import 'package:angel_validate/angel_validate.dart';
/// ///
/// You can optionally check for a matching [message], [statusCode] and [errors]. /// You can optionally check for a matching [message], [statusCode] and [errors].
Matcher isAngelHttpException( Matcher isAngelHttpException(
{String message, int statusCode, Iterable<String> errors: const []}) => {String? message,
int? statusCode,
Iterable<String> errors: const []}) =>
new _IsAngelHttpException( new _IsAngelHttpException(
message: message, statusCode: statusCode, errors: errors); message: message, statusCode: statusCode, errors: errors);
@ -98,7 +100,7 @@ class _HasContentType extends Matcher {
if (!item.headers.containsKey('content-type')) return false; if (!item.headers.containsKey('content-type')) return false;
if (contentType is ContentType) { if (contentType is ContentType) {
var compare = ContentType.parse(item.headers['content-type']); var compare = ContentType.parse(item.headers['content-type']!);
return equals(contentType.mimeType) return equals(contentType.mimeType)
.matches(compare.mimeType, matchState); .matches(compare.mimeType, matchState);
} else { } else {
@ -136,7 +138,7 @@ class _HasHeader extends Matcher {
Iterable v = value is Iterable ? (value as Iterable) : [value]; Iterable v = value is Iterable ? (value as Iterable) : [value];
return v return v
.map((x) => x.toString()) .map((x) => x.toString())
.every(item.headers[key.toLowerCase()].split(',').contains); .every(item.headers[key.toLowerCase()]!.split(',').contains);
} }
} else { } else {
return false; return false;
@ -182,13 +184,13 @@ class _HasValidBody extends Matcher {
} }
class _IsAngelHttpException extends Matcher { class _IsAngelHttpException extends Matcher {
String message; String? message;
int statusCode; int? statusCode;
final List<String> errors = []; final List<String> errors = [];
_IsAngelHttpException( _IsAngelHttpException(
{this.message, this.statusCode, Iterable<String> errors: const []}) { {this.message, this.statusCode, Iterable<String> errors: const []}) {
this.errors.addAll(errors ?? []); this.errors.addAll(errors);
} }
@override @override

View file

@ -2,40 +2,44 @@ author: Tobe O <thosakwe@gmail.com>
description: Testing utility library for the Angel framework. Use with package:test. description: Testing utility library for the Angel framework. Use with package:test.
homepage: https://github.com/angel-dart/test.git homepage: https://github.com/angel-dart/test.git
name: angel_test name: angel_test
version: 3.0.0 version: 4.0.0
publish_to: none publish_to: none
environment: environment:
sdk: ">=2.10.0 <3.0.0" sdk: '>=2.12.0 <3.0.0'
dependencies: dependencies:
angel_client: angel_client:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/client path: packages/client
angel_framework: angel_framework:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/framework path: packages/framework
angel_http_exception: angel_http_exception:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/http_exception path: packages/http_exception
angel_validate: angel_validate:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/validate path: packages/validate
angel_websocket: angel_websocket:
git: git:
url: https://github.com/dukefirehawk/angel.git url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x ref: sdk-2.12.x_nnbd
path: packages/websocket path: packages/websocket
http: ^0.13.0 http: ^0.13.1
matcher: ^0.12.0 matcher: ^0.12.10
mock_request: ^1.0.0 mock_request:
web_socket_channel: ^1.2.0 git:
url: https://github.com/dukefirehawk/angel.git
ref: sdk-2.12.x_nnbd
path: packages/mock_request
web_socket_channel: ^2.0.0
dev_dependencies: dev_dependencies:
test: ^1.15.7 test: ^1.17.1

View file

@ -6,13 +6,13 @@ import 'package:angel_websocket/server.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
void main() { void main() {
Angel app; Angel? app;
TestClient client; late TestClient client;
setUp(() async { setUp(() async {
app = Angel(reflector: MirrorsReflector()) app = Angel(reflector: MirrorsReflector())
..get('/hello', (req, res) => 'Hello') ..get('/hello', (req, res) => 'Hello')
..get('/user_info', (req, res) => {'u': req.uri.userInfo}) ..get('/user_info', (req, res) => {'u': req.uri!.userInfo})
..get( ..get(
'/error', '/error',
(req, res) => throw AngelHttpException.forbidden(message: 'Test') (req, res) => throw AngelHttpException.forbidden(message: 'Test')
@ -48,12 +48,12 @@ void main() {
<String, dynamic>{'foo': 'bar'})); <String, dynamic>{'foo': 'bar'}));
var ws = AngelWebSocket(app); var ws = AngelWebSocket(app);
await app.configure(ws.configureServer); await app!.configure(ws.configureServer);
app.all('/ws', ws.handleRequest); app!.all('/ws', ws.handleRequest);
app.errorHandler = (e, req, res) => e.toJson(); app!.errorHandler = (e, req, res) => e.toJson();
client = await connectTo(app, useZone: false); client = await connectTo(app!, useZone: false);
}); });
tearDown(() async { tearDown(() async {