2021-03-20 08:11:18 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
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 {
|
2021-03-20 08:11:18 +00:00
|
|
|
String? text;
|
|
|
|
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'
|
|
|
|
};
|
2021-04-03 05:50:52 +00:00
|
|
|
late Angel app;
|
2021-03-20 08:11:18 +00:00
|
|
|
late MapService service;
|
2021-04-03 05:50:52 +00:00
|
|
|
late String url;
|
|
|
|
late http.Client client;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
setUp(() async {
|
2019-05-02 22:48:31 +00:00
|
|
|
app = Angel(reflector: MirrorsReflector())
|
|
|
|
..use('/todos', service = 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}');
|
2021-03-20 08:11:18 +00:00
|
|
|
if (e.stackTrace != null) print(Chain.forTrace(e.stackTrace!).terse);
|
2017-09-22 14:03:23 +00:00
|
|
|
};
|
2017-02-23 00:37:15 +00:00
|
|
|
|
2021-03-20 08:11:18 +00:00
|
|
|
HttpServer server = await AngelHttp(app).startServer();
|
2019-05-02 22:48:31 +00:00
|
|
|
client = 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 {
|
2021-04-03 05:50:52 +00:00
|
|
|
await app.close();
|
|
|
|
client.close();
|
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
|
|
|
group('memory', () {
|
|
|
|
test('can index an empty service', () async {
|
2021-04-03 05:50:52 +00:00
|
|
|
var response = await client.get(Uri.parse("$url/todos/"));
|
2016-09-15 19:53:01 +00:00
|
|
|
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!'});
|
2021-04-03 05:50:52 +00:00
|
|
|
var response = await client.post(Uri.parse("$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!'});
|
2021-04-03 05:50:52 +00:00
|
|
|
await client.post(Uri.parse("$url/todos"),
|
2018-06-27 18:40:43 +00:00
|
|
|
headers: headers as Map<String, String>, body: postData);
|
2021-04-03 05:50:52 +00:00
|
|
|
var response = await client.get(Uri.parse("$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!'});
|
2021-04-03 05:50:52 +00:00
|
|
|
await client.post(Uri.parse("$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
|
|
|
|
2021-04-03 05:50:52 +00:00
|
|
|
var response = await client.patch(Uri.parse("$url/todos/0"),
|
|
|
|
headers: headers, 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!'});
|
2021-04-03 05:50:52 +00:00
|
|
|
await client.post(Uri.parse("$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
|
|
|
|
2021-04-03 05:50:52 +00:00
|
|
|
var response = await client.post(Uri.parse("$url/todos/0"),
|
|
|
|
headers: headers, 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'})
|
|
|
|
];
|
|
|
|
|
2021-03-20 08:11:18 +00:00
|
|
|
var ids = items.map((m) => m['id'] as String?).toList();
|
2018-11-08 02:34:03 +00:00
|
|
|
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!'});
|
2021-04-03 05:50:52 +00:00
|
|
|
var created = await client
|
2021-03-06 14:28:33 +00:00
|
|
|
.post(Uri.parse("$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));
|
2021-03-06 14:28:33 +00:00
|
|
|
var response =
|
2021-04-03 05:50:52 +00:00
|
|
|
await client.delete(Uri.parse("$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 {
|
2021-04-10 11:23:57 +00:00
|
|
|
var a = "1234";
|
2021-04-03 05:50:52 +00:00
|
|
|
var response = await client.delete(Uri.parse('$url/todos/null'));
|
2019-04-20 19:04:59 +00:00
|
|
|
expect(response.statusCode, 403);
|
|
|
|
});
|
2016-04-18 03:27:23 +00:00
|
|
|
});
|
2017-02-23 00:37:15 +00:00
|
|
|
}
|