2018-08-20 03:06:29 +00:00
|
|
|
import 'dart:convert';
|
2018-06-08 07:06:26 +00:00
|
|
|
import 'dart:io' show stderr;
|
2018-08-20 20:43:38 +00:00
|
|
|
|
2021-05-14 10:34:09 +00:00
|
|
|
import 'package:angel3_container/mirrors.dart';
|
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
|
|
|
import 'package:angel3_framework/http.dart';
|
|
|
|
import 'package:angel3_mock_request/angel3_mock_request.dart';
|
2020-02-05 23:02:46 +00:00
|
|
|
|
2017-08-28 15:29:27 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2021-05-14 10:34:09 +00:00
|
|
|
void main() {
|
2021-03-20 08:11:18 +00:00
|
|
|
late Angel app;
|
|
|
|
late AngelHttp http;
|
2017-08-28 15:29:27 +00:00
|
|
|
|
|
|
|
setUp(() {
|
2019-05-02 22:48:31 +00:00
|
|
|
app = Angel(reflector: MirrorsReflector())
|
2018-08-20 20:53:30 +00:00
|
|
|
..configuration['global'] = 305; // Pitbull!
|
2019-05-02 22:48:31 +00:00
|
|
|
http = AngelHttp(app);
|
2017-08-28 15:29:27 +00:00
|
|
|
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/string/:string', ioc((String string) => string));
|
2017-08-28 15:29:27 +00:00
|
|
|
|
|
|
|
app.get(
|
|
|
|
'/num/parsed/:num',
|
2018-09-12 04:17:50 +00:00
|
|
|
chain([
|
2018-08-20 20:43:38 +00:00
|
|
|
(req, res) {
|
2018-06-23 03:29:38 +00:00
|
|
|
req.params['n'] = num.parse(req.params['num'].toString());
|
2017-08-28 15:29:27 +00:00
|
|
|
return true;
|
|
|
|
},
|
2018-08-20 20:43:38 +00:00
|
|
|
ioc((num n) => n),
|
2017-08-28 15:29:27 +00:00
|
|
|
]));
|
|
|
|
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/num/global', ioc((num global) => global));
|
2017-08-28 15:29:27 +00:00
|
|
|
|
2017-09-22 14:03:23 +00:00
|
|
|
app.errorHandler = (e, req, res) {
|
|
|
|
stderr..writeln(e.error)..writeln(e.stackTrace);
|
|
|
|
};
|
2017-08-28 15:29:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() => app.close());
|
|
|
|
|
|
|
|
test('String type annotation', () async {
|
2019-05-02 22:48:31 +00:00
|
|
|
var rq = MockHttpRequest('GET', Uri.parse('/string/hello'));
|
2020-02-05 23:02:46 +00:00
|
|
|
(rq.close());
|
2018-02-07 04:34:08 +00:00
|
|
|
await http.handleRequest(rq);
|
2018-06-08 07:06:26 +00:00
|
|
|
var rs = await rq.response.transform(utf8.decoder).join();
|
|
|
|
expect(rs, json.encode('hello'));
|
2017-08-28 15:29:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Primitive after parsed param injection', () async {
|
2019-05-02 22:48:31 +00:00
|
|
|
var rq = MockHttpRequest('GET', Uri.parse('/num/parsed/24'));
|
2020-02-05 23:02:46 +00:00
|
|
|
(rq.close());
|
2018-02-07 04:34:08 +00:00
|
|
|
await http.handleRequest(rq);
|
2018-06-08 07:06:26 +00:00
|
|
|
var rs = await rq.response.transform(utf8.decoder).join();
|
|
|
|
expect(rs, json.encode(24));
|
2017-08-28 15:29:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('globally-injected primitive', () async {
|
2019-05-02 22:48:31 +00:00
|
|
|
var rq = MockHttpRequest('GET', Uri.parse('/num/global'));
|
2020-02-05 23:02:46 +00:00
|
|
|
(rq.close());
|
2018-02-07 04:34:08 +00:00
|
|
|
await http.handleRequest(rq);
|
2018-06-08 07:06:26 +00:00
|
|
|
var rs = await rq.response.transform(utf8.decoder).join();
|
|
|
|
expect(rs, json.encode(305));
|
2017-08-28 15:29:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('unparsed primitive throws error', () async {
|
|
|
|
try {
|
2019-05-02 22:48:31 +00:00
|
|
|
var rq = MockHttpRequest('GET', Uri.parse('/num/unparsed/32'));
|
2020-02-05 23:02:46 +00:00
|
|
|
(rq.close());
|
2018-11-11 01:07:09 +00:00
|
|
|
var req = await http.createRequestContext(rq, rq.response);
|
|
|
|
var res = await http.createResponseContext(rq, rq.response, req);
|
2017-08-28 15:29:27 +00:00
|
|
|
await app.runContained((num unparsed) => unparsed, req, res);
|
2019-05-02 22:48:31 +00:00
|
|
|
throw StateError(
|
2017-09-22 14:03:23 +00:00
|
|
|
'ArgumentError should be thrown if a parameter cannot be resolved.');
|
2017-08-28 15:29:27 +00:00
|
|
|
} on ArgumentError {
|
|
|
|
// Success
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|