2017-08-03 16:40:21 +00:00
|
|
|
import 'dart:convert';
|
2016-04-18 03:27:23 +00:00
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
2016-06-21 22:56:04 +00:00
|
|
|
import 'package:json_god/json_god.dart' as god;
|
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() {
|
2016-09-15 19:53:01 +00:00
|
|
|
Map headers = {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
};
|
|
|
|
Angel app;
|
|
|
|
String url;
|
|
|
|
http.Client client;
|
2016-04-18 03:27:23 +00:00
|
|
|
|
2016-09-15 19:53:01 +00:00
|
|
|
setUp(() async {
|
2017-02-23 00:37:15 +00:00
|
|
|
app = new Angel()
|
|
|
|
..use('/todos', new TypedService<Todo>(new MapService()))
|
|
|
|
..fatalErrorStream.listen((e) {
|
|
|
|
print('Whoops: ${e.error}');
|
|
|
|
print(e.stack);
|
|
|
|
});
|
|
|
|
|
|
|
|
await app.startServer();
|
2016-09-15 19:53:01 +00:00
|
|
|
client = new http.Client();
|
|
|
|
url = "http://${app.httpServer.address.host}:${app.httpServer.port}";
|
|
|
|
});
|
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);
|
2017-08-03 16:40:21 +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 {
|
|
|
|
String postData = god.serialize({'text': 'Hello, world!'});
|
2017-02-23 00:37:15 +00:00
|
|
|
var response =
|
|
|
|
await client.post("$url/todos", headers: headers, body: postData);
|
2017-03-28 23:29:22 +00:00
|
|
|
expect(response.statusCode, 201);
|
2016-09-15 19:53:01 +00:00
|
|
|
var json = god.deserialize(response.body);
|
|
|
|
print(json);
|
|
|
|
expect(json['text'], equals('Hello, world!'));
|
|
|
|
});
|
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 {
|
|
|
|
String postData = god.serialize({'text': 'Hello, world!'});
|
2017-02-23 00:37:15 +00:00
|
|
|
await client.post("$url/todos", headers: headers, body: postData);
|
|
|
|
var response = await client.get("$url/todos/0");
|
2017-03-28 23:29:22 +00:00
|
|
|
expect(response.statusCode, 200);
|
2016-09-15 19:53:01 +00:00
|
|
|
var json = god.deserialize(response.body);
|
|
|
|
print(json);
|
|
|
|
expect(json['text'], equals('Hello, world!'));
|
|
|
|
});
|
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 {
|
|
|
|
String postData = god.serialize({'text': 'Hello, world!'});
|
2017-02-23 00:37:15 +00:00
|
|
|
await client.post("$url/todos", headers: headers, body: postData);
|
2016-09-15 19:53:01 +00:00
|
|
|
postData = god.serialize({'text': 'modified'});
|
2017-02-23 00:37:15 +00:00
|
|
|
var response =
|
|
|
|
await client.patch("$url/todos/0", headers: headers, body: postData);
|
2017-03-28 23:29:22 +00:00
|
|
|
expect(response.statusCode, 200);
|
2016-09-15 19:53:01 +00:00
|
|
|
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!'});
|
2017-02-23 00:37:15 +00:00
|
|
|
await client.post("$url/todos", headers: headers, body: postData);
|
2016-09-15 19:53:01 +00:00
|
|
|
postData = god.serialize({'over': 'write'});
|
2017-02-23 00:37:15 +00:00
|
|
|
var response =
|
|
|
|
await client.post("$url/todos/0", headers: headers, body: postData);
|
2017-03-28 23:29:22 +00:00
|
|
|
expect(response.statusCode, 200);
|
2016-09-15 19:53:01 +00:00
|
|
|
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-08-03 16:40:21 +00:00
|
|
|
var created = await client.post("$url/todos", headers: headers, body: postData).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);
|
2016-09-15 19:53:01 +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
|
|
|
}
|