platform/packages/framework/test/hooked_test.dart

149 lines
4.3 KiB
Dart
Raw Normal View History

2018-08-20 02:31:08 +00:00
import 'dart:convert';
2016-11-23 09:10:47 +00:00
import 'dart:io';
2018-08-19 15:33:25 +00:00
import 'package:angel_container/mirrors.dart';
2016-06-21 04:19:43 +00:00
import 'package:angel_framework/angel_framework.dart';
2018-10-22 15:54:41 +00:00
import 'package:angel_framework/http.dart';
2016-06-21 04:19:43 +00:00
import 'package:http/http.dart' as http;
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() {
2018-07-09 14:54:50 +00:00
Map headers = <String, String>{
2016-11-23 09:10:47 +00:00
'Accept': 'application/json',
'Content-Type': 'application/json'
};
2016-06-21 04:19:43 +00:00
2021-04-03 05:50:52 +00:00
late Angel app;
2021-03-20 08:11:18 +00:00
late HttpServer server;
2021-04-03 05:50:52 +00:00
late String url;
late http.Client client;
late HookedService todoService;
2016-06-21 04:19:43 +00:00
2016-11-23 09:10:47 +00:00
setUp(() async {
2019-05-02 22:48:31 +00:00
app = Angel(reflector: MirrorsReflector());
client = http.Client();
2021-04-03 05:50:52 +00:00
app.use('/todos', MapService());
app.use('/books', BookService());
2016-06-21 04:19:43 +00:00
2021-04-03 05:50:52 +00:00
todoService = app.findHookedService<MapService>('todos')
as HookedService<dynamic, dynamic, Service>;
2018-09-12 04:29:53 +00:00
2021-04-03 05:50:52 +00:00
todoService.beforeAllStream().listen((e) {
2017-04-04 08:35:36 +00:00
print('Fired ${e.eventName}! Data: ${e.data}; Params: ${e.params}');
});
2021-04-03 05:50:52 +00:00
app.errorHandler = (e, req, res) {
2021-03-20 08:11:18 +00:00
throw e.error as Object;
2017-09-22 14:03:23 +00:00
};
2016-06-21 04:19:43 +00:00
2019-05-02 22:48:31 +00:00
server = await AngelHttp(app).startServer();
2018-02-07 05:36:24 +00:00
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);
2021-04-03 05:50:52 +00:00
client.close();
2016-11-23 09:10:47 +00:00
});
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
2018-09-12 04:29:53 +00:00
todoService
2021-04-03 05:50:52 +00:00
..beforeIndexed.listen((_) {
2016-11-23 09:10:47 +00:00
count++;
})
..afterIndexed.listen((_) {
count++;
});
2021-04-03 05:50:52 +00:00
var response = await client.get(Uri.parse("$url/todos"));
2016-11-23 09:10:47 +00:00
print(response.body);
expect(count, equals(2));
});
test("cancel before", () async {
2021-04-03 05:50:52 +00:00
todoService.beforeCreated
2016-11-23 09:10:47 +00:00
..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"});
});
2021-04-03 05:50:52 +00:00
var response = await client.post(Uri.parse("$url/todos"),
2018-08-19 15:49:33 +00:00
body: json.encode({"arbitrary": "data"}),
2018-06-27 18:40:43 +00:00
headers: headers as Map<String, String>);
2016-11-23 09:10:47 +00:00
print(response.body);
2019-04-08 19:53:07 +00:00
var result = json.decode(response.body) as Map;
2016-11-23 09:10:47 +00:00
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 {
2021-04-03 05:50:52 +00:00
todoService.afterIndexed
2016-11-23 09:10:47 +00:00
..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"});
});
2021-04-03 05:50:52 +00:00
var response = await client.get(Uri.parse("$url/todos"));
2016-11-23 09:10:47 +00:00
print(response.body);
2019-04-08 19:53:07 +00:00
var result = json.decode(response.body) as List;
2016-11-23 09:10:47 +00:00
expect(result[0]["angel"], equals("framework"));
});
2016-12-10 14:05:40 +00:00
2019-04-19 07:46:44 +00:00
test('asStream() fires', () async {
2021-04-03 05:50:52 +00:00
var stream = todoService.afterCreated.asStream();
await todoService.create({'angel': 'framework'});
2019-04-19 07:46:44 +00:00
expect(await stream.first.then((e) => e.result['angel']), 'framework');
});
2016-12-10 14:05:40 +00:00
test('metadata', () async {
2021-04-03 05:50:52 +00:00
final service = HookedService(IncrementService())..addHooks(app);
expect(service.inner, isNot(const IsInstanceOf<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 {
2021-04-03 05:50:52 +00:00
HookedService books = app.findService('books')
2021-03-20 08:11:18 +00:00
as HookedService<dynamic, dynamic, Service<dynamic, dynamic>>;
2017-01-28 03:47:00 +00:00
books.beforeIndexed.listen((e) {
expect([e.request, e.response], everyElement(isNotNull));
2021-04-03 05:50:52 +00:00
print('Indexing books at path: ${e.request?.path}');
2017-01-28 03:47:00 +00:00
});
2021-04-03 05:50:52 +00:00
var response = await client.get(Uri.parse('$url/books'));
2017-01-28 03:47:00 +00:00
print(response.body);
2018-08-20 02:31:08 +00:00
var result = json.decode(response.body);
2017-01-28 03:47:00 +00:00
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 {
2019-05-02 22:48:31 +00:00
var svc = HookedService(AnonymousService(index: ([p]) async => []));
2017-07-09 16:50:46 +00:00
ensureProviderIsPresent(HookedServiceEvent e) {
var type = e.isBefore ? 'before' : 'after';
print('Params to $type ${e.eventName}: ${e.params}');
expect(e.params, isMap);
2021-04-03 05:50:52 +00:00
expect(e.params?.keys, contains('provider'));
expect(e.params?['provider'], const IsInstanceOf<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
}