2018-08-20 20:21:06 +00:00
|
|
|
import 'dart:convert';
|
2018-07-09 15:02:00 +00:00
|
|
|
import 'dart:io';
|
2018-08-20 20:21:06 +00:00
|
|
|
|
2018-08-19 15:33:25 +00:00
|
|
|
import 'package:angel_container/mirrors.dart';
|
2016-04-18 03:27:23 +00:00
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2018-10-22 15:54:41 +00:00
|
|
|
import 'package:angel_framework/http.dart';
|
2016-04-18 03:27:23 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
2018-07-09 15:02:00 +00:00
|
|
|
import 'package:io/ansi.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
2016-04-18 03:27:23 +00:00
|
|
|
import 'package:test/test.dart';
|
2018-08-20 20:21:06 +00:00
|
|
|
|
2018-07-09 14:28:32 +00:00
|
|
|
import 'common.dart';
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2018-08-20 20:21:06 +00:00
|
|
|
@Middleware([interceptor])
|
2016-05-02 22:28:14 +00:00
|
|
|
testMiddlewareMetadata(RequestContext req, ResponseContext res) async {
|
|
|
|
return "This should not be shown.";
|
|
|
|
}
|
|
|
|
|
2018-08-20 20:21:06 +00:00
|
|
|
@Middleware([interceptService])
|
2016-06-23 19:05:55 +00:00
|
|
|
class QueryService extends Service {
|
|
|
|
@override
|
2018-08-20 20:21:06 +00:00
|
|
|
@Middleware([interceptor])
|
2017-08-03 16:40:21 +00:00
|
|
|
read(id, [Map params]) async => params;
|
2016-06-23 19:05:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 20:21:06 +00:00
|
|
|
void interceptor(RequestContext req, ResponseContext res) {
|
|
|
|
res
|
|
|
|
..write('Middleware')
|
2018-08-21 01:57:26 +00:00
|
|
|
..close();
|
2018-08-20 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 01:07:09 +00:00
|
|
|
bool interceptService(RequestContext req, ResponseContext res) {
|
2018-08-20 20:21:06 +00:00
|
|
|
res.write("Service with ");
|
2018-11-11 01:07:09 +00:00
|
|
|
return true;
|
2018-08-20 20:21:06 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 03:27:23 +00:00
|
|
|
main() {
|
2016-11-28 00:49:27 +00:00
|
|
|
Angel app;
|
2016-09-15 19:53:01 +00:00
|
|
|
Angel nested;
|
|
|
|
Angel todos;
|
|
|
|
String url;
|
|
|
|
http.Client client;
|
|
|
|
|
|
|
|
setUp(() async {
|
2019-05-02 22:48:31 +00:00
|
|
|
app = Angel(reflector: MirrorsReflector());
|
|
|
|
nested = Angel(reflector: MirrorsReflector());
|
|
|
|
todos = Angel(reflector: MirrorsReflector());
|
2016-10-22 20:41:36 +00:00
|
|
|
|
2017-04-01 01:00:24 +00:00
|
|
|
[app, nested, todos].forEach((Angel app) {
|
2019-05-02 22:48:31 +00:00
|
|
|
app.logger = Logger('routing_test')
|
2018-07-09 15:02:00 +00:00
|
|
|
..onRecord.listen((rec) {
|
|
|
|
if (rec.error != null) {
|
|
|
|
stdout
|
|
|
|
..writeln(cyan.wrap(rec.toString()))
|
|
|
|
..writeln(cyan.wrap(rec.error.toString()))
|
|
|
|
..writeln(cyan.wrap(rec.stackTrace.toString()));
|
|
|
|
}
|
|
|
|
});
|
2017-04-01 01:00:24 +00:00
|
|
|
});
|
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
todos.get('/action/:action', (req, res) => res.json(req.params));
|
2016-10-22 20:41:36 +00:00
|
|
|
|
|
|
|
Route ted;
|
|
|
|
|
|
|
|
ted = nested.post('/ted/:route', (RequestContext req, res) {
|
|
|
|
print('Params: ${req.params}');
|
2017-11-28 18:14:50 +00:00
|
|
|
print('Path: ${ted.path}, uri: ${req.path}');
|
|
|
|
print('matcher: ${ted.parser}');
|
2016-10-22 20:41:36 +00:00
|
|
|
return req.params;
|
|
|
|
});
|
|
|
|
|
2018-08-21 01:05:05 +00:00
|
|
|
app.mount('/nes', nested);
|
2016-11-28 00:49:27 +00:00
|
|
|
app.get('/meta', testMiddlewareMetadata);
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/intercepted', (req, res) => 'This should not be shown',
|
|
|
|
middleware: [interceptor]);
|
|
|
|
app.get('/hello', (req, res) => 'world');
|
2016-11-28 00:49:27 +00:00
|
|
|
app.get('/name/:first/last/:last', (req, res) => req.params);
|
2018-12-09 15:49:59 +00:00
|
|
|
app.post(
|
|
|
|
'/lambda',
|
|
|
|
(RequestContext req, res) =>
|
|
|
|
req.parseBody().then((_) => req.bodyAsMap));
|
2018-08-21 01:05:05 +00:00
|
|
|
app.mount('/todos/:id', todos);
|
2016-11-28 00:49:27 +00:00
|
|
|
app
|
2016-09-15 19:53:01 +00:00
|
|
|
.get('/greet/:name',
|
2016-10-22 20:41:36 +00:00
|
|
|
(RequestContext req, res) async => "Hello ${req.params['name']}")
|
2017-11-28 18:14:50 +00:00
|
|
|
.name = 'Named routes';
|
2016-11-28 00:49:27 +00:00
|
|
|
app.get('/named', (req, ResponseContext res) async {
|
2019-04-19 07:46:44 +00:00
|
|
|
await res.redirectTo('Named routes', {'name': 'tests'});
|
2016-04-18 03:27:23 +00:00
|
|
|
});
|
2016-11-28 00:49:27 +00:00
|
|
|
app.get('/log', (RequestContext req, res) async {
|
2018-12-09 15:49:59 +00:00
|
|
|
print("Query: ${req.queryParameters}");
|
2016-09-15 19:53:01 +00:00
|
|
|
return "Logged";
|
2016-04-18 03:27:23 +00:00
|
|
|
});
|
2016-10-22 20:41:36 +00:00
|
|
|
|
2018-08-20 20:43:38 +00:00
|
|
|
app.get('/method', (req, res) => 'Only GET');
|
|
|
|
app.post('/method', (req, res) => 'Only POST');
|
2017-10-04 14:09:12 +00:00
|
|
|
|
2019-05-02 22:48:31 +00:00
|
|
|
app.use('/query', QueryService());
|
2017-10-04 14:09:12 +00:00
|
|
|
|
2018-08-20 20:21:06 +00:00
|
|
|
RequestHandler write(String message) {
|
|
|
|
return (req, res) {
|
2017-10-10 16:55:42 +00:00
|
|
|
res.write(message);
|
2018-11-11 01:07:09 +00:00
|
|
|
return true;
|
2017-10-10 16:55:42 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-20 20:21:06 +00:00
|
|
|
app.chain([write('a')]).chain([write('b'), write('c')]).get(
|
2018-08-21 18:50:43 +00:00
|
|
|
'/chained', (req, res) => res.close());
|
2017-10-10 16:55:42 +00:00
|
|
|
|
2018-08-21 01:05:05 +00:00
|
|
|
app.fallback((req, res) => 'MJ');
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2018-11-11 01:07:09 +00:00
|
|
|
//app.dumpTree(header: "DUMPING ROUTES:", showMatchers: true);
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2019-05-02 22:48:31 +00:00
|
|
|
client = http.Client();
|
|
|
|
var server = await AngelHttp(app).startServer('127.0.0.1', 0);
|
2018-02-07 05:36:24 +00:00
|
|
|
url = "http://${server.address.host}:${server.port}";
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
tearDown(() async {
|
2018-02-07 04:34:08 +00:00
|
|
|
await app.close();
|
2016-11-28 00:49:27 +00:00
|
|
|
app = null;
|
2016-09-15 19:53:01 +00:00
|
|
|
nested = null;
|
|
|
|
todos = null;
|
|
|
|
client.close();
|
|
|
|
client = null;
|
|
|
|
url = null;
|
|
|
|
});
|
2016-04-21 20:37:02 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('Can match basic url', () async {
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client.get(Uri.parse("$url/hello"));
|
2016-09-15 19:53:01 +00:00
|
|
|
expect(response.body, equals('"world"'));
|
|
|
|
});
|
2016-04-21 20:37:02 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('Can match url with multiple parameters', () async {
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client.get(Uri.parse('$url/name/HELLO/last/WORLD'));
|
2016-11-28 00:49:27 +00:00
|
|
|
print('Response: ${response.body}');
|
2018-06-08 07:06:26 +00:00
|
|
|
var json_ = json.decode(response.body);
|
2018-07-09 14:28:32 +00:00
|
|
|
expect(json_, const IsInstanceOf<Map>());
|
2018-06-08 07:06:26 +00:00
|
|
|
expect(json_['first'], equals('HELLO'));
|
|
|
|
expect(json_['last'], equals('WORLD'));
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2017-10-10 16:55:42 +00:00
|
|
|
test('Chained routes', () async {
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client.get(Uri.parse("$url/chained"));
|
2017-10-10 16:55:42 +00:00
|
|
|
expect(response.body, equals('abc'));
|
|
|
|
});
|
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('Can nest another Angel instance', () async {
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client.post(Uri.parse('$url/nes/ted/foo'));
|
2018-06-08 07:06:26 +00:00
|
|
|
var json_ = json.decode(response.body);
|
|
|
|
expect(json_['route'], equals('foo'));
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('Can parse parameters from a nested Angel instance', () async {
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client.get(Uri.parse('$url/todos/1337/action/test'));
|
2018-06-08 07:06:26 +00:00
|
|
|
var json_ = json.decode(response.body);
|
|
|
|
print('JSON: $json_');
|
|
|
|
expect(json_['id'], equals('1337'));
|
|
|
|
expect(json_['action'], equals('test'));
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('Can add and use named middleware', () async {
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client.get(Uri.parse('$url/intercepted'));
|
2016-09-15 19:53:01 +00:00
|
|
|
expect(response.body, equals('Middleware'));
|
|
|
|
});
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('Middleware via metadata', () async {
|
|
|
|
// Metadata
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client.get(Uri.parse('$url/meta'));
|
2016-09-15 19:53:01 +00:00
|
|
|
expect(response.body, equals('Middleware'));
|
|
|
|
});
|
2016-06-23 19:05:55 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('Can serialize function result as JSON', () async {
|
2018-07-09 14:54:50 +00:00
|
|
|
Map headers = <String, String>{'Content-Type': 'application/json'};
|
2018-06-08 07:06:26 +00:00
|
|
|
String postData = json.encode({'it': 'works'});
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client.post(Uri.parse("$url/lambda"),
|
2018-06-27 18:40:43 +00:00
|
|
|
headers: headers as Map<String, String>, body: postData);
|
2018-06-08 07:06:26 +00:00
|
|
|
print('Response: ${response.body}');
|
|
|
|
expect(json.decode(response.body)['it'], equals('works'));
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
2016-06-23 19:05:55 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('Fallback routes', () async {
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client.get(Uri.parse('$url/my_favorite_artist'));
|
2016-09-15 19:53:01 +00:00
|
|
|
expect(response.body, equals('"MJ"'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can name routes', () {
|
2018-08-20 20:21:06 +00:00
|
|
|
Route foo = app.get('/framework/:id', null)..name = 'frm';
|
2016-10-22 20:41:36 +00:00
|
|
|
print('Foo: $foo');
|
2016-09-15 19:53:01 +00:00
|
|
|
String uri = foo.makeUri({'id': 'angel'});
|
|
|
|
print(uri);
|
2016-10-22 20:41:36 +00:00
|
|
|
expect(uri, equals('framework/angel'));
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Redirect to named routes', () async {
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client.get(Uri.parse('$url/named'));
|
2016-09-15 19:53:01 +00:00
|
|
|
print(response.body);
|
2018-06-08 07:06:26 +00:00
|
|
|
expect(json.decode(response.body), equals('Hello tests'));
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Match routes, even with query params', () async {
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client
|
|
|
|
.get(Uri.parse("$url/log?foo=bar&bar=baz&baz.foo=bar&baz.bar=foo"));
|
2016-09-15 19:53:01 +00:00
|
|
|
print(response.body);
|
2018-06-08 07:06:26 +00:00
|
|
|
expect(json.decode(response.body), equals('Logged'));
|
2016-09-15 19:53:01 +00:00
|
|
|
|
2021-03-06 14:28:33 +00:00
|
|
|
response = await client.get(Uri.parse("$url/query/foo?bar=baz"));
|
2016-09-15 19:53:01 +00:00
|
|
|
print(response.body);
|
2016-11-23 09:10:47 +00:00
|
|
|
expect(response.body, equals("Service with Middleware"));
|
2016-04-18 03:27:23 +00:00
|
|
|
});
|
2017-10-04 14:09:12 +00:00
|
|
|
|
|
|
|
test('only match route with matching method', () async {
|
2021-03-06 14:28:33 +00:00
|
|
|
var response = await client.get(Uri.parse("$url/method"));
|
2017-10-04 14:09:12 +00:00
|
|
|
print(response.body);
|
|
|
|
expect(response.body, '"Only GET"');
|
|
|
|
|
2021-03-06 14:28:33 +00:00
|
|
|
response = await client.post(Uri.parse("$url/method"));
|
2017-10-04 14:09:12 +00:00
|
|
|
print(response.body);
|
|
|
|
expect(response.body, '"Only POST"');
|
|
|
|
|
2021-03-06 14:28:33 +00:00
|
|
|
response = await client.patch(Uri.parse("$url/method"));
|
2017-10-04 14:09:12 +00:00
|
|
|
print(response.body);
|
|
|
|
expect(response.body, '"MJ"');
|
|
|
|
});
|
2016-06-23 19:05:55 +00:00
|
|
|
}
|