Fixed test cases

This commit is contained in:
thomashii 2021-03-05 15:51:48 +08:00
parent dd938c7512
commit 6875451993
6 changed files with 31 additions and 22 deletions

View file

@ -124,7 +124,7 @@ class AngelAuthResult {
/// Attempts to deserialize a response from a [Map]. /// Attempts to deserialize a response from a [Map].
factory AngelAuthResult.fromMap(Map data) { factory AngelAuthResult.fromMap(Map data) {
final result = new AngelAuthResult(); final result = AngelAuthResult();
if (data is Map && data.containsKey('token') && data['token'] is String) if (data is Map && data.containsKey('token') && data['token'] is String)
result._token = data['token'].toString(); result._token = data['token'].toString();
@ -133,10 +133,10 @@ class AngelAuthResult {
result.data.addAll((data['data'] as Map<String, dynamic>) ?? {}); result.data.addAll((data['data'] as Map<String, dynamic>) ?? {});
if (result.token == null) { if (result.token == null) {
throw new FormatException( throw FormatException(
'The required "token" field was not present in the given data.'); 'The required "token" field was not present in the given data.');
} else if (data['data'] is! Map) { } else if (data['data'] is! Map) {
throw new FormatException( throw FormatException(
'The required "data" field in the given data was not a map; instead, it was ${data['data']}.'); 'The required "data" field in the given data was not a map; instead, it was ${data['data']}.');
} }
@ -145,7 +145,7 @@ class AngelAuthResult {
/// Attempts to deserialize a response from a [String]. /// Attempts to deserialize a response from a [String].
factory AngelAuthResult.fromJson(String s) => factory AngelAuthResult.fromJson(String s) =>
new AngelAuthResult.fromMap(json.decode(s) as Map); AngelAuthResult.fromMap(json.decode(s) as Map);
/// Converts this instance into a JSON-friendly representation. /// Converts this instance into a JSON-friendly representation.
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
@ -200,7 +200,7 @@ abstract class Service<Id, Data> {
/// ///
/// Handy utility for handling data in a type-safe manner. /// Handy utility for handling data in a type-safe manner.
Service<Id, U> map<U>(U Function(Data) encoder, Data Function(U) decoder) { Service<Id, U> map<U>(U Function(Data) encoder, Data Function(U) decoder) {
return new _MappedService(this, encoder, decoder); return _MappedService(this, encoder, decoder);
} }
} }
@ -215,7 +215,7 @@ class _MappedService<Id, Data, U> extends Service<Id, U> {
Angel get app => inner.app; Angel get app => inner.app;
@override @override
Future close() => new Future.value(); Future close() => Future.value();
@override @override
Future<U> create(U data, [Map<String, dynamic> params]) { Future<U> create(U data, [Map<String, dynamic> params]) {
@ -281,19 +281,18 @@ class ServiceList<Id, Data> extends DelegatingList<Data> {
final Service<Id, Data> service; final Service<Id, Data> service;
final StreamController<ServiceList<Id, Data>> _onChange = final StreamController<ServiceList<Id, Data>> _onChange = StreamController();
new StreamController();
final List<StreamSubscription> _subs = []; final List<StreamSubscription> _subs = [];
ServiceList(this.service, {this.idField = 'id', Equality<Data> equality}) ServiceList(this.service, {this.idField = 'id', Equality<Data> equality})
: super([]) { : super([]) {
_equality = equality; _equality = equality;
_equality ??= new EqualityBy<Data, Id>((map) { _equality ??= EqualityBy<Data, Id>((map) {
if (map is Map) if (map is Map)
return map[idField ?? 'id'] as Id; return map[idField ?? 'id'] as Id;
else else
throw new UnsupportedError( throw UnsupportedError(
'ServiceList only knows how to find the id from a Map object. Provide a custom `Equality` in your call to the constructor.'); 'ServiceList only knows how to find the id from a Map object. Provide a custom `Equality` in your call to the constructor.');
}); });
// Index // Index

View file

@ -70,7 +70,11 @@ abstract class BaseAngelClient extends Angel {
var segments = baseUrl.pathSegments var segments = baseUrl.pathSegments
.followedBy(p.split(authEndpoint)) .followedBy(p.split(authEndpoint))
.followedBy([type]); .followedBy([type]);
var url = baseUrl.replace(path: p.joinAll(segments));
// TODO: convert windows path to proper url
var p1 = p.joinAll(segments).replaceAll('\\', '/');
var url = baseUrl.replace(path: p1);
http.Response response; http.Response response;
if (credentials != null) { if (credentials != null) {

View file

@ -13,13 +13,13 @@ export 'angel_client.dart';
class Rest extends BaseAngelClient { class Rest extends BaseAngelClient {
final List<Service> _services = []; final List<Service> _services = [];
Rest(String path) : super(new http.Client() as http.BaseClient, path); Rest(String path) : super(http.Client() as http.BaseClient, path);
@override @override
Service<Id, Data> service<Id, Data>(String path, Service<Id, Data> service<Id, Data>(String path,
{Type type, AngelDeserializer deserializer}) { {Type type, AngelDeserializer deserializer}) {
var url = baseUrl.replace(path: p.join(baseUrl.path, path)); var url = baseUrl.replace(path: p.join(baseUrl.path, path));
var s = new RestService<Id, Data>(client, this, url, type); var s = RestService<Id, Data>(client, this, url, type);
_services.add(s); _services.add(s);
return s; return s;
} }
@ -27,10 +27,11 @@ class Rest extends BaseAngelClient {
@override @override
Stream<String> authenticateViaPopup(String url, Stream<String> authenticateViaPopup(String url,
{String eventName = 'token'}) { {String eventName = 'token'}) {
throw new UnimplementedError( throw UnimplementedError(
'Opening popup windows is not supported in the `dart:io` client.'); 'Opening popup windows is not supported in the `dart:io` client.');
} }
@override
Future close() async { Future close() async {
await super.close(); await super.close();
await Future.wait(_services.map((s) => s.close())).then((_) { await Future.wait(_services.map((s) => s.close())).then((_) {
@ -48,6 +49,7 @@ class RestService<Id, Data> extends BaseAngelService<Id, Data> {
@override @override
Data deserialize(x) { Data deserialize(x) {
print(x);
if (type != null) { if (type != null) {
return x.runtimeType == type return x.runtimeType == type
? x as Data ? x as Data
@ -58,7 +60,8 @@ class RestService<Id, Data> extends BaseAngelService<Id, Data> {
} }
@override @override
makeBody(x) { String makeBody(x) {
print(x);
if (type != null) { if (type != null) {
return super.makeBody(god.serializeObject(x)); return super.makeBody(god.serializeObject(x));
} }

View file

@ -10,7 +10,8 @@ dependencies:
path: ../http_exception path: ../http_exception
collection: ^1.0.0 collection: ^1.0.0
http: ^0.12.0 http: ^0.12.0
json_god: ">=2.0.0-beta <3.0.0" json_god: ^2.0.0-beta
#dart_json_mapper: ^1.7.0
meta: ^1.0.0 meta: ^1.0.0
path: ^1.0.0 path: ^1.0.0
dev_dependencies: dev_dependencies:

View file

@ -5,7 +5,7 @@ import 'package:file/memory.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
main() { void main() {
Angel app; Angel app;
MemoryFileSystem fileSystem; MemoryFileSystem fileSystem;
TestClient client; TestClient client;
@ -48,6 +48,7 @@ main() {
test('serves as fallback', () async { test('serves as fallback', () async {
var response = await client.get('/nope'); var response = await client.get('/nope');
print(response);
expect(response.body, 'index'); expect(response.body, 'index');
}); });
} }

View file

@ -36,22 +36,23 @@ void main() {
await app.configure(auth.configureServer); await app.configure(auth.configureServer);
var sock = AngelWebSocket(app); var sock = AngelWebSocket(app);
await app.configure(sock.configureServer); await app.configure(sock.configureServer);
app.all('/ws', sock.handleRequest); app.all('/ws', sock.handleRequest);
app.logger = Logger('angel_auth')..onRecord.listen(print); app.logger = Logger('angel_auth')..onRecord.listen(print);
var server = await http.startServer(); var server = await http.startServer();
client = c.Rest('http://${server.address.address}:${server.port}'); client = c.Rest('http://${server.address.address}:${server.port}');
ws = c.WebSockets('ws://${server.address.address}:${server.port}/ws'); ws = c.WebSockets('ws://${server.address.address}:${server.port}/ws');
await ws.connect(); await ws.connect();
}); });
tearDown(() { tearDown(() {
return Future.wait([ http.close();
http.close(), client.close();
client.close(), ws.close();
ws.close(),
]);
}); });
test('auth event fires', () async { test('auth event fires', () async {