platform/test/controller_test.dart

120 lines
3.2 KiB
Dart
Raw Normal View History

2016-06-27 00:20:42 +00:00
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:http/http.dart' as http;
2017-06-29 22:17:42 +00:00
import 'package:mock_request/mock_request.dart';
2016-06-27 00:20:42 +00:00
import 'package:test/test.dart';
import 'common.dart';
@Expose("/todos", middleware: const ["foo"])
class TodoController extends Controller {
List<Todo> todos = [new Todo(text: "Hello", over: "world")];
2016-11-23 09:10:47 +00:00
@Expose("/:id", middleware: const ["bar"])
Future<Todo> fetchTodo(
2016-11-28 00:49:27 +00:00
String id, RequestContext req, ResponseContext res) async {
2016-06-27 00:20:42 +00:00
expect(req, isNotNull);
expect(res, isNotNull);
2016-11-28 00:49:27 +00:00
return todos[int.parse(id)];
2016-06-27 00:20:42 +00:00
}
@Expose("/namedRoute/:foo", as: "foo")
2016-11-23 09:10:47 +00:00
Future<String> someRandomRoute(
RequestContext req, ResponseContext res) async {
2016-06-27 00:20:42 +00:00
return "${req.params['foo']}!";
}
}
2017-09-22 14:03:23 +00:00
class NoExposeController extends Controller {}
2017-06-29 22:17:42 +00:00
@Expose('/named', as: 'foo')
class NamedController extends Controller {
@Expose('/optional/:arg?', allowNull: const ['arg'])
optional() => 2;
}
2016-06-27 00:20:42 +00:00
main() {
2016-11-23 09:10:47 +00:00
Angel app;
2017-06-29 22:17:42 +00:00
TodoController ctrl;
HttpServer server;
2016-11-24 07:12:53 +00:00
http.Client client = new http.Client();
String url;
2016-06-27 00:20:42 +00:00
setUp(() async {
2017-08-15 23:01:16 +00:00
app = new Angel();
app.registerMiddleware("foo", (req, res) async => res.write("Hello, "));
app.registerMiddleware("bar", (req, res) async => res.write("world!"));
2016-11-23 09:10:47 +00:00
app.get(
"/redirect",
(req, ResponseContext res) async =>
res.redirectToAction("TodoController@foo", {"foo": "world"}));
2017-06-29 22:17:42 +00:00
await app.configure(ctrl = new TodoController());
2016-06-27 00:20:42 +00:00
print(app.controllers);
2016-11-23 09:10:47 +00:00
app.dumpTree();
2016-06-27 00:20:42 +00:00
2016-11-24 07:12:53 +00:00
server = await app.startServer();
url = 'http://${server.address.address}:${server.port}';
});
2016-06-27 00:20:42 +00:00
tearDown(() async {
2016-11-23 09:10:47 +00:00
await server.close(force: true);
app = null;
2016-11-24 07:12:53 +00:00
url = null;
});
2016-06-27 00:20:42 +00:00
2017-06-29 22:17:42 +00:00
test('basic', () {
expect(ctrl.app, app);
});
test('require expose', () async {
try {
var app = new Angel();
await app.configure(new NoExposeController());
throw 'Should require @Expose';
} on Exception {
// :)
}
});
test('create dynamic handler', () async {
var app = new Angel();
2017-09-22 14:03:23 +00:00
app.get(
'/foo',
createDynamicHandler(({String bar}) {
return 2;
}, optional: ['bar']));
2017-06-29 22:17:42 +00:00
var rq = new MockHttpRequest('GET', new Uri(path: 'foo'));
await app.handleRequest(rq);
var body = await rq.response.transform(UTF8.decoder).join();
expect(JSON.decode(body), 2);
});
test('optional name', () async {
var app = new Angel();
await app.configure(new NamedController());
expect(app.controllers['foo'], new isInstanceOf<NamedController>());
});
test("middleware", () async {
var rgx = new RegExp("^Hello, world!");
var response = await client.get("$url/todos/0");
2016-11-28 00:49:27 +00:00
print('Response: ${response.body}');
2016-06-27 00:20:42 +00:00
2016-11-23 09:10:47 +00:00
expect(rgx.firstMatch(response.body).start, equals(0));
Map todo = JSON.decode(response.body.replaceAll(rgx, ""));
print("Todo: $todo");
2017-02-23 00:37:15 +00:00
// expect(todo.keys.length, equals(3));
expect(todo['text'], equals("Hello"));
expect(todo['over'], equals("world"));
});
2016-06-27 00:20:42 +00:00
test("named actions", () async {
var response = await client.get("$url/redirect");
2016-11-28 00:49:27 +00:00
print('Response: ${response.body}');
expect(response.body, equals("Hello, \"world!\""));
2016-06-27 00:20:42 +00:00
});
}