2016-06-27 00:20:42 +00:00
|
|
|
import 'dart:async';
|
2018-08-20 20:43:38 +00:00
|
|
|
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';
|
|
|
|
|
2018-08-20 20:43:38 +00:00
|
|
|
@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
|
|
|
|
2018-08-20 20:43:38 +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) {
|
2018-08-20 20:43:38 +00:00
|
|
|
res.write("Hello, ");
|
2018-11-11 01:07:09 +00:00
|
|
|
return true;
|
2018-08-20 20:43:38 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 01:07:09 +00:00
|
|
|
bool bar(RequestContext req, ResponseContext res) {
|
2018-08-20 20:43:38 +00:00
|
|
|
res.write("world!");
|
2018-11-11 01:07:09 +00:00
|
|
|
return true;
|
2018-08-20 20:43:38 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2016-09-15 19:53:01 +00:00
|
|
|
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
|
|
|
|
2016-09-15 19:53:01 +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",
|
2018-08-20 20:43:38 +00:00
|
|
|
(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
|
2019-05-31 03:49:00 +00:00
|
|
|
if (!app.container.has<TodoController>()) {
|
2019-05-02 22:48:31 +00:00
|
|
|
app.container.registerSingleton(ctrl = TodoController());
|
2019-05-31 03:49:00 +00:00
|
|
|
}
|
2018-11-08 02:50:04 +00:00
|
|
|
|
|
|
|
// Using mountController<T>();
|
|
|
|
await app.mountController<TodoController>();
|
2016-06-27 00:20:42 +00:00
|
|
|
|
2016-09-15 19:53:01 +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-09-15 19:53:01 +00:00
|
|
|
});
|
2016-06-27 00:20:42 +00:00
|
|
|
|
2016-09-15 19:53:01 +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-09-15 19:53:01 +00:00
|
|
|
});
|
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',
|
2018-08-20 20:43:38 +00:00
|
|
|
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);
|
2018-07-09 14:28:32 +00:00
|
|
|
expect(app.controllers['foo'], const IsInstanceOf<NamedController>());
|
2017-06-29 22:17:42 +00:00
|
|
|
});
|
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test("middleware", () async {
|
2019-05-02 22:48:31 +00:00
|
|
|
var rgx = RegExp("^Hello, world!");
|
2016-09-15 19:53:01 +00:00
|
|
|
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));
|
2016-09-15 19:53:01 +00:00
|
|
|
|
2019-04-08 19:53:07 +00:00
|
|
|
var todo = json.decode(response.body.replaceAll(rgx, "")) as Map;
|
2016-09-15 19:53:01 +00:00
|
|
|
print("Todo: $todo");
|
|
|
|
expect(todo['text'], equals("Hello"));
|
|
|
|
expect(todo['over'], equals("world"));
|
|
|
|
});
|
2016-06-27 00:20:42 +00:00
|
|
|
|
2016-09-15 19:53:01 +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}');
|
2016-09-15 19:53:01 +00:00
|
|
|
expect(response.body, equals("Hello, \"world!\""));
|
2016-06-27 00:20:42 +00:00
|
|
|
});
|
|
|
|
}
|