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
|
|
|
|
2016-11-23 09:10:47 +00:00
|
|
|
Angel app;
|
|
|
|
HttpServer server;
|
|
|
|
String url;
|
|
|
|
http.Client client;
|
2018-09-12 04:29:53 +00:00
|
|
|
HookedService todoService;
|
2016-06-21 04:19:43 +00:00
|
|
|
|
2016-11-23 09:10:47 +00:00
|
|
|
setUp(() async {
|
2018-08-20 02:40:11 +00:00
|
|
|
app = new Angel(reflector: MirrorsReflector());
|
2016-11-23 09:10:47 +00:00
|
|
|
client = new http.Client();
|
2018-08-20 02:37:48 +00:00
|
|
|
app.use('/todos', new MapService());
|
2017-01-28 03:47:00 +00:00
|
|
|
app.use('/books', new BookService());
|
2016-06-21 04:19:43 +00:00
|
|
|
|
2018-09-12 04:29:53 +00:00
|
|
|
todoService = app.findHookedService<MapService>('todos');
|
|
|
|
|
|
|
|
todoService.beforeAllStream().listen((e) {
|
2017-04-04 08:35:36 +00:00
|
|
|
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;
|
2018-09-12 04:29:53 +00:00
|
|
|
todoService = null;
|
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
|
2016-11-23 09:10:47 +00:00
|
|
|
..beforeIndexed.listen((_) {
|
|
|
|
count++;
|
|
|
|
})
|
|
|
|
..afterIndexed.listen((_) {
|
|
|
|
count++;
|
|
|
|
});
|
|
|
|
|
|
|
|
var response = await client.get("$url/todos");
|
|
|
|
print(response.body);
|
|
|
|
expect(count, equals(2));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("cancel before", () async {
|
2018-09-12 04:29:53 +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"});
|
|
|
|
});
|
|
|
|
|
2016-11-23 09:10:47 +00:00
|
|
|
var response = await client.post("$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 {
|
2018-09-12 04:29:53 +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"});
|
|
|
|
});
|
|
|
|
|
2016-11-23 09:10:47 +00:00
|
|
|
var response = await client.get("$url/todos");
|
|
|
|
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
|
|
|
|
|
|
|
test('metadata', () async {
|
2018-08-21 02:44:32 +00:00
|
|
|
final service = new HookedService(new IncrementService())..addHooks(app);
|
2018-07-09 14:28:32 +00:00
|
|
|
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 {
|
2018-09-12 04:29:53 +00:00
|
|
|
HookedService books = app.findService('books');
|
2017-01-28 03:47:00 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
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 {
|
|
|
|
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-07-09 14:28:32 +00:00
|
|
|
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
|
|
|
}
|