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;
|
|
|
|
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")];
|
|
|
|
|
|
|
|
@Expose("/:id", middleware: const["bar"])
|
|
|
|
Future<Todo> fetchTodo(int id, RequestContext req,
|
|
|
|
ResponseContext res) async {
|
|
|
|
expect(req, isNotNull);
|
|
|
|
expect(res, isNotNull);
|
|
|
|
return todos[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Expose("/namedRoute/:foo", as: "foo")
|
2016-09-15 19:53:01 +00:00
|
|
|
Future<String> someRandomRoute(RequestContext req,
|
|
|
|
ResponseContext res) async {
|
2016-06-27 00:20:42 +00:00
|
|
|
return "${req.params['foo']}!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2016-09-15 19:53:01 +00:00
|
|
|
Angel app = new Angel();
|
|
|
|
HttpServer server;
|
|
|
|
InternetAddress host = InternetAddress.LOOPBACK_IP_V4;
|
|
|
|
int port = 3000;
|
|
|
|
http.Client client;
|
|
|
|
String url = "http://${host.address}:$port";
|
2016-06-27 00:20:42 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
setUp(() async {
|
|
|
|
app.registerMiddleware("foo", (req, res) async => res.write("Hello, "));
|
|
|
|
app.registerMiddleware("bar", (req, res) async => res.write("world!"));
|
|
|
|
app.get("/redirect", (req, ResponseContext res) async =>
|
|
|
|
res.redirectToAction("TodoController@foo", {"foo": "world"}));
|
|
|
|
await app.configure(new TodoController());
|
2016-06-27 00:20:42 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
print(app.controllers);
|
|
|
|
print("\nDUMPING ROUTES:");
|
|
|
|
app.routes.forEach((Route route) {
|
|
|
|
print("\t${route.method} ${route.path} -> ${route.handlers}");
|
2016-06-27 00:20:42 +00:00
|
|
|
});
|
2016-09-15 19:53:01 +00:00
|
|
|
print("\n");
|
2016-06-27 00:20:42 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
server = await app.startServer(host, port);
|
|
|
|
client = new http.Client();
|
|
|
|
});
|
2016-06-27 00:20:42 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
tearDown(() async {
|
2016-11-22 04:58:53 +00:00
|
|
|
await (server ?? app.httpServer).close(force: true);
|
2016-09-15 19:53:01 +00:00
|
|
|
client.close();
|
|
|
|
client = null;
|
|
|
|
});
|
2016-06-27 00:20:42 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test("middleware", () async {
|
|
|
|
var rgx = new RegExp("^Hello, world!");
|
|
|
|
var response = await client.get("$url/todos/0");
|
|
|
|
print(response.body);
|
2016-06-27 00:20:42 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
expect(response.body.indexOf("Hello, "), equals(0));
|
|
|
|
|
|
|
|
Map todo = JSON.decode(response.body.replaceAll(rgx, ""));
|
|
|
|
print("Todo: $todo");
|
|
|
|
expect(todo.keys.length, equals(3));
|
|
|
|
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");
|
|
|
|
print(response.body);
|
2016-06-27 00:20:42 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
expect(response.body, equals("Hello, \"world!\""));
|
2016-06-27 00:20:42 +00:00
|
|
|
});
|
|
|
|
}
|