2017-06-30 13:49:45 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
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', () {
|
2017-09-24 19:43:14 +00:00
|
|
|
var parent = new Angel()..configuration['two'] = 2;
|
2017-06-30 13:49:45 +00:00
|
|
|
var child = new Angel();
|
|
|
|
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-02-07 04:34:08 +00:00
|
|
|
var app = new Angel();
|
|
|
|
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 {
|
|
|
|
var app = new Angel();
|
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(
|
|
|
|
ContentType.parse(rs.headers.value(HttpHeaders.CONTENT_TYPE)).mimeType,
|
|
|
|
ContentType.HTML.mimeType,
|
|
|
|
);
|
2017-06-30 13:49:45 +00:00
|
|
|
expect(rs.statusCode, e.statusCode);
|
|
|
|
var body = await rs.transform(UTF8.decoder).join();
|
|
|
|
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 {
|
|
|
|
var app = new Angel();
|
2017-09-22 04:48:22 +00:00
|
|
|
app.startupHooks.add((app) async {
|
2017-09-24 19:43:14 +00:00
|
|
|
app.configuration['two'] = 2;
|
2017-06-30 13:49:45 +00:00
|
|
|
});
|
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', () {
|
|
|
|
var app = new Angel()..optimizeForProduction(force: true);
|
|
|
|
app.dumpTree();
|
|
|
|
app.get('/', () => 2);
|
|
|
|
app.mount('/foo', new Router()..get('/', 3));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('services close on close call', () async {
|
|
|
|
var app = new Angel();
|
|
|
|
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 {
|
2017-06-30 13:49:45 +00:00
|
|
|
var app = new Angel()..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);
|
2017-09-22 04:48:22 +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-02-07 04:34:08 +00:00
|
|
|
var app = new Angel()..serializer = (_) => 'x';
|
|
|
|
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);
|
2017-06-30 13:49:45 +00:00
|
|
|
var body = await rq.response.transform(UTF8.decoder).join();
|
|
|
|
expect(body, 'x');
|
|
|
|
});
|
|
|
|
|
|
|
|
group('handler results', () {
|
|
|
|
var app = new Angel();
|
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 {
|
|
|
|
app = new Angel();
|
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);
|
|
|
|
await http.startServer(InternetAddress.LOOPBACK_IP_V4, 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'))
|
|
|
|
..headers.set(HttpHeaders.ACCEPT, ContentType.JSON.toString());
|
2017-06-30 13:49:45 +00:00
|
|
|
rq.close();
|
2018-02-07 04:34:08 +00:00
|
|
|
await http.handleRequest(rq);
|
2017-06-30 13:49:45 +00:00
|
|
|
expect(rq.response.statusCode, HttpStatus.FORBIDDEN);
|
|
|
|
expect(
|
|
|
|
rq.response.headers.contentType.mimeType, ContentType.JSON.mimeType);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can throw in finalizer', () async {
|
2017-10-04 14:09:12 +00:00
|
|
|
var rq = new MockHttpRequest('GET', new Uri(path: 'wtf'))
|
|
|
|
..headers.set(HttpHeaders.ACCEPT, ContentType.JSON.toString());
|
2017-06-30 13:49:45 +00:00
|
|
|
rq.close();
|
2018-02-07 04:34:08 +00:00
|
|
|
await http.handleRequest(rq);
|
2017-06-30 13:49:45 +00:00
|
|
|
expect(rq.response.statusCode, HttpStatus.FORBIDDEN);
|
|
|
|
expect(
|
|
|
|
rq.response.headers.contentType.mimeType, ContentType.JSON.mimeType);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can send html', () async {
|
|
|
|
var rq = new MockHttpRequest('GET', new Uri(path: 'wtf2'));
|
|
|
|
rq.headers.set(HttpHeaders.ACCEPT, ContentType.HTML.toString());
|
|
|
|
rq.close();
|
2018-02-07 04:34:08 +00:00
|
|
|
await http.handleRequest(rq);
|
2017-06-30 13:49:45 +00:00
|
|
|
expect(rq.response.statusCode, HttpStatus.FORBIDDEN);
|
|
|
|
expect(
|
2017-09-22 14:03:23 +00:00
|
|
|
rq.response.headers.contentType?.mimeType, ContentType.HTML.mimeType);
|
2017-06-30 13:49:45 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class CustomCloseService extends Service {
|
|
|
|
int value = 2;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future close() {
|
|
|
|
value = 3;
|
|
|
|
return super.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Expose('/foo')
|
|
|
|
class FooController extends Controller {
|
|
|
|
@Expose('/bar')
|
|
|
|
bar() async => 'baz';
|
|
|
|
}
|