platform/test/controller_test.dart

137 lines
3.6 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';
2018-10-22 15:54:41 +00:00
import 'package:angel_framework/http.dart';
2016-06-27 00:20:42 +00:00
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 {
2019-05-02 22:48:31 +00:00
List<Todo> todos = [Todo(text: "Hello", over: "world")];
2016-06-27 00:20:42 +00:00
@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 {
2019-05-02 22:48:31 +00:00
@Expose('/optional/:arg?', allowNull: ['arg'])
2017-06-29 22:17:42 +00:00
optional() => 2;
}
2018-11-11 01:07:09 +00:00
bool foo(RequestContext req, ResponseContext res) {
res.write("Hello, ");
2018-11-11 01:07:09 +00:00
return true;
}
2018-11-11 01:07:09 +00:00
bool bar(RequestContext req, ResponseContext res) {
res.write("world!");
2018-11-11 01:07:09 +00:00
return true;
}
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;
2019-05-02 22:48:31 +00:00
http.Client client = http.Client();
2016-11-24 07:12:53 +00:00
String url;
2016-06-27 00:20:42 +00:00
setUp(() async {
2019-05-02 22:48:31 +00:00
app = 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"}));
2018-11-08 02:50:04 +00:00
// Register as a singleton, just for the purpose of this test
if (!app.container.has<TodoController>())
2019-05-02 22:48:31 +00:00
app.container.registerSingleton(ctrl = TodoController());
2018-11-08 02:50:04 +00:00
// Using mountController<T>();
await app.mountController<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
2019-05-02 22:48:31 +00:00
server = await 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 {
2019-05-02 22:48:31 +00:00
var app = Angel(reflector: MirrorsReflector());
await app.configure(NoExposeController().configureServer);
2017-06-29 22:17:42 +00:00
throw 'Should require @Expose';
} on Exception {
// :)
}
});
test('create dynamic handler', () async {
2019-05-02 22:48:31 +00:00
var app = 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']));
2019-05-02 22:48:31 +00:00
var rq = MockHttpRequest('GET', Uri(path: 'foo'));
await 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 {
2019-05-02 22:48:31 +00:00
var app = Angel(reflector: MirrorsReflector());
await app.configure(NamedController().configureServer);
expect(app.controllers['foo'], const IsInstanceOf<NamedController>());
2017-06-29 22:17:42 +00:00
});
test("middleware", () async {
2019-05-02 22:48:31 +00:00
var rgx = 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));
2019-04-08 19:53:07 +00:00
var todo = json.decode(response.body.replaceAll(rgx, "")) as Map;
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
});
}