2024-09-23 01:44:59 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
2024-09-25 04:04:57 +00:00
|
|
|
import 'package:platform_container/container.dart';
|
|
|
|
import 'package:platform_core/http.dart';
|
2024-09-23 01:44:59 +00:00
|
|
|
import 'package:platform_container/mirrors.dart';
|
2024-09-25 04:04:57 +00:00
|
|
|
import 'package:platform_core/core.dart';
|
2024-09-23 01:44:59 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
2024-11-11 19:49:32 +00:00
|
|
|
import 'package:platform_testing/http.dart';
|
2024-09-23 01:44:59 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
import 'common.dart';
|
|
|
|
|
|
|
|
final String sampleText = 'make your bed';
|
|
|
|
final String sampleOver = 'never';
|
|
|
|
|
|
|
|
void main() {
|
2024-09-28 23:14:48 +00:00
|
|
|
late Application app;
|
2024-09-23 01:44:59 +00:00
|
|
|
late http.Client client;
|
|
|
|
late HttpServer server;
|
|
|
|
String? url;
|
|
|
|
|
|
|
|
setUp(() async {
|
2024-09-28 23:14:48 +00:00
|
|
|
app = Application(reflector: MirrorsReflector());
|
2024-09-23 01:44:59 +00:00
|
|
|
client = http.Client();
|
|
|
|
|
|
|
|
// Inject some todos
|
|
|
|
app.container.registerSingleton(Todo(text: sampleText, over: sampleOver));
|
|
|
|
app.container.registerFactory<Future<Foo>>((container) async {
|
|
|
|
var req = container.make<RequestContext>();
|
|
|
|
var text = await utf8.decoder.bind(req.body!).join();
|
|
|
|
return Foo(text);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get('/errands', ioc((Todo singleton) => singleton));
|
|
|
|
app.get(
|
|
|
|
'/errands3',
|
|
|
|
ioc(({required Errand singleton, Todo? foo, RequestContext? req}) =>
|
|
|
|
singleton.text));
|
|
|
|
app.post('/async', ioc((Foo foo) => {'baz': foo.bar}));
|
|
|
|
await app.configure(SingletonController().configureServer);
|
|
|
|
await app.configure(ErrandController().configureServer);
|
|
|
|
|
2024-09-28 23:14:48 +00:00
|
|
|
server = await PlatformHttp(app).startServer();
|
2024-09-23 01:44:59 +00:00
|
|
|
url = 'http://${server.address.host}:${server.port}';
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
|
|
|
url = null;
|
|
|
|
client.close();
|
|
|
|
await server.close(force: true);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('runContained with custom container', () async {
|
2024-09-28 23:14:48 +00:00
|
|
|
var app = Application();
|
2024-09-23 01:44:59 +00:00
|
|
|
var c = Container(const MirrorsReflector());
|
|
|
|
c.registerSingleton(Todo(text: 'Hey!'));
|
|
|
|
|
|
|
|
app.get('/', (req, res) async {
|
|
|
|
return app.runContained((Todo t) => t.text, req, res, c);
|
|
|
|
});
|
|
|
|
|
|
|
|
var rq = MockHttpRequest('GET', Uri(path: '/'));
|
|
|
|
await rq.close();
|
|
|
|
var rs = rq.response;
|
2024-09-28 23:14:48 +00:00
|
|
|
await PlatformHttp(app).handleRequest(rq);
|
2024-09-23 01:44:59 +00:00
|
|
|
var text = await rs.transform(utf8.decoder).join();
|
|
|
|
expect(text, json.encode('Hey!'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('singleton in route', () async {
|
|
|
|
validateTodoSingleton(await client.get(Uri.parse('$url/errands')));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('singleton in controller', () async {
|
|
|
|
validateTodoSingleton(await client.get(Uri.parse('$url/errands2')));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('make in route', () async {
|
|
|
|
var response = await client.get(Uri.parse('$url/errands3'));
|
|
|
|
var text = await json.decode(response.body) as String?;
|
|
|
|
expect(text, equals(sampleText));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('make in controller', () async {
|
|
|
|
var response = await client.get(Uri.parse('$url/errands4'));
|
|
|
|
var text = await json.decode(response.body) as String?;
|
|
|
|
expect(text, equals(sampleText));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('resolve from future in controller', () async {
|
|
|
|
var response =
|
|
|
|
await client.post(Uri.parse('$url/errands4/async'), body: 'hey');
|
|
|
|
expect(response.body, json.encode({'bar': 'hey'}));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('resolve from future in route', () async {
|
|
|
|
var response = await client.post(Uri.parse('$url/async'), body: 'yes');
|
|
|
|
expect(response.body, json.encode({'baz': 'yes'}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void validateTodoSingleton(response) {
|
|
|
|
var todo = json.decode(response.body.toString()) as Map;
|
|
|
|
expect(todo['id'], equals(null));
|
|
|
|
expect(todo['text'], equals(sampleText));
|
|
|
|
expect(todo['over'], equals(sampleOver));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Expose('/errands2')
|
|
|
|
class SingletonController extends Controller {
|
|
|
|
@Expose('/')
|
|
|
|
Todo todo(Todo singleton) => singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Expose('/errands4')
|
|
|
|
class ErrandController extends Controller {
|
|
|
|
@Expose('/')
|
|
|
|
String? errand(Errand errand) {
|
|
|
|
return errand.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Expose('/async', method: 'POST')
|
|
|
|
Map<String, String> asyncResolve(Foo foo) {
|
|
|
|
return {'bar': foo.bar};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Foo {
|
|
|
|
final String bar;
|
|
|
|
|
|
|
|
Foo(this.bar);
|
|
|
|
}
|
|
|
|
|
|
|
|
class Errand {
|
|
|
|
Todo todo;
|
|
|
|
|
|
|
|
String? get text => todo.text;
|
|
|
|
|
|
|
|
Errand(this.todo);
|
|
|
|
}
|