platform/test/simple_test.dart

108 lines
2.9 KiB
Dart
Raw Normal View History

2017-03-25 15:26:59 +00:00
import 'dart:io';
2017-03-25 15:26:00 +00:00
import 'package:angel_framework/angel_framework.dart';
2016-12-10 17:05:31 +00:00
import 'package:angel_test/angel_test.dart';
2017-03-25 15:26:00 +00:00
import 'package:angel_validate/angel_validate.dart';
import 'package:angel_websocket/server.dart';
2016-12-10 17:05:31 +00:00
import 'package:test/test.dart';
main() {
2017-03-25 15:26:00 +00:00
Angel app;
TestClient client;
2016-12-10 17:05:31 +00:00
2016-12-10 17:05:45 +00:00
setUp(() async {
2017-03-25 15:26:00 +00:00
app = new Angel()
2016-12-10 18:11:27 +00:00
..get('/hello', 'Hello')
2017-03-25 15:26:00 +00:00
..get(
'/error',
() => throw new AngelHttpException.forbidden(message: 'Test')
..errors.addAll(['foo', 'bar']))
..get('/body', (ResponseContext res) {
res
..write('OK')
..end();
})
..get(
'/valid',
() => {
'michael': 'jackson',
'billie': {'jean': 'hee-hee', 'is_my_lover': false}
})
2016-12-10 18:11:27 +00:00
..post('/hello', (req, res) async {
return {'bar': req.body['foo']};
2017-03-25 15:26:00 +00:00
})
..use(
'/foo',
new AnonymousService(
create: (data, [params]) async => {'foo': 'bar'}));
var ws = new AngelWebSocket();
await app.configure(ws);
2016-12-10 17:05:45 +00:00
2017-03-25 15:26:00 +00:00
client = await connectTo(app);
2016-12-10 17:05:45 +00:00
});
2016-12-10 18:11:27 +00:00
tearDown(() async {
2017-03-25 15:26:00 +00:00
await client.close();
2016-12-10 18:11:27 +00:00
app = null;
});
2017-03-25 15:26:00 +00:00
group('matchers', () {
group('isJson+hasStatus', () {
test('get', () async {
final response = await client.get('/hello');
expect(response, isJson('Hello'));
});
2017-03-25 04:28:50 +00:00
2017-03-25 15:26:00 +00:00
test('post', () async {
final response = await client.post('/hello', body: {'foo': 'baz'});
expect(response, allOf(hasStatus(200), isJson({'bar': 'baz'})));
});
2016-12-10 18:11:27 +00:00
});
2017-03-25 15:26:00 +00:00
test('isAngelHttpException', () async {
var res = await client.get('/error');
expect(res, isAngelHttpException());
expect(
res,
isAngelHttpException(
statusCode: 403, message: 'Test', errors: ['foo', 'bar']));
2016-12-10 18:11:27 +00:00
});
2017-03-25 15:26:00 +00:00
test('hasBody', () async {
var res = await client.get('/body');
expect(res, hasBody());
expect(res, hasBody('OK'));
2016-12-10 18:11:27 +00:00
});
2017-03-25 15:26:00 +00:00
test('hasHeader', () async {
var res = await client.get('/hello');
expect(res, hasHeader('server'));
expect(res, hasHeader('server', 'angel'));
expect(res, hasHeader('server', ['angel']));
2016-12-10 18:11:27 +00:00
});
2017-03-25 15:26:59 +00:00
test('hasValidBody+hasContentType', () async {
2017-03-25 15:26:00 +00:00
var res = await client.get('/valid');
expect(res, hasContentType('application/json'));
2017-03-25 15:26:59 +00:00
expect(res, hasContentType(ContentType.JSON));
2017-03-25 15:26:00 +00:00
expect(
res,
hasValidBody(new Validator({
'michael*': [isString, isNotEmpty, equals('jackson')],
'billie': new Validator({
'jean': [isString, isNotEmpty],
'is_my_lover': [isBool, isFalse]
})
})));
2016-12-10 18:11:27 +00:00
});
2017-03-25 15:26:00 +00:00
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'}));
2016-12-10 18:11:27 +00:00
});
});
}