2017-06-30 13:49:45 +00:00
|
|
|
import 'dart:async';
|
2018-08-20 20:43:38 +00:00
|
|
|
import 'dart:convert';
|
2017-06-30 13:49:45 +00:00
|
|
|
import 'dart:io';
|
2018-08-20 20:43:38 +00:00
|
|
|
|
2018-08-19 15:33:25 +00:00
|
|
|
import 'package:angel_container/mirrors.dart';
|
2017-06-30 13:49:45 +00:00
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2018-10-22 15:54:41 +00:00
|
|
|
import 'package:angel_framework/http.dart';
|
2017-06-30 13:49:45 +00:00
|
|
|
import 'package:matcher/matcher.dart';
|
|
|
|
import 'package:mock_request/mock_request.dart';
|
2019-04-17 19:42:24 +00:00
|
|
|
import 'package:pedantic/pedantic.dart';
|
2017-06-30 13:49:45 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
final Uri $foo = Uri.parse('http://localhost:3000/foo');
|
|
|
|
|
|
|
|
/// Additional tests to improve coverage of server.dart
|
|
|
|
main() {
|
|
|
|
group('scoping', () {
|
2018-08-20 20:43:38 +00:00
|
|
|
var parent = new Angel(reflector: MirrorsReflector())
|
|
|
|
..configuration['two'] = 2;
|
2018-08-20 02:40:11 +00:00
|
|
|
var child = new Angel(reflector: MirrorsReflector());
|
2018-08-21 01:05:05 +00:00
|
|
|
parent.mount('/child', child);
|
2017-06-30 13:49:45 +00:00
|
|
|
|
|
|
|
test('sets children', () {
|
|
|
|
expect(parent.children, contains(child));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('sets parent', () {
|
|
|
|
expect(child.parent, parent);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('properties can climb up hierarchy', () {
|
|
|
|
expect(child.findProperty('two'), 2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('custom server generator', () {
|
2018-08-20 02:40:11 +00:00
|
|
|
var app = new Angel(reflector: MirrorsReflector());
|
2018-02-07 04:34:08 +00:00
|
|
|
var http = new AngelHttp.custom(app, HttpServer.bind);
|
|
|
|
expect(http.serverGenerator, HttpServer.bind);
|
2017-06-30 13:49:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('default error handler', () async {
|
2018-08-20 02:40:11 +00:00
|
|
|
var app = new Angel(reflector: MirrorsReflector());
|
2018-02-07 04:34:08 +00:00
|
|
|
var http = new AngelHttp(app);
|
2017-06-30 13:49:45 +00:00
|
|
|
var rq = new MockHttpRequest('GET', $foo);
|
2019-04-17 19:42:24 +00:00
|
|
|
unawaited(rq.close());
|
2017-06-30 13:49:45 +00:00
|
|
|
var rs = rq.response;
|
2018-11-11 01:07:09 +00:00
|
|
|
var req = await http.createRequestContext(rq, rs);
|
|
|
|
var res = await http.createResponseContext(rq, rs);
|
2017-06-30 13:49:45 +00:00
|
|
|
var e = new AngelHttpException(null,
|
|
|
|
statusCode: 321, message: 'Hello', errors: ['foo', 'bar']);
|
|
|
|
await app.errorHandler(e, req, res);
|
2018-11-11 01:07:09 +00:00
|
|
|
await http.sendResponse(rq, rs, req, res);
|
2018-02-07 04:34:08 +00:00
|
|
|
expect(
|
2018-06-23 03:29:38 +00:00
|
|
|
ContentType.parse(rs.headers.value('content-type')).mimeType,
|
|
|
|
'text/html',
|
2018-02-07 04:34:08 +00:00
|
|
|
);
|
2017-06-30 13:49:45 +00:00
|
|
|
expect(rs.statusCode, e.statusCode);
|
2018-06-08 07:06:26 +00:00
|
|
|
var body = await rs.transform(utf8.decoder).join();
|
2017-06-30 13:49:45 +00:00
|
|
|
expect(body, contains('<title>${e.message}</title>'));
|
|
|
|
expect(body, contains('<li>foo</li>'));
|
|
|
|
expect(body, contains('<li>bar</li>'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('plug-ins run on startup', () async {
|
2018-08-20 02:40:11 +00:00
|
|
|
var app = new Angel(reflector: MirrorsReflector());
|
2018-06-08 07:06:26 +00:00
|
|
|
app.startupHooks.add((app) => app.configuration['two'] = 2);
|
2018-02-07 05:36:24 +00:00
|
|
|
|
|
|
|
var http = new AngelHttp(app);
|
|
|
|
await http.startServer();
|
2017-09-24 19:43:14 +00:00
|
|
|
expect(app.configuration['two'], 2);
|
2017-06-30 13:49:45 +00:00
|
|
|
await app.close();
|
2018-02-07 05:36:24 +00:00
|
|
|
await http.close();
|
2017-06-30 13:49:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('warning when adding routes to flattened router', () {
|
2018-08-20 20:43:38 +00:00
|
|
|
var app = new Angel(reflector: MirrorsReflector())
|
|
|
|
..optimizeForProduction(force: true);
|
2017-06-30 13:49:45 +00:00
|
|
|
app.dumpTree();
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/', (req, res) => 2);
|
|
|
|
app.mount('/foo', new Router()..get('/', (req, res) => 3));
|
2017-06-30 13:49:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('services close on close call', () async {
|
2018-08-20 02:40:11 +00:00
|
|
|
var app = new Angel(reflector: MirrorsReflector());
|
2017-06-30 13:49:45 +00:00
|
|
|
var svc = new CustomCloseService();
|
|
|
|
expect(svc.value, 2);
|
|
|
|
app.use('/', svc);
|
|
|
|
await app.close();
|
|
|
|
expect(svc.value, 3);
|
|
|
|
});
|
|
|
|
|
2017-09-22 04:48:22 +00:00
|
|
|
test('global injection added to injection map', () async {
|
2018-08-20 20:53:30 +00:00
|
|
|
var app = new Angel(reflector: MirrorsReflector())
|
|
|
|
..configuration['a'] = 'b';
|
2018-02-07 04:34:08 +00:00
|
|
|
var http = new AngelHttp(app);
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/', ioc((String a) => a));
|
2019-04-17 19:42:24 +00:00
|
|
|
var rq = new MockHttpRequest('GET', Uri.parse('/'));
|
|
|
|
unawaited(rq.close());
|
2018-02-07 04:34:08 +00:00
|
|
|
await http.handleRequest(rq);
|
2018-06-08 07:06:26 +00:00
|
|
|
var body = await rq.response.transform(utf8.decoder).join();
|
|
|
|
expect(body, json.encode('b'));
|
2017-06-30 13:49:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('global injected serializer', () async {
|
2018-08-20 02:40:11 +00:00
|
|
|
var app = new Angel(reflector: MirrorsReflector())..serializer = (_) => 'x';
|
2018-02-07 04:34:08 +00:00
|
|
|
var http = new AngelHttp(app);
|
2017-06-30 13:49:45 +00:00
|
|
|
app.get($foo.path, (req, ResponseContext res) => res.serialize(null));
|
2019-04-17 19:42:24 +00:00
|
|
|
var rq = new MockHttpRequest('GET', $foo);
|
|
|
|
unawaited(rq.close());
|
2018-02-07 04:34:08 +00:00
|
|
|
await http.handleRequest(rq);
|
2018-06-08 07:06:26 +00:00
|
|
|
var body = await rq.response.transform(utf8.decoder).join();
|
2017-06-30 13:49:45 +00:00
|
|
|
expect(body, 'x');
|
|
|
|
});
|
|
|
|
|
|
|
|
group('handler results', () {
|
2018-08-20 02:40:11 +00:00
|
|
|
var app = new Angel(reflector: MirrorsReflector());
|
2018-02-07 04:34:08 +00:00
|
|
|
var http = new AngelHttp(app);
|
2017-06-30 21:23:31 +00:00
|
|
|
app.responseFinalizers
|
|
|
|
.add((req, res) => throw new AngelHttpException.forbidden());
|
2017-06-30 13:49:45 +00:00
|
|
|
RequestContext req;
|
|
|
|
ResponseContext res;
|
|
|
|
|
|
|
|
setUp(() async {
|
2019-04-17 19:42:24 +00:00
|
|
|
var rq = new MockHttpRequest('GET', $foo);
|
|
|
|
unawaited(rq.close());
|
2018-11-11 01:07:09 +00:00
|
|
|
req = await http.createRequestContext(rq, rq.response);
|
|
|
|
res = await http.createResponseContext(rq, rq.response);
|
2017-06-30 13:49:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
group('getHandlerResult', () {
|
|
|
|
test('return request handler', () async {
|
|
|
|
var handler = (req, res) => (req, res) async {
|
|
|
|
return 2;
|
|
|
|
};
|
|
|
|
var r = await app.getHandlerResult(handler, req, res);
|
|
|
|
expect(r, 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('return future', () async {
|
|
|
|
var handler = new Future.value(2);
|
|
|
|
expect(await app.getHandlerResult(handler, req, res), 2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group('executeHandler', () {
|
|
|
|
test('return Stream', () async {
|
|
|
|
var handler = (req, res) => new Stream.fromIterable([2, 3]);
|
|
|
|
expect(await app.executeHandler(handler, req, res), isFalse);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('end response', () async {
|
2018-08-21 18:50:43 +00:00
|
|
|
var handler = (req, ResponseContext res) => res.close();
|
2017-06-30 13:49:45 +00:00
|
|
|
expect(await app.executeHandler(handler, req, res), isFalse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group('handleAngelHttpException', () {
|
|
|
|
Angel app;
|
2018-02-07 04:34:08 +00:00
|
|
|
AngelHttp http;
|
2017-06-30 13:49:45 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2018-08-20 02:40:11 +00:00
|
|
|
app = new Angel(reflector: MirrorsReflector());
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/wtf', (req, res) => throw new AngelHttpException.forbidden());
|
|
|
|
app.get('/wtf2', (req, res) => throw new AngelHttpException.forbidden());
|
2018-02-07 04:34:08 +00:00
|
|
|
http = new AngelHttp(app);
|
2018-06-08 07:06:26 +00:00
|
|
|
await http.startServer('127.0.0.1', 0);
|
2017-09-22 14:03:23 +00:00
|
|
|
|
|
|
|
var oldHandler = app.errorHandler;
|
|
|
|
app.errorHandler = (e, req, res) {
|
|
|
|
print('FATAL: ${e.error ?? e}');
|
|
|
|
print(e.stackTrace);
|
|
|
|
return oldHandler(e, req, res);
|
|
|
|
};
|
2017-06-30 13:49:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() => app.close());
|
|
|
|
|
|
|
|
test('can send json', () async {
|
2017-10-04 14:09:12 +00:00
|
|
|
var rq = new MockHttpRequest('GET', new Uri(path: 'wtf'))
|
2018-06-23 03:29:38 +00:00
|
|
|
..headers.set('accept', 'application/json');
|
2019-04-17 19:42:24 +00:00
|
|
|
unawaited(rq.close());
|
|
|
|
unawaited(http.handleRequest(rq));
|
2018-05-16 03:14:29 +00:00
|
|
|
await rq.response.toList();
|
2018-06-23 03:29:38 +00:00
|
|
|
expect(rq.response.statusCode, 403);
|
2018-06-23 03:59:41 +00:00
|
|
|
expect(rq.response.headers.contentType.mimeType, 'application/json');
|
2017-06-30 13:49:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('can throw in finalizer', () async {
|
2017-10-04 14:09:12 +00:00
|
|
|
var rq = new MockHttpRequest('GET', new Uri(path: 'wtf'))
|
2018-06-23 03:29:38 +00:00
|
|
|
..headers.set('accept', 'application/json');
|
2019-04-17 19:42:24 +00:00
|
|
|
unawaited(rq.close());
|
|
|
|
unawaited(http.handleRequest(rq));
|
2018-05-16 03:14:29 +00:00
|
|
|
await rq.response.toList();
|
2018-06-23 03:29:38 +00:00
|
|
|
expect(rq.response.statusCode, 403);
|
2018-06-23 03:59:41 +00:00
|
|
|
expect(rq.response.headers.contentType.mimeType, 'application/json');
|
2017-06-30 13:49:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('can send html', () async {
|
|
|
|
var rq = new MockHttpRequest('GET', new Uri(path: 'wtf2'));
|
2018-06-23 03:29:38 +00:00
|
|
|
rq.headers.set('accept', 'text/html');
|
2019-04-17 19:42:24 +00:00
|
|
|
unawaited(rq.close());
|
|
|
|
unawaited(http.handleRequest(rq));
|
2018-05-16 03:14:29 +00:00
|
|
|
await rq.response.toList();
|
2018-06-23 03:29:38 +00:00
|
|
|
expect(rq.response.statusCode, 403);
|
2018-06-23 03:59:41 +00:00
|
|
|
expect(rq.response.headers.contentType?.mimeType, 'text/html');
|
2017-06-30 13:49:45 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class CustomCloseService extends Service {
|
|
|
|
int value = 2;
|
|
|
|
|
|
|
|
@override
|
2018-06-08 07:06:26 +00:00
|
|
|
void close() {
|
2017-06-30 13:49:45 +00:00
|
|
|
value = 3;
|
2018-06-08 07:06:26 +00:00
|
|
|
super.close();
|
2017-06-30 13:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Expose('/foo')
|
|
|
|
class FooController extends Controller {
|
|
|
|
@Expose('/bar')
|
|
|
|
bar() async => 'baz';
|
|
|
|
}
|