platform/test/di_test.dart

115 lines
2.9 KiB
Dart
Raw Normal View History

import 'dart:convert';
2016-09-17 16:12:25 +00:00
import 'dart:io';
2018-10-21 08:00:39 +00:00
import 'package:angel_container/angel_container.dart';
2018-10-22 15:54:41 +00:00
import 'package:angel_framework/http.dart';
2018-08-19 15:33:25 +00:00
import 'package:angel_container/mirrors.dart';
2016-09-17 16:12:25 +00:00
import 'package:angel_framework/angel_framework.dart';
import 'package:http/http.dart' as http;
2018-10-21 08:08:08 +00:00
import 'package:mock_request/mock_request.dart';
2016-09-17 16:12:25 +00:00
import 'package:test/test.dart';
2016-09-17 16:12:25 +00:00
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 {
2018-08-20 02:40:11 +00:00
app = new Angel(reflector: MirrorsReflector());
2016-09-17 16:12:25 +00:00
client = new http.Client();
// Inject some todos
app.container.registerSingleton(new Todo(text: TEXT, over: OVER));
2016-09-17 16:12:25 +00:00
app.get("/errands", ioc((Todo singleton) => singleton));
app.get(
"/errands3",
ioc(({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
2018-02-07 05:36:24 +00:00
server = await new AngelHttp(app).startServer();
2016-09-17 16:12:25 +00:00
url = "http://${server.address.host}:${server.port}";
});
tearDown(() async {
app = null;
url = null;
client.close();
client = null;
await server.close(force: true);
});
2018-10-21 08:00:39 +00:00
test('runContained with custom container', () async {
var app = new Angel();
2018-10-21 08:08:08 +00:00
var c = new Container(const MirrorsReflector());
2018-10-21 08:00:39 +00:00
c.registerSingleton(new Todo(text: 'Hey!'));
2018-10-21 08:08:08 +00:00
app.get('/', (req, res) async {
return app.runContained((Todo t) => t.text, req, res, c);
});
var rq = new MockHttpRequest('GET', new Uri(path: '/'))..close();
var rs = rq.response;
await new AngelHttp(app).handleRequest(rq);
var text = await rs.transform(utf8.decoder).join();
expect(text, json.encode('Hey!'));
2018-10-21 08:00:39 +00:00
});
2016-09-17 16:12:25 +00:00
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");
2018-05-16 02:05:13 +00:00
String text = await json.decode(response.body);
2016-09-17 16:12:25 +00:00
expect(text, equals(TEXT));
});
test("make in controller", () async {
var response = await client.get("$url/errands4");
2018-05-16 02:05:13 +00:00
String text = await json.decode(response.body);
2016-09-17 16:12:25 +00:00
expect(text, equals(TEXT));
});
}
void validateTodoSingleton(response) {
2018-06-23 03:29:38 +00:00
Map todo = json.decode(response.body.toString());
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;
2018-05-16 02:05:13 +00:00
2016-09-17 16:12:25 +00:00
String get text => todo.text;
Errand(this.todo);
2016-11-23 19:50:17 +00:00
}