platform/test/di_test.dart

91 lines
2.2 KiB
Dart
Raw Normal View History

2016-09-17 16:12:25 +00:00
import 'dart:convert';
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:http/http.dart' as http;
import 'package:test/test.dart';
import 'common.dart';
final String TEXT = "make your bed";
final String OVER = "never";
main() {
Angel app;
http.Client client;
HttpServer server;
String url;
setUp(() async {
2017-08-15 23:01:16 +00:00
app = new Angel();
2016-09-17 16:12:25 +00:00
client = new http.Client();
// Inject some todos
app.container.singleton(new Todo(text: TEXT, over: OVER));
app.get("/errands", (Todo singleton) => singleton);
2016-11-23 19:50:17 +00:00
app.get("/errands3",
2017-01-20 22:11:20 +00:00
({Errand singleton, Todo foo, RequestContext req}) => singleton.text);
2017-10-04 14:09:12 +00:00
await app.configure(new SingletonController().configureServer);
await app.configure(new ErrandController().configureServer);
2016-09-17 16:12:25 +00:00
server = await app.startServer();
url = "http://${server.address.host}:${server.port}";
});
tearDown(() async {
app = null;
url = null;
client.close();
client = null;
await server.close(force: true);
});
test("singleton in route", () async {
validateTodoSingleton(await client.get("$url/errands"));
});
test("singleton in controller", () async {
validateTodoSingleton(await client.get("$url/errands2"));
});
test("make in route", () async {
var response = await client.get("$url/errands3");
String text = await JSON.decode(response.body);
expect(text, equals(TEXT));
});
test("make in controller", () async {
var response = await client.get("$url/errands4");
String text = await JSON.decode(response.body);
expect(text, equals(TEXT));
});
}
void validateTodoSingleton(response) {
Map todo = JSON.decode(response.body);
2017-02-23 00:37:15 +00:00
// expect(todo.keys.length, equals(3));
2016-09-17 16:12:25 +00:00
expect(todo["id"], equals(null));
expect(todo["text"], equals(TEXT));
expect(todo["over"], equals(OVER));
}
@Expose("/errands2")
class SingletonController extends Controller {
@Expose("/")
todo(Todo singleton) => singleton;
}
@Expose("/errands4")
class ErrandController extends Controller {
@Expose("/")
2017-11-18 17:42:31 +00:00
errand(Errand errand) {
2016-11-23 19:50:17 +00:00
return errand.text;
}
2016-09-17 16:12:25 +00:00
}
class Errand {
Todo todo;
String get text => todo.text;
Errand(this.todo);
2016-11-23 19:50:17 +00:00
}