platform/framework/test/di_test.dart

143 lines
3.7 KiB
Dart
Raw Normal View History

2019-04-17 15:15:46 +00:00
import 'dart:async';
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 {
2019-05-02 22:48:31 +00:00
app = Angel(reflector: MirrorsReflector());
client = http.Client();
2016-09-17 16:12:25 +00:00
// Inject some todos
2019-05-02 22:48:31 +00:00
app.container.registerSingleton(Todo(text: TEXT, over: OVER));
2019-04-17 15:15:46 +00:00
app.container.registerFactory<Future<Foo>>((container) async {
var req = container.make<RequestContext>();
2019-09-29 05:38:22 +00:00
var text = await utf8.decoder.bind(req.body).join();
2019-04-17 15:15:46 +00:00
return Foo(text);
});
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));
2019-04-17 15:15:46 +00:00
app.post('/async', ioc((Foo foo) => {'baz': foo.bar}));
2019-05-02 22:48:31 +00:00
await app.configure(SingletonController().configureServer);
await app.configure(ErrandController().configureServer);
2016-09-17 16:12:25 +00:00
2019-05-02 22:48:31 +00:00
server = await 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 {
2019-05-02 22:48:31 +00:00
var app = Angel();
var c = Container(const MirrorsReflector());
c.registerSingleton(Todo(text: 'Hey!'));
2018-10-21 08:00:39 +00:00
2018-10-21 08:08:08 +00:00
app.get('/', (req, res) async {
return app.runContained((Todo t) => t.text, req, res, c);
});
2019-05-02 22:48:31 +00:00
var rq = MockHttpRequest('GET', Uri(path: '/'));
2019-04-17 19:42:24 +00:00
await rq.close();
2018-10-21 08:08:08 +00:00
var rs = rq.response;
2019-05-02 22:48:31 +00:00
await AngelHttp(app).handleRequest(rq);
2018-10-21 08:08:08 +00:00
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");
2019-04-08 19:53:07 +00:00
var text = await json.decode(response.body) as String;
2016-09-17 16:12:25 +00:00
expect(text, equals(TEXT));
});
test("make in controller", () async {
var response = await client.get("$url/errands4");
2019-04-08 19:53:07 +00:00
var text = await json.decode(response.body) as String;
2016-09-17 16:12:25 +00:00
expect(text, equals(TEXT));
});
2019-04-17 15:15:46 +00:00
test('resolve from future in controller', () async {
var response = await client.post('$url/errands4/async', body: 'hey');
expect(response.body, json.encode({'bar': 'hey'}));
});
test('resolve from future in route', () async {
var response = await client.post('$url/async', body: 'yes');
expect(response.body, json.encode({'baz': 'yes'}));
});
2016-09-17 16:12:25 +00:00
}
void validateTodoSingleton(response) {
2019-04-08 19:53:07 +00:00
var todo = json.decode(response.body.toString()) as Map;
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;
}
2019-04-17 15:15:46 +00:00
@Expose('/async', method: 'POST')
asyncResolve(Foo foo) {
return {'bar': foo.bar};
}
}
class Foo {
final String bar;
Foo(this.bar);
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
}