2018-08-19 15:33:25 +00:00
|
|
|
import 'package:angel_container/mirrors.dart';
|
2016-04-18 03:27:23 +00:00
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2018-10-22 15:54:41 +00:00
|
|
|
import 'package:angel_framework/http.dart';
|
2018-08-20 03:06:29 +00:00
|
|
|
import 'dart:convert';
|
2016-04-18 03:27:23 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
2018-06-08 07:06:26 +00:00
|
|
|
import 'package:stack_trace/stack_trace.dart';
|
2016-04-18 03:27:23 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2017-02-13 00:38:33 +00:00
|
|
|
class Todo extends Model {
|
2016-04-18 03:27:23 +00:00
|
|
|
String text;
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
String over;
|
2016-04-18 03:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main() {
|
2018-07-09 14:49:59 +00:00
|
|
|
Map headers = <String, String>{
|
2016-09-15 19:53:01 +00:00
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
};
|
|
|
|
Angel app;
|
2018-11-08 02:34:03 +00:00
|
|
|
MapService service;
|
2016-09-15 19:53:01 +00:00
|
|
|
String url;
|
|
|
|
http.Client client;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
setUp(() async {
|
2018-08-20 02:40:11 +00:00
|
|
|
app = new Angel(reflector: MirrorsReflector())
|
2018-11-08 02:34:03 +00:00
|
|
|
..use('/todos', service = new MapService())
|
2017-09-22 14:03:23 +00:00
|
|
|
..errorHandler = (e, req, res) {
|
2019-04-20 19:04:59 +00:00
|
|
|
if (e.error != null) print('Whoops: ${e.error}');
|
2018-06-08 07:06:26 +00:00
|
|
|
if (e.stackTrace != null) print(new Chain.forTrace(e.stackTrace).terse);
|
2017-09-22 14:03:23 +00:00
|
|
|
};
|
2017-02-23 00:37:15 +00:00
|
|
|
|
2018-02-07 05:36:24 +00:00
|
|
|
var server = await new AngelHttp(app).startServer();
|
2016-09-15 19:53:01 +00:00
|
|
|
client = new http.Client();
|
2018-02-07 05:36:24 +00:00
|
|
|
url = "http://${server.address.host}:${server.port}";
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
tearDown(() async {
|
2017-05-27 12:39:45 +00:00
|
|
|
await app.close();
|
2016-09-15 19:53:01 +00:00
|
|
|
app = null;
|
|
|
|
url = null;
|
|
|
|
client.close();
|
|
|
|
client = null;
|
|
|
|
});
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
group('memory', () {
|
|
|
|
test('can index an empty service', () async {
|
|
|
|
var response = await client.get("$url/todos/");
|
|
|
|
print(response.body);
|
|
|
|
expect(response.body, equals('[]'));
|
|
|
|
print(response.body);
|
2018-06-08 07:06:26 +00:00
|
|
|
expect(json.decode(response.body).length, 0);
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('can create data', () async {
|
2018-08-19 15:49:33 +00:00
|
|
|
String postData = json.encode({'text': 'Hello, world!'});
|
2018-06-23 03:59:41 +00:00
|
|
|
var response = await client.post("$url/todos",
|
2018-06-27 18:40:43 +00:00
|
|
|
headers: headers as Map<String, String>, body: postData);
|
2017-03-28 23:29:22 +00:00
|
|
|
expect(response.statusCode, 201);
|
2018-08-20 02:31:08 +00:00
|
|
|
var jsons = json.decode(response.body);
|
|
|
|
print(jsons);
|
|
|
|
expect(jsons['text'], equals('Hello, world!'));
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('can fetch data', () async {
|
2018-08-19 15:49:33 +00:00
|
|
|
String postData = json.encode({'text': 'Hello, world!'});
|
2018-06-23 03:59:41 +00:00
|
|
|
await client.post("$url/todos",
|
2018-06-27 18:40:43 +00:00
|
|
|
headers: headers as Map<String, String>, body: postData);
|
2017-02-23 00:37:15 +00:00
|
|
|
var response = await client.get("$url/todos/0");
|
2017-03-28 23:29:22 +00:00
|
|
|
expect(response.statusCode, 200);
|
2018-08-20 02:31:08 +00:00
|
|
|
var jsons = json.decode(response.body);
|
|
|
|
print(jsons);
|
|
|
|
expect(jsons['text'], equals('Hello, world!'));
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
Angel.secure, fallback routes, 404, app.addRoute, app.all, services are a go (just missing params, i.e. $sort?), now have service.app, app.before, app.after, angel.configure now uses futures, errors are implemented
2016-04-29 00:01:58 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('can modify data', () async {
|
2018-08-19 15:49:33 +00:00
|
|
|
String postData = json.encode({'text': 'Hello, world!'});
|
2018-06-23 03:59:41 +00:00
|
|
|
await client.post("$url/todos",
|
2018-06-27 18:40:43 +00:00
|
|
|
headers: headers as Map<String, String>, body: postData);
|
2018-08-19 15:49:33 +00:00
|
|
|
postData = json.encode({'text': 'modified'});
|
2018-12-11 03:19:16 +00:00
|
|
|
|
2018-06-23 03:59:41 +00:00
|
|
|
var response = await client.patch("$url/todos/0",
|
2018-06-27 18:40:43 +00:00
|
|
|
headers: headers as Map<String, String>, body: postData);
|
2017-03-28 23:29:22 +00:00
|
|
|
expect(response.statusCode, 200);
|
2018-08-20 02:31:08 +00:00
|
|
|
var jsons = json.decode(response.body);
|
|
|
|
print(jsons);
|
|
|
|
expect(jsons['text'], equals('modified'));
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('can overwrite data', () async {
|
2018-08-19 15:49:33 +00:00
|
|
|
String postData = json.encode({'text': 'Hello, world!'});
|
2018-06-23 03:59:41 +00:00
|
|
|
await client.post("$url/todos",
|
2018-06-27 18:40:43 +00:00
|
|
|
headers: headers as Map<String, String>, body: postData);
|
2018-08-19 15:49:33 +00:00
|
|
|
postData = json.encode({'over': 'write'});
|
2018-12-09 15:49:59 +00:00
|
|
|
|
2018-06-23 03:59:41 +00:00
|
|
|
var response = await client.post("$url/todos/0",
|
2018-06-27 18:40:43 +00:00
|
|
|
headers: headers as Map<String, String>, body: postData);
|
2017-03-28 23:29:22 +00:00
|
|
|
expect(response.statusCode, 200);
|
2018-08-20 02:31:08 +00:00
|
|
|
var jsons = json.decode(response.body);
|
|
|
|
print(jsons);
|
|
|
|
expect(jsons['text'], equals(null));
|
|
|
|
expect(jsons['over'], equals('write'));
|
2016-09-15 19:53:01 +00:00
|
|
|
});
|
|
|
|
|
2018-11-08 02:34:03 +00:00
|
|
|
test('readMany', () async {
|
|
|
|
var items = <Map>[
|
|
|
|
await service.create({'foo': 'bar'}),
|
|
|
|
await service.create({'bar': 'baz'}),
|
|
|
|
await service.create({'baz': 'quux'})
|
|
|
|
];
|
|
|
|
|
|
|
|
var ids = items.map((m) => m['id'] as String).toList();
|
|
|
|
expect(await service.readMany(ids), items);
|
|
|
|
});
|
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
test('can delete data', () async {
|
2018-08-19 15:49:33 +00:00
|
|
|
String postData = json.encode({'text': 'Hello, world!'});
|
2017-09-22 14:03:23 +00:00
|
|
|
var created = await client
|
2018-06-23 03:59:41 +00:00
|
|
|
.post("$url/todos",
|
2018-06-27 18:40:43 +00:00
|
|
|
headers: headers as Map<String, String>, body: postData)
|
2018-06-08 07:06:26 +00:00
|
|
|
.then((r) => json.decode(r.body));
|
2017-08-03 16:40:21 +00:00
|
|
|
var response = await client.delete("$url/todos/${created['id']}");
|
2017-03-28 23:29:22 +00:00
|
|
|
expect(response.statusCode, 200);
|
2018-08-20 02:31:08 +00:00
|
|
|
var json_ = json.decode(response.body);
|
2018-06-08 07:06:26 +00:00
|
|
|
print(json_);
|
|
|
|
expect(json_['text'], equals('Hello, world!'));
|
2016-04-18 03:27:23 +00:00
|
|
|
});
|
2019-04-20 19:04:59 +00:00
|
|
|
|
|
|
|
test('cannot remove all unless explicitly set', () async {
|
|
|
|
var response = await client.delete('$url/todos/null');
|
|
|
|
expect(response.statusCode, 403);
|
|
|
|
});
|
2016-04-18 03:27:23 +00:00
|
|
|
});
|
2017-02-23 00:37:15 +00:00
|
|
|
}
|