platform/test/hooked_test.dart

141 lines
3.8 KiB
Dart
Raw Normal View History

2016-11-23 09:10:47 +00:00
import 'dart:io';
2016-06-21 04:19:43 +00:00
import 'package:angel_framework/angel_framework.dart';
import 'package:http/http.dart' as http;
2016-06-21 22:56:04 +00:00
import 'package:json_god/json_god.dart' as god;
2016-06-21 04:19:43 +00:00
import 'package:test/test.dart';
2016-06-27 00:20:42 +00:00
import 'common.dart';
2016-06-21 04:19:43 +00:00
main() {
Map headers = {
2016-11-23 09:10:47 +00:00
'Accept': 'application/json',
'Content-Type': 'application/json'
};
2016-06-21 04:19:43 +00:00
2016-11-23 09:10:47 +00:00
Angel app;
HttpServer server;
String url;
http.Client client;
HookedService Todos;
2016-06-21 04:19:43 +00:00
2016-11-23 09:10:47 +00:00
setUp(() async {
2016-12-10 14:05:40 +00:00
app = new Angel();
2016-11-23 09:10:47 +00:00
client = new http.Client();
2017-02-23 00:37:15 +00:00
app.use('/todos', new TypedService<Todo>(new MapService()));
2017-01-28 03:47:00 +00:00
app.use('/books', new BookService());
2018-06-23 03:29:38 +00:00
Todos = app.service("todos") as HookedService;
2016-06-21 04:19:43 +00:00
2017-04-04 08:35:36 +00:00
Todos.beforeAllStream().listen((e) {
print('Fired ${e.eventName}! Data: ${e.data}; Params: ${e.params}');
});
2017-09-22 14:03:23 +00:00
app.errorHandler = (e, req, res) {
throw e.error;
};
2016-06-21 04:19:43 +00:00
2018-02-07 05:36:24 +00:00
server = await new AngelHttp(app).startServer();
url = "http://${server.address.host}:${server.port}";
2016-11-23 09:10:47 +00:00
});
2016-06-21 04:19:43 +00:00
2016-11-23 09:10:47 +00:00
tearDown(() async {
await server.close(force: true);
app = null;
url = null;
client.close();
client = null;
Todos = null;
});
2016-06-21 04:19:43 +00:00
2016-11-23 09:10:47 +00:00
test("listen before and after", () async {
int count = 0;
2016-06-21 04:19:43 +00:00
2016-11-23 09:10:47 +00:00
Todos
..beforeIndexed.listen((_) {
count++;
})
..afterIndexed.listen((_) {
count++;
});
var response = await client.get("$url/todos");
print(response.body);
expect(count, equals(2));
});
test("cancel before", () async {
Todos.beforeCreated
..listen((HookedServiceEvent event) {
2016-06-21 04:19:43 +00:00
event.cancel({"hello": "hooked world"});
2016-11-23 09:10:47 +00:00
})
..listen((HookedServiceEvent event) {
2016-06-21 04:19:43 +00:00
event.cancel({"this_hook": "should never run"});
});
2016-11-23 09:10:47 +00:00
var response = await client.post("$url/todos",
2018-06-23 03:29:38 +00:00
body: god.serialize({"arbitrary": "data"}), headers: headers.cast<String, String>());
2016-11-23 09:10:47 +00:00
print(response.body);
Map result = god.deserialize(response.body);
expect(result["hello"], equals("hooked world"));
});
2016-06-21 04:19:43 +00:00
2016-11-23 09:10:47 +00:00
test("cancel after", () async {
Todos.afterIndexed
..listen((HookedServiceEvent event) async {
2016-06-21 04:19:43 +00:00
// Hooks can be Futures ;)
2016-11-23 09:10:47 +00:00
event.cancel([
{"angel": "framework"}
]);
})
..listen((HookedServiceEvent event) {
2016-06-21 04:19:43 +00:00
event.cancel({"this_hook": "should never run either"});
});
2016-11-23 09:10:47 +00:00
var response = await client.get("$url/todos");
print(response.body);
List result = god.deserialize(response.body);
expect(result[0]["angel"], equals("framework"));
});
2016-12-10 14:05:40 +00:00
test('metadata', () async {
final service = new HookedService(new IncrementService())..addHooks();
2018-06-23 03:29:38 +00:00
expect(service.inner, isNot(const TypeMatcher<MapService>()));
2016-12-10 14:05:40 +00:00
IncrementService.TIMES = 0;
await service.index();
expect(IncrementService.TIMES, equals(2));
});
2017-01-28 03:47:00 +00:00
test('inject request + response', () async {
HookedService books = app.service('books');
books.beforeIndexed.listen((e) {
expect([e.request, e.response], everyElement(isNotNull));
print('Indexing books at path: ${e.request.path}');
});
var response = await client.get('$url/books');
print(response.body);
var result = god.deserialize(response.body);
expect(result, isList);
expect(result, isNotEmpty);
expect(result[0], equals({'foo': 'bar'}));
});
2017-07-09 16:50:46 +00:00
test('contains provider in before and after', () async {
var svc = new HookedService(new AnonymousService(index: ([p]) async => []));
ensureProviderIsPresent(HookedServiceEvent e) {
var type = e.isBefore ? 'before' : 'after';
print('Params to $type ${e.eventName}: ${e.params}');
expect(e.params, isMap);
expect(e.params.keys, contains('provider'));
2018-06-23 03:29:38 +00:00
expect(e.params['provider'], const TypeMatcher<Providers>());
2017-07-09 16:50:46 +00:00
}
svc
..beforeAll(ensureProviderIsPresent)
..afterAll(ensureProviderIsPresent);
await svc.index({'provider': const Providers('testing')});
});
2016-11-23 09:10:47 +00:00
}