2018-07-09 16:21:14 +00:00
|
|
|
import 'dart:io' show ContentType;
|
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';
|
2018-07-09 16:21:14 +00:00
|
|
|
import 'package:dart2_constant/io.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()
|
2018-08-28 14:24:44 +00:00
|
|
|
..get('/hello', (req, res) => 'Hello')
|
2017-03-25 15:26:00 +00:00
|
|
|
..get(
|
|
|
|
'/error',
|
2018-08-28 14:24:44 +00:00
|
|
|
(req, res) => throw new AngelHttpException.forbidden(message: 'Test')
|
2017-03-25 15:26:00 +00:00
|
|
|
..errors.addAll(['foo', 'bar']))
|
2018-08-28 14:24:44 +00:00
|
|
|
..get('/body', (req, res) {
|
2017-03-25 15:26:00 +00:00
|
|
|
res
|
|
|
|
..write('OK')
|
2018-08-28 14:24:44 +00:00
|
|
|
..close();
|
2017-03-25 15:26:00 +00:00
|
|
|
})
|
|
|
|
..get(
|
|
|
|
'/valid',
|
2018-08-28 14:24:44 +00:00
|
|
|
(req, res) => {
|
2017-03-25 15:26:00 +00:00
|
|
|
'michael': 'jackson',
|
|
|
|
'billie': {'jean': 'hee-hee', 'is_my_lover': false}
|
|
|
|
})
|
2016-12-10 18:11:27 +00:00
|
|
|
..post('/hello', (req, res) async {
|
2018-08-28 14:24:44 +00:00
|
|
|
var body = await req.parseBody();
|
|
|
|
return {'bar': body['foo']};
|
2017-03-25 15:26:00 +00:00
|
|
|
})
|
2017-04-25 02:50:37 +00:00
|
|
|
..get('/gzip', (req, res) async {
|
|
|
|
res
|
2018-07-09 16:10:25 +00:00
|
|
|
..headers['content-encoding'] = 'gzip'
|
|
|
|
..write(gzip.encode('Poop'.codeUnits))
|
2018-08-28 14:24:44 +00:00
|
|
|
..close();
|
2017-04-25 02:50:37 +00:00
|
|
|
})
|
2017-03-25 15:26:00 +00:00
|
|
|
..use(
|
|
|
|
'/foo',
|
|
|
|
new AnonymousService(
|
2017-03-29 02:28:58 +00:00
|
|
|
index: ([params]) async => [
|
|
|
|
{'michael': 'jackson'}
|
|
|
|
],
|
2017-03-25 15:26:00 +00:00
|
|
|
create: (data, [params]) async => {'foo': 'bar'}));
|
|
|
|
|
2017-09-24 04:40:35 +00:00
|
|
|
var ws = new AngelWebSocket(app);
|
|
|
|
await app.configure(ws.configureServer);
|
|
|
|
app.all('/ws', ws.handleRequest);
|
2016-12-10 17:05:45 +00:00
|
|
|
|
2017-11-18 05:04:42 +00:00
|
|
|
app.errorHandler = (e, req, res) => e.toJson();
|
|
|
|
|
2018-07-09 16:10:25 +00:00
|
|
|
client = await connectTo(app, useZone: false);
|
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');
|
2017-11-18 05:04:42 +00:00
|
|
|
print(res.body);
|
2017-03-25 15:26:00 +00:00
|
|
|
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');
|
2018-07-09 16:10:25 +00:00
|
|
|
print('Body: ${res.body}');
|
2017-03-25 15:26:00 +00:00
|
|
|
expect(res, hasContentType('application/json'));
|
2018-07-09 16:10:25 +00:00
|
|
|
expect(res, hasContentType(new ContentType('application', '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-04-25 02:50:37 +00:00
|
|
|
test('gzip decode', () async {
|
|
|
|
var res = await client.get('/gzip');
|
2018-07-09 16:21:14 +00:00
|
|
|
print('Body: ${res.body}');
|
2018-07-09 16:10:25 +00:00
|
|
|
expect(res, hasHeader('content-encoding', 'gzip'));
|
2017-04-25 02:50:37 +00:00
|
|
|
expect(res, hasBody('Poop'));
|
|
|
|
});
|
|
|
|
|
2017-03-29 02:28:58 +00:00
|
|
|
group('service', () {
|
|
|
|
test('index', () async {
|
|
|
|
var foo = client.service('foo');
|
|
|
|
var result = await foo.index();
|
2017-04-25 02:50:37 +00:00
|
|
|
expect(result, [
|
|
|
|
{'michael': 'jackson'}
|
|
|
|
]);
|
2017-03-29 02:28:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('index', () async {
|
|
|
|
var foo = client.service('foo');
|
|
|
|
var result = await foo.create({});
|
|
|
|
expect(result, {'foo': 'bar'});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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;
|
2018-07-09 16:10:25 +00:00
|
|
|
expect(result is Map ? result : result.data, equals({'foo': 'bar'}));
|
2016-12-10 18:11:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|