platform/test/services.dart

121 lines
3.8 KiB
Dart
Raw Normal View History

import 'dart:mirrors';
2016-04-18 03:27:23 +00:00
import 'package:angel_framework/angel_framework.dart';
import 'package:http/http.dart' as http;
import 'package:json_god/json_god.dart';
import 'package:test/test.dart';
class Todo {
String text;
String over;
2016-04-18 03:27:23 +00:00
}
main() {
group('Services', () {
2016-04-18 03:27:23 +00:00
Map headers = {
'Accept': 'application/json',
2016-04-18 03:27:23 +00:00
'Content-Type': 'application/json'
};
2016-06-21 04:19:43 +00:00
Angel app;
2016-04-18 03:27:23 +00:00
String url;
http.Client client;
God god;
setUp(() async {
2016-06-21 04:19:43 +00:00
app = new Angel();
2016-04-18 03:27:23 +00:00
client = new http.Client();
god = new God();
Service todos = new MemoryService<Todo>();
2016-06-21 04:19:43 +00:00
app.use('/todos', todos);
print(app.service("todos"));
await app.startServer(null, 0);
url = "http://${app.httpServer.address.host}:${app.httpServer.port}";
2016-04-18 03:27:23 +00:00
});
tearDown(() async {
2016-06-21 04:19:43 +00:00
app = null;
2016-04-18 03:27:23 +00:00
url = null;
client.close();
client = null;
god = null;
});
group('memory', () {
test('can index an empty service', () async {
var response = await client.get("$url/todos/");
print(response.body);
2016-04-18 03:27:23 +00:00
expect(response.body, equals('[]'));
for (int i = 0; i < 3; i++) {
String postData = god.serialize({'text': 'Hello, world!'});
await client.post(
"$url/todos", headers: headers, body: postData);
}
response = await client.get("$url/todos");
print(response.body);
expect(god
.deserialize(response.body)
.length, equals(3));
2016-04-18 03:27:23 +00:00
});
test('can create data', () async {
String postData = god.serialize({'text': 'Hello, world!'});
var response = await client.post(
"$url/todos", headers: headers, body: postData);
2016-04-18 03:27:23 +00:00
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!'});
await client.post(
"$url/todos", headers: headers, body: postData);
var response = await client.get(
"$url/todos/0");
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!'});
await client.post(
"$url/todos", headers: headers, body: postData);
postData = god.serialize({'text': 'modified'});
var response = await client.patch(
"$url/todos/0", headers: headers, body: postData);
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!'});
await client.post(
"$url/todos", headers: headers, body: postData);
postData = god.serialize({'over': 'write'});
var response = await client.post(
"$url/todos/0", headers: headers, body: postData);
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!'});
await client.post(
"$url/todos", headers: headers, body: postData);
var response = await client.delete(
"$url/todos/0");
var json = god.deserialize(response.body);
print(json);
expect(json['text'], equals('Hello, world!'));
response = await client.get("$url/todos");
print(response.body);
expect(god
.deserialize(response.body)
.length, equals(0));
});
2016-04-18 03:27:23 +00:00
});
});
}