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-06-23 19:09:49 +00:00
|
|
|
@Middleware(const ['intercept_service'])
|
2016-06-23 19:05:55 +00:00
|
|
|
class QueryService extends Service {
|
|
|
|
@override
|
2016-06-23 19:09:49 +00:00
|
|
|
@Middleware(const ['interceptor'])
|
2017-08-03 16:40:21 +00:00
|
|
|
read(id, [Map params]) async => params;
|
2016-06-23 19:05:55 +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 {
|
2016-11-23 09:10:47 +00:00
|
|
|
final debug = true;
|
2016-11-28 00:49:27 +00:00
|
|
|
app = new Angel(debug: debug);
|
2016-10-22 20:41:36 +00:00
|
|
|
nested = new Angel(debug: debug);
|
|
|
|
todos = new Angel(debug: debug);
|
|
|
|
|
2017-04-01 01:00:24 +00:00
|
|
|
// Lazy-parse in production
|
|
|
|
[app, nested, todos].forEach((Angel app) {
|
|
|
|
app.lazyParseBodies = app.isProduction;
|
|
|
|
});
|
|
|
|
|
2016-11-28 00:49:27 +00:00
|
|
|
app
|
2016-10-22 20:41:36 +00:00
|
|
|
..registerMiddleware('interceptor', (req, res) async {
|
|
|
|
res.write('Middleware');
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
..registerMiddleware('intercept_service',
|
|
|
|
(RequestContext req, res) async {
|
2016-11-23 09:10:47 +00:00
|
|
|
res.write("Service with ");
|
2016-10-22 20:41:36 +00:00
|
|
|
return true;
|
|
|
|
});
|
2016-04-18 03:27:23 +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}');
|
2016-11-23 09:10:47 +00:00
|
|
|
print(
|
|
|
|
'Path: ${ted.path}, matcher: ${ted.matcher.pattern}, uri: ${req.path}');
|
2016-10-22 20:41:36 +00:00
|
|
|
return req.params;
|
|
|
|
});
|
|
|
|
|
2016-11-28 00:49:27 +00:00
|
|
|
app.use('/nes', nested);
|
|
|
|
app.get('/meta', testMiddlewareMetadata);
|
|
|
|
app.get('/intercepted', 'This should not be shown',
|
2016-09-15 19:53:01 +00:00
|
|
|
middleware: ['interceptor']);
|
2016-11-28 00:49:27 +00:00
|
|
|
app.get('/hello', 'world');
|
|
|
|
app.get('/name/:first/last/:last', (req, res) => req.params);
|
2017-03-28 23:29:22 +00:00
|
|
|
app.post('/lambda', (RequestContext req, res) => req.lazyBody());
|
2016-11-28 00:49:27 +00:00
|
|
|
app.use('/todos/:id', todos);
|
|
|
|
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']}")
|
2016-09-15 19:53:01 +00:00
|
|
|
.as('Named routes');
|
2016-11-28 00:49:27 +00:00
|
|
|
app.get('/named', (req, ResponseContext res) async {
|
2016-09-15 19:53:01 +00:00
|
|
|
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 {
|
2016-09-15 19:53:01 +00:00
|
|
|
print("Query: ${req.query}");
|
|
|
|
return "Logged";
|
2016-04-18 03:27:23 +00:00
|
|
|
});
|
2016-10-22 20:41:36 +00:00
|
|
|
|
2016-11-28 00:49:27 +00:00
|
|
|
app.use('/query', new QueryService());
|
|
|
|
app.get('*', 'MJ');
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-11-28 00:49:27 +00:00
|
|
|
app.dumpTree(header: "DUMPING ROUTES:", showMatchers: true);
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
client = new http.Client();
|
2016-11-28 00:49:27 +00:00
|
|
|
await app.startServer(InternetAddress.LOOPBACK_IP_V4, 0);
|
|
|
|
url = "http://${app.httpServer.address.host}:${app.httpServer.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 {
|
2016-11-28 00:49:27 +00:00
|
|
|
await app.httpServer.close(force: true);
|
|
|
|
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 {
|
|
|
|
var response = await client.get("$url/hello");
|
|
|
|
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 {
|
|
|
|
var response = await client.get('$url/name/HELLO/last/WORLD');
|
2016-11-28 00:49:27 +00:00
|
|
|
print('Response: ${response.body}');
|
2016-09-15 19:53:01 +00:00
|
|
|
var json = god.deserialize(response.body);
|
2016-11-23 09:10:47 +00:00
|
|
|
expect(json, new isInstanceOf<Map<String, String>>());
|
2016-09-15 19:53:01 +00:00
|
|
|
expect(json['first'], equals('HELLO'));
|
|
|
|
expect(json['last'], equals('WORLD'));
|
|
|
|
});
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
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'));
|
|
|
|
});
|
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 {
|
|
|
|
var response = await client.get('$url/todos/1337/action/test');
|
|
|
|
var json = god.deserialize(response.body);
|
2016-10-22 20:41:36 +00:00
|
|
|
print('JSON: $json');
|
2016-11-28 00:49:27 +00:00
|
|
|
expect(json['id'], equals('1337'));
|
2016-09-15 19:53:01 +00:00
|
|
|
expect(json['action'], equals('test'));
|
|
|
|
});
|
2016-05-02 22:28:14 +00:00
|
|
|
|
2016-09-15 19:53:01 +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
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('Middleware via metadata', () async {
|
|
|
|
// Metadata
|
|
|
|
var response = await client.get('$url/meta');
|
|
|
|
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 {
|
|
|
|
Map headers = {'Content-Type': 'application/json'};
|
|
|
|
String postData = god.serialize({'it': 'works'});
|
|
|
|
var response =
|
2016-10-22 20:41:36 +00:00
|
|
|
await client.post("$url/lambda", headers: headers, body: postData);
|
2016-09-15 19:53:01 +00:00
|
|
|
expect(god.deserialize(response.body)['it'], equals('works'));
|
|
|
|
});
|
2016-06-23 19:05:55 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('Fallback routes', () async {
|
|
|
|
var response = await client.get('$url/my_favorite_artist');
|
|
|
|
expect(response.body, equals('"MJ"'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can name routes', () {
|
2016-10-22 20:41:36 +00:00
|
|
|
Route foo = new Route('/framework/:id', name: 'frm');
|
|
|
|
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 {
|
|
|
|
var response = await client.get('$url/named');
|
|
|
|
print(response.body);
|
|
|
|
expect(god.deserialize(response.body), equals('Hello tests'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Match routes, even with query params', () async {
|
|
|
|
var response =
|
2016-10-22 20:41:36 +00:00
|
|
|
await client.get("$url/log?foo=bar&bar=baz&baz.foo=bar&baz.bar=foo");
|
2016-09-15 19:53:01 +00:00
|
|
|
print(response.body);
|
|
|
|
expect(god.deserialize(response.body), equals('Logged'));
|
|
|
|
|
|
|
|
response = await client.get("$url/query/foo?bar=baz");
|
|
|
|
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
|
|
|
});
|
2016-06-23 19:05:55 +00:00
|
|
|
}
|