platform/test/services_test.dart

114 lines
3.8 KiB
Dart
Raw Normal View History

2016-04-18 03:27:23 +00:00
import 'package:angel_framework/angel_framework.dart';
2018-06-08 07:06:26 +00:00
import 'package:dart2_constant/convert.dart';
2016-04-18 03:27:23 +00:00
import 'package:http/http.dart' as http;
2016-06-21 22:56:04 +00:00
import 'package:json_god/json_god.dart' as god;
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;
String over;
2016-04-18 03:27:23 +00:00
}
main() {
2018-07-09 14:49:59 +00:00
Map headers = <String, String>{
'Accept': 'application/json',
'Content-Type': 'application/json'
};
Angel app;
String url;
http.Client client;
2016-04-18 03:27:23 +00:00
setUp(() async {
2017-02-23 00:37:15 +00:00
app = new Angel()
..use('/todos', new TypedService<Todo>(new MapService()))
2017-09-22 14:03:23 +00:00
..errorHandler = (e, req, res) {
2017-02-23 00:37:15 +00:00
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();
client = new http.Client();
2018-02-07 05:36:24 +00:00
url = "http://${server.address.host}:${server.port}";
});
2016-04-18 03:27:23 +00:00
tearDown(() async {
2017-05-27 12:39:45 +00:00
await app.close();
app = null;
url = null;
client.close();
client = null;
});
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);
});
test('can create data', () async {
String postData = god.serialize({'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);
var json = god.deserialize(response.body);
print(json);
expect(json['text'], equals('Hello, world!'));
});
test('can fetch data', () async {
String postData = god.serialize({'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);
var json = god.deserialize(response.body);
print(json);
expect(json['text'], equals('Hello, world!'));
});
test('can modify data', () async {
String postData = god.serialize({'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);
postData = god.serialize({'text': 'modified'});
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);
var json = god.deserialize(response.body);
print(json);
expect(json['text'], equals('modified'));
});
test('can overwrite data', () async {
String postData = god.serialize({'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);
postData = god.serialize({'over': 'write'});
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);
var json = god.deserialize(response.body);
print(json);
expect(json['text'], equals(null));
expect(json['over'], equals('write'));
});
test('can delete data', () async {
String postData = god.serialize({'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));
var response = await client.delete("$url/todos/${created['id']}");
2017-03-28 23:29:22 +00:00
expect(response.statusCode, 200);
2018-06-08 07:06:26 +00:00
var json_ = god.deserialize(response.body);
print(json_);
expect(json_['text'], equals('Hello, world!'));
2016-04-18 03:27:23 +00:00
});
});
2017-02-23 00:37:15 +00:00
}