2017-06-30 13:49:45 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
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-06-08 07:06:26 +00:00
|
|
|
import 'package:dart2_constant/convert.dart';
|
2017-06-30 13:49:45 +00:00
|
|
|
import 'package:matcher/matcher.dart';
|
|
|
|
import 'package:mock_request/mock_request.dart';
|
|
|
|
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 02:40:11 +00:00
|
|
|
var parent = new Angel(reflector: MirrorsReflector())..configuration['two'] = 2;
|
|
|
|
var child = new Angel(reflector: MirrorsReflector());
|
2017-06-30 13:49:45 +00:00
|
|
|
parent.use('/child', child);
|
|
|
|
|
|
|
|
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);
|
|
|
|
rq.close();
|
|
|
|
var rs = rq.response;
|
2018-02-07 04:34:08 +00:00
|
|
|
var req = await http.createRequestContext(rq);
|
|
|
|
var res = await http.createResponseContext(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-02-07 04:34:08 +00:00
|
|
|
await http.sendResponse(rq, req, res);
|
|
|
|
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 02:40:11 +00:00
|
|
|
var app = new Angel(reflector: MirrorsReflector())..optimizeForProduction(force: true);
|
2017-06-30 13:49:45 +00:00
|
|
|
app.dumpTree();
|
|
|
|
app.get('/', () => 2);
|
|
|
|
app.mount('/foo', new Router()..get('/', 3));
|
|
|
|
});
|
|
|
|
|
|
|
|
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 02:40:11 +00:00
|
|
|
var app = new Angel(reflector: MirrorsReflector())..inject('a', 'b');
|
2018-02-07 04:34:08 +00:00
|
|
|
var http = new AngelHttp(app);
|
2017-09-22 04:48:22 +00:00
|
|
|
app.get('/', (String a) => a);
|
|
|
|
var rq = new MockHttpRequest('GET', Uri.parse('/'))..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));
|
|
|
|
var rq = new MockHttpRequest('GET', $foo)..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 {
|
|
|
|
var rq = new MockHttpRequest('GET', $foo)..close();
|
2018-02-07 04:34:08 +00:00
|
|
|
req = await http.createRequestContext(rq);
|
|
|
|
res = await http.createResponseContext(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 {
|
|
|
|
var handler = (req, res) => res.end();
|
|
|
|
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());
|
2017-06-30 21:23:31 +00:00
|
|
|
app.get('/wtf', () => throw new AngelHttpException.forbidden());
|
|
|
|
app.get('/wtf2', () => 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');
|
2017-06-30 13:49:45 +00:00
|
|
|
rq.close();
|
2018-05-16 03:14:29 +00:00
|
|
|
http.handleRequest(rq);
|
|
|
|
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');
|
2017-06-30 13:49:45 +00:00
|
|
|
rq.close();
|
2018-05-16 03:14:29 +00:00
|
|
|
http.handleRequest(rq);
|
|
|
|
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');
|
2017-06-30 13:49:45 +00:00
|
|
|
rq.close();
|
2018-05-16 03:14:29 +00:00
|
|
|
http.handleRequest(rq);
|
|
|
|
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';
|
|
|
|
}
|