2016-04-18 03:27:23 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
2016-06-21 22:56:04 +00:00
|
|
|
import 'package:json_god/json_god.dart' as god;
|
2016-04-18 03:27:23 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2016-05-02 22:28:14 +00:00
|
|
|
@Middleware(const ['interceptor'])
|
|
|
|
testMiddlewareMetadata(RequestContext req, ResponseContext res) async {
|
|
|
|
return "This should not be shown.";
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:27:23 +00:00
|
|
|
main() {
|
|
|
|
group('routing', () {
|
|
|
|
Angel angel;
|
|
|
|
Angel nested;
|
|
|
|
Angel todos;
|
|
|
|
String url;
|
|
|
|
http.Client client;
|
|
|
|
|
|
|
|
setUp(() async {
|
|
|
|
angel = new Angel();
|
|
|
|
nested = new Angel();
|
|
|
|
todos = new Angel();
|
|
|
|
|
2016-04-21 20:37:02 +00:00
|
|
|
angel.registerMiddleware('interceptor', (req, res) async {
|
|
|
|
res.write('Middleware');
|
|
|
|
return false;
|
|
|
|
});
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-04-21 20:37:02 +00:00
|
|
|
todos.get('/action/:action', (req, res) => res.json(req.params));
|
2016-04-18 03:27:23 +00:00
|
|
|
nested.post('/ted/:route', (req, res) => res.json(req.params));
|
2016-05-02 22:28:14 +00:00
|
|
|
angel.get('/meta', testMiddlewareMetadata);
|
2016-04-21 20:37:02 +00:00
|
|
|
angel.get('/intercepted', 'This should not be shown',
|
|
|
|
middleware: ['interceptor']);
|
2016-04-18 03:27:23 +00:00
|
|
|
angel.get('/hello', 'world');
|
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
|
|
|
angel.get('/name/:first/last/:last', (req, res) => req.params);
|
2016-04-21 20:37:02 +00:00
|
|
|
angel.post('/lambda', (req, res) => req.body);
|
2016-04-18 03:27:23 +00:00
|
|
|
angel.use('/nes', nested);
|
|
|
|
angel.use('/todos/:id', todos);
|
2016-05-02 22:28:14 +00:00
|
|
|
angel.get('/greet/:name', (RequestContext req, res) async => "Hello ${req.params['name']}").as('Named routes');
|
|
|
|
angel.get('/named', (req, ResponseContext res) async {
|
|
|
|
res.redirectTo('Named routes', {'name': 'tests'});
|
|
|
|
});
|
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
|
|
|
angel.get('*', 'MJ');
|
2016-04-18 03:27:23 +00:00
|
|
|
|
|
|
|
client = new http.Client();
|
|
|
|
await angel.startServer(InternetAddress.LOOPBACK_IP_V4, 0);
|
|
|
|
url = "http://${angel.httpServer.address.host}:${angel.httpServer.port}";
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
|
|
|
await angel.httpServer.close(force: true);
|
|
|
|
angel = null;
|
|
|
|
nested = null;
|
|
|
|
todos = null;
|
|
|
|
client.close();
|
|
|
|
client = null;
|
|
|
|
url = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can match basic url', () async {
|
|
|
|
var response = await client.get("$url/hello");
|
|
|
|
expect(response.body, equals('"world"'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can match url with multiple parameters', () async {
|
|
|
|
var response = await client.get('$url/name/HELLO/last/WORLD');
|
|
|
|
var json = god.deserialize(response.body);
|
|
|
|
expect(json['first'], equals('HELLO'));
|
|
|
|
expect(json['last'], equals('WORLD'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can nest another Angel instance', () async {
|
|
|
|
var response = await client.post('$url/nes/ted/foo');
|
|
|
|
var json = god.deserialize(response.body);
|
|
|
|
expect(json['route'], equals('foo'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can parse parameters from a nested Angel instance', () async {
|
|
|
|
var response = await client.get('$url/todos/1337/action/test');
|
|
|
|
var json = god.deserialize(response.body);
|
|
|
|
expect(json['id'], equals(1337));
|
|
|
|
expect(json['action'], equals('test'));
|
|
|
|
});
|
2016-04-21 20:37:02 +00:00
|
|
|
|
|
|
|
test('Can add and use named middleware', () async {
|
|
|
|
var response = await client.get('$url/intercepted');
|
|
|
|
expect(response.body, equals('Middleware'));
|
|
|
|
});
|
|
|
|
|
2016-05-02 22:28:14 +00:00
|
|
|
test('Middleware via metadata', () async {
|
|
|
|
// Metadata
|
|
|
|
var response = await client.get('$url/meta');
|
|
|
|
expect(response.body, equals('Middleware'));
|
|
|
|
});
|
|
|
|
|
2016-04-21 20:37:02 +00:00
|
|
|
test('Can serialize function result as JSON', () async {
|
|
|
|
Map headers = {'Content-Type': 'application/json'};
|
|
|
|
String postData = god.serialize({'it': 'works'});
|
|
|
|
var response = await client.post(
|
|
|
|
"$url/lambda", headers: headers, body: postData);
|
|
|
|
expect(god.deserialize(response.body)['it'], equals('works'));
|
|
|
|
});
|
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
|
|
|
|
|
|
|
test('Fallback routes', () async {
|
|
|
|
var response = await client.get('$url/my_favorite_artist');
|
|
|
|
expect(response.body, equals('"MJ"'));
|
|
|
|
});
|
2016-05-02 22:28:14 +00:00
|
|
|
|
|
|
|
test('Can name routes', () {
|
|
|
|
Route foo = angel.get('/framework/:id', 'Angel').as('frm');
|
|
|
|
String uri = foo.makeUri({'id': 'angel'});
|
|
|
|
print(uri);
|
|
|
|
expect(uri, equals('/framework/angel'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Redirect to named routes', () async {
|
|
|
|
var response = await client.get('$url/named');
|
|
|
|
print(response.body);
|
|
|
|
expect(god.deserialize(response.body), equals('Hello tests'));
|
|
|
|
});
|
2016-04-18 03:27:23 +00:00
|
|
|
});
|
|
|
|
}
|