2024-09-23 01:44:59 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io' show stderr;
|
|
|
|
|
|
|
|
import 'package:platform_container/mirrors.dart';
|
|
|
|
import 'package:platform_framework/platform_framework.dart';
|
|
|
|
import 'package:platform_framework/http.dart';
|
2024-09-23 07:54:31 +00:00
|
|
|
import 'package:platform_mocking/platform_mocking.dart';
|
2024-09-23 01:44:59 +00:00
|
|
|
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
void main() {
|
2024-09-23 04:39:29 +00:00
|
|
|
late Protevus app;
|
|
|
|
late ProtevusHttp http;
|
2024-09-23 01:44:59 +00:00
|
|
|
|
|
|
|
setUp(() {
|
2024-09-23 04:39:29 +00:00
|
|
|
app = Protevus(reflector: MirrorsReflector())
|
2024-09-23 01:44:59 +00:00
|
|
|
..configuration['global'] = 305; // Pitbull!
|
2024-09-23 04:39:29 +00:00
|
|
|
http = ProtevusHttp(app);
|
2024-09-23 01:44:59 +00:00
|
|
|
|
|
|
|
app.get('/string/:string', ioc((String string) => string));
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
'/num/parsed/:num',
|
|
|
|
chain([
|
|
|
|
(req, res) {
|
|
|
|
req.params['n'] = num.parse(req.params['num'].toString());
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
ioc((num n) => n),
|
|
|
|
]));
|
|
|
|
|
|
|
|
app.get('/num/global', ioc((num global) => global));
|
|
|
|
|
|
|
|
app.errorHandler = (e, req, res) {
|
|
|
|
stderr
|
|
|
|
..writeln(e.error)
|
|
|
|
..writeln(e.stackTrace);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() => app.close());
|
|
|
|
|
|
|
|
test('String type annotation', () async {
|
|
|
|
var rq = MockHttpRequest('GET', Uri.parse('/string/hello'));
|
|
|
|
await rq.close();
|
|
|
|
await http.handleRequest(rq);
|
|
|
|
var rs = await rq.response.transform(utf8.decoder).join();
|
|
|
|
expect(rs, json.encode('hello'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Primitive after parsed param injection', () async {
|
|
|
|
var rq = MockHttpRequest('GET', Uri.parse('/num/parsed/24'));
|
|
|
|
await rq.close();
|
|
|
|
await http.handleRequest(rq);
|
|
|
|
var rs = await rq.response.transform(utf8.decoder).join();
|
|
|
|
expect(rs, json.encode(24));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('globally-injected primitive', () async {
|
|
|
|
var rq = MockHttpRequest('GET', Uri.parse('/num/global'));
|
|
|
|
await rq.close();
|
|
|
|
await http.handleRequest(rq);
|
|
|
|
var rs = await rq.response.transform(utf8.decoder).join();
|
|
|
|
expect(rs, json.encode(305));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('unparsed primitive throws error', () async {
|
|
|
|
var rq = MockHttpRequest('GET', Uri.parse('/num/unparsed/32'));
|
|
|
|
await rq.close();
|
|
|
|
var req = await http.createRequestContext(rq, rq.response);
|
|
|
|
var res = await http.createResponseContext(rq, rq.response, req);
|
|
|
|
expect(() => app.runContained((num unparsed) => unparsed, req, res),
|
|
|
|
throwsA(isA<ArgumentError>()));
|
|
|
|
});
|
|
|
|
}
|