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() {
|
2016-09-15 19:53:01 +00:00
|
|
|
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 {
|
|
|
|
app = new Angel(debug: true);
|
|
|
|
client = new http.Client();
|
|
|
|
app.use('/todos', new MemoryService<Todo>());
|
|
|
|
Todos = app.service("todos");
|
2016-06-21 04:19:43 +00:00
|
|
|
|
2016-11-28 00:49:27 +00:00
|
|
|
app.dumpTree(showMatchers: true);
|
2016-06-21 04:19:43 +00:00
|
|
|
|
2016-11-23 09:10:47 +00:00
|
|
|
server = await app.startServer();
|
|
|
|
url = "http://${app.httpServer.address.host}:${app.httpServer.port}";
|
|
|
|
});
|
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",
|
|
|
|
body: god.serialize({"arbitrary": "data"}), headers: headers);
|
|
|
|
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"));
|
|
|
|
});
|
|
|
|
}
|