2016-11-25 23:22:33 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_route/angel_route.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2019-11-28 17:40:32 +00:00
|
|
|
const List<Map<String, String>> people = [
|
|
|
|
{'name': 'John Smith'}
|
2017-01-28 04:17:42 +00:00
|
|
|
];
|
|
|
|
|
2021-02-21 02:47:23 +00:00
|
|
|
void main() {
|
2016-11-25 23:22:33 +00:00
|
|
|
http.Client client;
|
2017-01-28 04:17:42 +00:00
|
|
|
|
2021-03-06 12:15:53 +00:00
|
|
|
final router = Router();
|
2016-11-25 23:22:33 +00:00
|
|
|
HttpServer server;
|
|
|
|
String url;
|
|
|
|
|
|
|
|
router.get('/', (req, res) {
|
|
|
|
res.write('Root');
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/hello', (req, res) {
|
|
|
|
res.write('World');
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
router.group('/people', (router) {
|
|
|
|
router.get('/', (req, res) {
|
2018-08-20 14:24:11 +00:00
|
|
|
res.write(json.encode(people));
|
2016-11-25 23:22:33 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
router.group('/:id', (router) {
|
|
|
|
router.get('/', (req, res) {
|
|
|
|
// In a real application, we would take the param,
|
|
|
|
// but not here...
|
2018-08-20 14:24:11 +00:00
|
|
|
res.write(json.encode(people.first));
|
2016-11-25 23:22:33 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/name', (req, res) {
|
|
|
|
// In a real application, we would take the param,
|
|
|
|
// but not here...
|
2018-08-20 14:24:11 +00:00
|
|
|
res.write(json.encode(people.first['name']));
|
2016-11-25 23:22:33 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-11-28 17:40:32 +00:00
|
|
|
final beatles = Router();
|
2016-11-27 04:04:47 +00:00
|
|
|
|
|
|
|
beatles.post('/spinal_clacker', (req, res) {
|
|
|
|
res.write('come ');
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2019-11-28 17:40:32 +00:00
|
|
|
final yellow = Router()
|
2016-11-27 04:04:47 +00:00
|
|
|
..get('/submarine', (req, res) {
|
|
|
|
res.write('we all live in a');
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
beatles.group('/big', (router) {
|
|
|
|
router.mount('/yellow', yellow);
|
|
|
|
});
|
|
|
|
|
|
|
|
beatles.all('*', (req, res) {
|
|
|
|
res.write('together');
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
router.mount('/beatles', beatles);
|
|
|
|
|
2016-11-25 23:22:33 +00:00
|
|
|
setUp(() async {
|
2019-11-28 17:40:32 +00:00
|
|
|
client = http.Client();
|
2016-11-25 23:22:33 +00:00
|
|
|
|
|
|
|
router.dumpTree();
|
2018-08-20 18:59:51 +00:00
|
|
|
server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
2016-11-25 23:22:33 +00:00
|
|
|
url = 'http://${server.address.address}:${server.port}';
|
|
|
|
|
|
|
|
server.listen((req) async {
|
|
|
|
final res = req.response;
|
|
|
|
|
|
|
|
// Easy middleware pipeline
|
2017-11-25 00:26:06 +00:00
|
|
|
final results =
|
|
|
|
router.resolveAbsolute(req.uri.toString(), method: req.method);
|
2019-11-28 17:40:32 +00:00
|
|
|
final pipeline = MiddlewarePipeline(results);
|
2016-11-25 23:22:33 +00:00
|
|
|
|
|
|
|
if (pipeline.handlers.isEmpty) {
|
|
|
|
res
|
2018-08-20 18:59:51 +00:00
|
|
|
..statusCode = 404
|
2016-11-25 23:22:33 +00:00
|
|
|
..writeln('404 Not Found');
|
|
|
|
} else {
|
|
|
|
for (final handler in pipeline.handlers) {
|
2018-08-20 18:59:51 +00:00
|
|
|
if (!((await handler(req, res)) as bool)) break;
|
2016-11-25 23:22:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await res.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
|
|
|
await server.close(force: true);
|
|
|
|
client.close();
|
|
|
|
client = null;
|
|
|
|
url = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
group('top-level', () {
|
|
|
|
group('get', () {
|
|
|
|
test('root', () async {
|
2021-03-06 12:15:53 +00:00
|
|
|
final res = await client.get(Uri.parse(url));
|
2016-11-25 23:22:33 +00:00
|
|
|
print('Response: ${res.body}');
|
|
|
|
expect(res.body, equals('Root'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('path', () async {
|
2021-03-06 12:15:53 +00:00
|
|
|
final res = await client.get(Uri.parse('$url/hello'));
|
2016-11-25 23:22:33 +00:00
|
|
|
print('Response: ${res.body}');
|
|
|
|
expect(res.body, equals('World'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
group('group', () {
|
|
|
|
group('top-level', () {
|
|
|
|
test('root', () async {
|
2021-03-06 12:15:53 +00:00
|
|
|
final res = await client.get(Uri.parse('$url/people'));
|
2016-11-25 23:22:33 +00:00
|
|
|
print('Response: ${res.body}');
|
2018-08-20 14:24:11 +00:00
|
|
|
expect(json.decode(res.body), equals(people));
|
2016-11-25 23:22:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
group('param', () {
|
|
|
|
test('root', () async {
|
2021-03-06 12:15:53 +00:00
|
|
|
final res = await client.get(Uri.parse('$url/people/0'));
|
2016-11-25 23:22:33 +00:00
|
|
|
print('Response: ${res.body}');
|
2018-08-20 14:24:11 +00:00
|
|
|
expect(json.decode(res.body), equals(people.first));
|
2016-11-25 23:22:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('path', () async {
|
2021-03-06 12:15:53 +00:00
|
|
|
final res = await client.get(Uri.parse('$url/people/0/name'));
|
2016-11-25 23:22:33 +00:00
|
|
|
print('Response: ${res.body}');
|
2018-08-20 14:24:11 +00:00
|
|
|
expect(json.decode(res.body), equals(people.first['name']));
|
2016-11-25 23:22:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-27 04:04:47 +00:00
|
|
|
group('mount', () {
|
|
|
|
group('path', () {
|
|
|
|
test('top-level', () async {
|
2021-03-06 12:15:53 +00:00
|
|
|
final res = await client.post(Uri.parse('$url/beatles/spinal_clacker'));
|
2016-11-27 04:04:47 +00:00
|
|
|
print('Response: ${res.body}');
|
|
|
|
expect(res.body, equals('come together'));
|
|
|
|
});
|
2016-11-27 14:46:13 +00:00
|
|
|
|
|
|
|
test('fallback', () async {
|
2021-03-06 12:15:53 +00:00
|
|
|
final res = await client.patch(Uri.parse('$url/beatles/muddy_water'));
|
2016-11-27 14:46:13 +00:00
|
|
|
print('Response: ${res.body}');
|
|
|
|
expect(res.body, equals('together'));
|
|
|
|
});
|
2016-11-27 22:24:30 +00:00
|
|
|
|
|
|
|
test('fallback', () async {
|
2021-03-06 12:15:53 +00:00
|
|
|
final res =
|
|
|
|
await client.patch(Uri.parse('$url/beatles/spanil_clakcer'));
|
2016-11-27 22:24:30 +00:00
|
|
|
print('Response: ${res.body}');
|
|
|
|
expect(res.body, equals('together'));
|
|
|
|
});
|
2016-11-27 14:46:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('deep nested', () async {
|
2021-03-06 12:15:53 +00:00
|
|
|
final res =
|
|
|
|
await client.get(Uri.parse('$url/beatles/big/yellow/submarine'));
|
2016-11-27 14:46:13 +00:00
|
|
|
print('Response: ${res.body}');
|
|
|
|
expect(res.body, equals('we all live in a'));
|
|
|
|
});
|
|
|
|
|
|
|
|
group('fallback', () {});
|
|
|
|
});
|
|
|
|
|
|
|
|
group('404', () {
|
|
|
|
expect404(r) => r.then((res) {
|
|
|
|
print('Response (${res.statusCode}): ${res.body}');
|
|
|
|
expect(res.statusCode, equals(404));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('path', () async {
|
2021-03-06 12:15:53 +00:00
|
|
|
await expect404(client.get(Uri.parse('$url/foo')));
|
|
|
|
await expect404(client.get(Uri.parse('$url/bye')));
|
|
|
|
await expect404(client.get(Uri.parse('$url/people/0/age')));
|
|
|
|
await expect404(client.get(Uri.parse('$url/beatles2')));
|
2016-11-27 04:04:47 +00:00
|
|
|
});
|
2016-11-27 14:46:13 +00:00
|
|
|
|
2016-11-27 22:24:30 +00:00
|
|
|
test('method', () async {
|
2021-03-06 12:15:53 +00:00
|
|
|
await expect404(client.head(Uri.parse(url)));
|
|
|
|
await expect404(client.patch(Uri.parse('$url/people')));
|
|
|
|
await expect404(client.post(Uri.parse('$url/people/0')));
|
|
|
|
await expect404(client.delete(Uri.parse('$url/beatles2/spinal_clacker')));
|
2016-11-27 22:24:30 +00:00
|
|
|
});
|
2016-11-27 04:04:47 +00:00
|
|
|
});
|
2016-11-25 23:22:33 +00:00
|
|
|
}
|