platform/packages/framework/test/di_test.dart

142 lines
3.9 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';
2021-05-14 10:34:09 +00:00
import 'package:angel3_container/angel3_container.dart';
import 'package:angel3_framework/http.dart';
import 'package:angel3_container/mirrors.dart';
import 'package:angel3_framework/angel3_framework.dart';
2016-09-17 16:12:25 +00:00
import 'package:http/http.dart' as http;
2021-05-14 10:34:09 +00:00
import 'package:angel3_mock_request/angel3_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';
2022-08-17 03:05:48 +00:00
final String sampleText = 'make your bed';
final String sampleOver = 'never';
2016-09-17 16:12:25 +00:00
2021-05-14 10:34:09 +00:00
void main() {
2021-04-03 05:50:52 +00:00
late Angel app;
late http.Client client;
2021-03-20 08:11:18 +00:00
late HttpServer server;
String? url;
2016-09-17 16:12:25 +00:00
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
2022-08-17 03:05:48 +00:00
app.container.registerSingleton(Todo(text: sampleText, over: sampleOver));
2021-11-28 04:15:56 +00:00
app.container.registerFactory<Future<Foo>>((container) async {
2021-10-04 05:13:56 +00:00
var req = container.make<RequestContext>();
2021-03-20 08:11:18 +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
2021-07-08 02:42:40 +00:00
app.get('/errands', ioc((Todo singleton) => singleton));
2021-04-03 05:50:52 +00:00
app.get(
2021-07-08 02:42:40 +00:00
'/errands3',
2021-03-20 08:11:18 +00:00
ioc(({required Errand singleton, Todo? foo, RequestContext? req}) =>
singleton.text));
2021-04-03 05:50:52 +00:00
app.post('/async', ioc((Foo foo) => {'baz': foo.bar}));
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();
2021-07-08 02:42:40 +00:00
url = 'http://${server.address.host}:${server.port}';
2016-09-17 16:12:25 +00:00
});
tearDown(() async {
url = null;
2021-04-03 05:50:52 +00:00
client.close();
2016-09-17 16:12:25 +00:00
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
});
2021-07-08 02:42:40 +00:00
test('singleton in route', () async {
validateTodoSingleton(await client.get(Uri.parse('$url/errands')));
2016-09-17 16:12:25 +00:00
});
2021-07-08 02:42:40 +00:00
test('singleton in controller', () async {
validateTodoSingleton(await client.get(Uri.parse('$url/errands2')));
2016-09-17 16:12:25 +00:00
});
2021-07-08 02:42:40 +00:00
test('make in route', () async {
var response = await client.get(Uri.parse('$url/errands3'));
2021-03-20 08:11:18 +00:00
var text = await json.decode(response.body) as String?;
2022-08-17 03:05:48 +00:00
expect(text, equals(sampleText));
2016-09-17 16:12:25 +00:00
});
2021-07-08 02:42:40 +00:00
test('make in controller', () async {
var response = await client.get(Uri.parse('$url/errands4'));
2021-03-20 08:11:18 +00:00
var text = await json.decode(response.body) as String?;
2022-08-17 03:05:48 +00:00
expect(text, equals(sampleText));
2016-09-17 16:12:25 +00:00
});
2019-04-17 15:15:46 +00:00
test('resolve from future in controller', () async {
2021-03-06 14:10:24 +00:00
var response =
2021-04-03 05:50:52 +00:00
await client.post(Uri.parse('$url/errands4/async'), body: 'hey');
2019-04-17 15:15:46 +00:00
expect(response.body, json.encode({'bar': 'hey'}));
});
test('resolve from future in route', () async {
2021-04-03 05:50:52 +00:00
var response = await client.post(Uri.parse('$url/async'), body: 'yes');
2019-04-17 15:15:46 +00:00
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;
2021-07-08 02:42:40 +00:00
expect(todo['id'], equals(null));
2022-08-17 03:05:48 +00:00
expect(todo['text'], equals(sampleText));
expect(todo['over'], equals(sampleOver));
2016-09-17 16:12:25 +00:00
}
2021-07-08 02:42:40 +00:00
@Expose('/errands2')
2016-09-17 16:12:25 +00:00
class SingletonController extends Controller {
2021-07-08 02:42:40 +00:00
@Expose('/')
Todo todo(Todo singleton) => singleton;
2016-09-17 16:12:25 +00:00
}
2021-07-08 02:42:40 +00:00
@Expose('/errands4')
2016-09-17 16:12:25 +00:00
class ErrandController extends Controller {
2021-07-08 02:42:40 +00:00
@Expose('/')
String? 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')
2021-07-08 02:42:40 +00:00
Map<String, String> asyncResolve(Foo foo) {
2019-04-17 15:15:46 +00:00
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
2021-03-20 08:11:18 +00:00
String? get text => todo.text;
2016-09-17 16:12:25 +00:00
Errand(this.todo);
2016-11-23 19:50:17 +00:00
}