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")];
|
|
|
|
|
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']}!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2016-11-23 09:10:47 +00:00
|
|
|
Angel app;
|
2016-09-15 19:53:01 +00:00
|
|
|
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
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
setUp(() async {
|
2016-11-28 00:49:27 +00:00
|
|
|
app = new Angel(debug: true);
|
2016-09-15 19:53:01 +00:00
|
|
|
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"}));
|
2016-09-15 19:53:01 +00:00
|
|
|
await app.configure(new 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
|
|
|
|
2016-11-24 07:12:53 +00:00
|
|
|
server = await app.startServer();
|
|
|
|
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
|
|
|
|
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");
|
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));
|
2016-09-15 19:53:01 +00:00
|
|
|
|
|
|
|
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));
|
2016-09-15 19:53:01 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|