platform/packages/client/test/all_test.dart

87 lines
2.7 KiB
Dart
Raw Normal View History

2018-08-26 22:41:01 +00:00
import 'dart:convert';
2017-09-24 04:12:53 +00:00
import 'package:test/test.dart';
import 'common.dart';
2021-03-07 16:02:53 +00:00
void main() {
var app = MockAngel();
var todoService = app.service('api/todos');
2017-09-24 04:12:53 +00:00
test('sets method,body,headers,path', () async {
2021-03-07 16:02:53 +00:00
await app.post(Uri.parse('/post'),
headers: {'method': 'post'}, body: 'post');
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.method, 'POST');
expect(app.client.spec!.path, '/post');
expect(app.client.spec!.headers['method'], 'post');
expect(await read(app.client.spec!.request.finalize()), 'post');
2017-09-24 04:12:53 +00:00
});
group('service methods', () {
test('index', () async {
await todoService.index();
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.method, 'GET');
expect(app.client.spec!.path, '/api/todos');
2017-09-24 04:12:53 +00:00
});
test('read', () async {
await todoService.read('sleep');
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.method, 'GET');
expect(app.client.spec!.path, '/api/todos/sleep');
2017-09-24 04:12:53 +00:00
});
test('create', () async {
await todoService.create({});
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.method, 'POST');
expect(app.client.spec!.headers['content-type'],
2017-09-24 04:12:53 +00:00
startsWith('application/json'));
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.path, '/api/todos');
expect(await read(app.client.spec!.request.finalize()), '{}');
2017-09-24 04:12:53 +00:00
});
test('modify', () async {
await todoService.modify('sleep', {});
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.method, 'PATCH');
expect(app.client.spec!.headers['content-type'],
2017-09-24 04:12:53 +00:00
startsWith('application/json'));
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.path, '/api/todos/sleep');
expect(await read(app.client.spec!.request.finalize()), '{}');
2017-09-24 04:12:53 +00:00
});
test('update', () async {
await todoService.update('sleep', {});
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.method, 'POST');
expect(app.client.spec!.headers['content-type'],
2017-09-24 04:12:53 +00:00
startsWith('application/json'));
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.path, '/api/todos/sleep');
expect(await read(app.client.spec!.request.finalize()), '{}');
2017-09-24 04:12:53 +00:00
});
test('remove', () async {
await todoService.remove('sleep');
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.method, 'DELETE');
expect(app.client.spec!.path, '/api/todos/sleep');
2017-09-24 04:12:53 +00:00
});
});
group('authentication', () {
test('no type defaults to token', () async {
await app.authenticate(credentials: '<jwt>');
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.path, '/auth/token');
2017-09-24 04:12:53 +00:00
});
test('sets type', () async {
await app.authenticate(type: 'local');
2021-04-10 13:22:20 +00:00
expect(app.client.spec!.path, '/auth/local');
2017-09-24 04:12:53 +00:00
});
test('credentials send right body', () async {
await app
.authenticate(type: 'local', credentials: {'username': 'password'});
2021-07-15 08:11:54 +00:00
print(app.client.spec?.headers);
2017-09-24 04:12:53 +00:00
expect(
2021-04-10 13:22:20 +00:00
await read(app.client.spec!.request.finalize()),
2018-06-23 00:18:38 +00:00
json.encode({'username': 'password'}),
2017-09-24 04:12:53 +00:00
);
});
});
}