platform/test/controller_test.dart

128 lines
3.4 KiB
Dart
Raw Normal View History

2016-06-27 00:20:42 +00:00
import 'dart:async';
import 'dart:convert';
2016-06-27 00:20:42 +00:00
import 'dart:io';
2018-08-20 03:06:29 +00:00
2018-08-19 15:33:25 +00:00
import 'package:angel_container/mirrors.dart';
2016-06-27 00:20:42 +00:00
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';
2018-08-20 03:06:29 +00:00
2016-06-27 00:20:42 +00:00
import 'common.dart';
@Expose("/todos", middleware: [foo])
2016-06-27 00:20:42 +00:00
class TodoController extends Controller {
List<Todo> todos = [new Todo(text: "Hello", over: "world")];
@Expose("/:id", middleware: [bar])
2016-11-23 09:10:47 +00:00
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;
}
void foo(RequestContext req, ResponseContext res) {
res.write("Hello, ");
}
void bar(RequestContext req, ResponseContext res) {
res.write("world!");
}
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 {
2018-08-20 02:40:11 +00:00
app = new Angel(reflector: MirrorsReflector());
2016-11-23 09:10:47 +00:00
app.get(
"/redirect",
(req, res) async =>
2016-11-23 09:10:47 +00:00
res.redirectToAction("TodoController@foo", {"foo": "world"}));
2017-10-04 14:09:12 +00:00
await app.configure((ctrl = new TodoController()).configureServer);
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
2018-02-07 05:36:24 +00:00
server = await new AngelHttp(app).startServer();
2016-11-24 07:12:53 +00:00
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 {
2018-08-20 02:40:11 +00:00
var app = new Angel(reflector: MirrorsReflector());
2017-10-04 14:09:12 +00:00
await app.configure(new NoExposeController().configureServer);
2017-06-29 22:17:42 +00:00
throw 'Should require @Expose';
} on Exception {
// :)
}
});
test('create dynamic handler', () async {
2018-08-20 02:40:11 +00:00
var app = new Angel(reflector: MirrorsReflector());
2017-09-22 14:03:23 +00:00
app.get(
'/foo',
ioc(({String bar}) {
2017-09-22 14:03:23 +00:00
return 2;
}, optional: ['bar']));
2017-06-29 22:17:42 +00:00
var rq = new MockHttpRequest('GET', new Uri(path: 'foo'));
2018-02-07 05:36:24 +00:00
await new AngelHttp(app).handleRequest(rq);
2018-05-16 02:05:13 +00:00
var body = await rq.response.transform(utf8.decoder).join();
expect(json.decode(body), 2);
2017-06-29 22:17:42 +00:00
});
test('optional name', () async {
2018-08-20 02:40:11 +00:00
var app = new Angel(reflector: MirrorsReflector());
2017-10-04 14:09:12 +00:00
await app.configure(new NamedController().configureServer);
expect(app.controllers['foo'], const IsInstanceOf<NamedController>());
2017-06-29 22:17:42 +00:00
});
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
2017-11-18 17:42:31 +00:00
expect(rgx.firstMatch(response.body)?.start, equals(0));
2018-05-16 02:05:13 +00:00
Map todo = json.decode(response.body.replaceAll(rgx, ""));
print("Todo: $todo");
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
});
}