platform/packages/framework/test/services_test.dart

134 lines
4.4 KiB
Dart
Raw Normal View History

2021-03-20 08:11:18 +00:00
import 'dart:io';
2021-05-14 10:34:09 +00:00
import 'package:angel3_container/mirrors.dart';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_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
}
2021-05-14 10:34:09 +00:00
void main() {
2018-07-09 14:49:59 +00:00
Map headers = <String, String>{
'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
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-07-08 02:42:40 +00:00
var server = await AngelHttp(app).startServer();
2019-05-02 22:48:31 +00:00
client = http.Client();
2021-07-08 02:42:40 +00:00
url = 'http://${server.address.host}:${server.port}';
});
2016-04-18 03:27:23 +00:00
tearDown(() async {
2021-04-03 05:50:52 +00:00
await app.close();
client.close();
});
group('memory', () {
test('can index an empty service', () async {
2021-07-08 02:42:40 +00:00
var response = await client.get(Uri.parse('$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);
});
test('can create data', () async {
2021-07-08 02:42:40 +00:00
var postData = json.encode({'text': 'Hello, world!'});
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!'));
});
test('can fetch data', () async {
2021-07-08 02:42:40 +00:00
var postData = json.encode({'text': 'Hello, world!'});
await client.post(Uri.parse('$url/todos'),
2018-06-27 18:40:43 +00:00
headers: headers as Map<String, String>, body: postData);
2021-07-08 02:42:40 +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!'));
});
test('can modify data', () async {
2021-07-08 02:42:40 +00:00
var postData = json.encode({'text': 'Hello, world!'});
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-07-08 02:42:40 +00:00
var response = await client.patch(Uri.parse('$url/todos/0'),
2021-04-03 05:50:52 +00:00
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'));
});
test('can overwrite data', () async {
2021-07-08 02:42:40 +00:00
var postData = json.encode({'text': 'Hello, world!'});
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-07-08 02:42:40 +00:00
var response = await client.post(Uri.parse('$url/todos/0'),
2021-04-03 05:50:52 +00:00
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'));
});
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);
});
test('can delete data', () async {
2021-07-08 02:42:40 +00:00
var postData = json.encode({'text': 'Hello, world!'});
2021-04-03 05:50:52 +00:00
var created = await client
2021-07-08 02:42:40 +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-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
}