2018-10-21 08:22:41 +00:00
|
|
|
import 'dart:io';
|
2021-05-15 08:35:27 +00:00
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
|
|
|
import 'package:angel3_test/angel3_test.dart';
|
|
|
|
import 'package:angel3_validate/angel3_validate.dart';
|
|
|
|
import 'package:angel3_websocket/server.dart';
|
2018-07-09 16:10:25 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2021-05-15 08:35:27 +00:00
|
|
|
void main() {
|
2021-09-29 07:40:27 +00:00
|
|
|
Angel app;
|
2021-04-26 01:05:34 +00:00
|
|
|
late TestClient client;
|
2018-07-09 16:10:25 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2021-05-15 08:35:27 +00:00
|
|
|
app = Angel()
|
2018-08-28 14:24:44 +00:00
|
|
|
..get('/hello', (req, res) => 'Hello')
|
2018-07-09 16:10:25 +00:00
|
|
|
..get(
|
|
|
|
'/error',
|
2021-05-15 08:35:27 +00:00
|
|
|
(req, res) => throw 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'
|
2021-05-18 11:43:58 +00:00
|
|
|
..add(gzip.encode('Poop'.codeUnits));
|
|
|
|
await res.close();
|
2018-07-09 16:10:25 +00:00
|
|
|
})
|
|
|
|
..use(
|
|
|
|
'/foo',
|
2021-05-15 08:35:27 +00:00
|
|
|
AnonymousService(
|
2018-07-09 16:10:25 +00:00
|
|
|
index: ([params]) async => [
|
|
|
|
{'michael': 'jackson'}
|
|
|
|
],
|
2021-04-26 01:05:34 +00:00
|
|
|
create: (dynamic data, [params]) async => {'foo': 'bar'}));
|
2018-07-09 16:10:25 +00:00
|
|
|
|
2021-05-15 08:35:27 +00:00
|
|
|
var ws = AngelWebSocket(app);
|
2021-09-29 07:40:27 +00:00
|
|
|
await app.configure(ws.configureServer);
|
|
|
|
app.all('/ws', ws.handleRequest);
|
2018-07-09 16:10:25 +00:00
|
|
|
|
2021-09-29 07:40:27 +00:00
|
|
|
app.errorHandler = (e, req, res) => e.toJson();
|
2018-07-09 16:10:25 +00:00
|
|
|
|
2021-09-29 07:40:27 +00:00
|
|
|
client = await connectTo(app);
|
2018-07-09 16:10:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
|
|
|
await client.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
2021-05-15 08:35:27 +00:00
|
|
|
hasValidBody(Validator({
|
2018-07-09 16:10:25 +00:00
|
|
|
'michael*': [isString, isNotEmpty, equals('jackson')],
|
2021-05-15 08:35:27 +00:00
|
|
|
'billie': Validator({
|
2018-07-09 16:10:25 +00:00
|
|
|
'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');
|
2021-05-18 11:43:58 +00:00
|
|
|
await foo.create({});
|
2018-07-09 16:10:25 +00:00
|
|
|
var result = await foo.onCreated.first;
|
|
|
|
expect(result.data, equals({'foo': 'bar'}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|