platform/packages/test/example/main.dart

144 lines
4 KiB
Dart
Raw Normal View History

2018-10-21 08:22:41 +00:00
import 'dart:io';
2018-07-09 16:10:25 +00:00
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_test/angel_test.dart';
import 'package:angel_validate/angel_validate.dart';
import 'package:angel_websocket/server.dart';
import 'package:test/test.dart';
main() {
Angel app;
TestClient client;
setUp(() async {
app = new Angel()
2018-08-28 14:24:44 +00:00
..get('/hello', (req, res) => 'Hello')
2018-07-09 16:10:25 +00:00
..get(
'/error',
2018-08-28 14:24:44 +00:00
(req, res) => throw new AngelHttpException.forbidden(message: 'Test')
2018-07-09 16:10:25 +00:00
..errors.addAll(['foo', 'bar']))
2018-08-28 14:24:44 +00:00
..get('/body', (req, res) {
2018-07-09 16:10:25 +00:00
res
..write('OK')
2018-08-28 14:24:44 +00:00
..close();
2018-07-09 16:10:25 +00:00
})
..get(
'/valid',
2018-08-28 14:24:44 +00:00
(req, res) => {
2018-07-09 16:10:25 +00:00
'michael': 'jackson',
'billie': {'jean': 'hee-hee', 'is_my_lover': false}
})
..post('/hello', (req, res) async {
2019-01-06 17:41:06 +00:00
var body = await req.parseBody().then((_) => req.bodyAsMap);
2018-08-28 14:24:44 +00:00
return {'bar': body['foo']};
2018-07-09 16:10:25 +00:00
})
..get('/gzip', (req, res) async {
res
..headers['content-encoding'] = 'gzip'
2018-10-21 08:22:41 +00:00
..add(gzip.encode('Poop'.codeUnits))
2018-08-28 14:24:44 +00:00
..close();
2018-07-09 16:10:25 +00:00
})
..use(
'/foo',
new AnonymousService(
index: ([params]) async => [
{'michael': 'jackson'}
],
create: (data, [params]) async => {'foo': 'bar'}));
var ws = new AngelWebSocket(app);
await app.configure(ws.configureServer);
app.all('/ws', ws.handleRequest);
app.errorHandler = (e, req, res) => e.toJson();
client = await connectTo(app);
});
tearDown(() async {
await client.close();
app = null;
});
group('matchers', () {
group('isJson+hasStatus', () {
test('get', () async {
2021-03-07 16:47:49 +00:00
final response = await client.get(Uri.parse('/hello'));
2018-07-09 16:10:25 +00:00
expect(response, isJson('Hello'));
});
test('post', () async {
2021-03-07 16:47:49 +00:00
final response =
await client.post(Uri.parse('/hello'), body: {'foo': 'baz'});
2018-07-09 16:10:25 +00:00
expect(response, allOf(hasStatus(200), isJson({'bar': 'baz'})));
});
});
test('isAngelHttpException', () async {
2021-03-07 16:47:49 +00:00
var res = await client.get(Uri.parse('/error'));
2018-07-09 16:10:25 +00:00
print(res.body);
expect(res, isAngelHttpException());
expect(
res,
isAngelHttpException(
statusCode: 403, message: 'Test', errors: ['foo', 'bar']));
});
test('hasBody', () async {
2021-03-07 16:47:49 +00:00
var res = await client.get(Uri.parse('/body'));
2018-07-09 16:10:25 +00:00
expect(res, hasBody());
expect(res, hasBody('OK'));
});
test('hasHeader', () async {
2021-03-07 16:47:49 +00:00
var res = await client.get(Uri.parse('/hello'));
2018-07-09 16:10:25 +00:00
expect(res, hasHeader('server'));
expect(res, hasHeader('server', 'angel'));
expect(res, hasHeader('server', ['angel']));
});
test('hasValidBody+hasContentType', () async {
2021-03-07 16:47:49 +00:00
var res = await client.get(Uri.parse('/valid'));
2018-07-09 16:10:25 +00:00
expect(res, hasContentType('application/json'));
expect(
res,
hasValidBody(new Validator({
'michael*': [isString, isNotEmpty, equals('jackson')],
'billie': new Validator({
'jean': [isString, isNotEmpty],
'is_my_lover': [isBool, isFalse]
})
})));
});
test('gzip decode', () async {
2021-03-07 16:47:49 +00:00
var res = await client.get(Uri.parse('/gzip'));
2018-07-09 16:10:25 +00:00
expect(res, hasHeader('content-encoding', 'gzip'));
expect(res, hasBody('Poop'));
});
group('service', () {
test('index', () async {
var foo = client.service('foo');
var result = await foo.index();
expect(result, [
{'michael': 'jackson'}
]);
});
test('index', () async {
var foo = client.service('foo');
var result = await foo.create({});
expect(result, {'foo': 'bar'});
});
});
test('websocket', () async {
var ws = await client.websocket();
var foo = ws.service('foo');
foo.create({});
var result = await foo.onCreated.first;
expect(result.data, equals({'foo': 'bar'}));
});
});
}