platform/packages/client/test/all_test.dart

91 lines
3.2 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 = MockProtevus();
2021-03-07 16:02:53 +00:00
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');
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).spec!.method, 'POST');
expect((app.client as SpecClient).spec!.path, '/post');
expect((app.client as SpecClient).spec!.headers['method'], 'post');
expect(await read((app.client as SpecClient).spec!.request.finalize()),
'post');
2017-09-24 04:12:53 +00:00
});
group('service methods', () {
test('index', () async {
await todoService.index();
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).spec!.method, 'GET');
expect((app.client as SpecClient).spec!.path, '/api/todos');
2017-09-24 04:12:53 +00:00
});
test('read', () async {
await todoService.read('sleep');
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).spec!.method, 'GET');
expect((app.client as SpecClient).spec!.path, '/api/todos/sleep');
2017-09-24 04:12:53 +00:00
});
test('create', () async {
await todoService.create({});
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).spec!.method, 'POST');
expect((app.client as SpecClient).spec!.headers['content-type'],
2017-09-24 04:12:53 +00:00
startsWith('application/json'));
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).spec!.path, '/api/todos');
expect(await read((app.client as SpecClient).spec!.request.finalize()),
'{}');
2017-09-24 04:12:53 +00:00
});
test('modify', () async {
await todoService.modify('sleep', {});
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).spec!.method, 'PATCH');
expect((app.client as SpecClient).spec!.headers['content-type'],
2017-09-24 04:12:53 +00:00
startsWith('application/json'));
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).spec!.path, '/api/todos/sleep');
expect(await read((app.client as SpecClient).spec!.request.finalize()),
'{}');
2017-09-24 04:12:53 +00:00
});
test('update', () async {
await todoService.update('sleep', {});
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).spec!.method, 'POST');
expect((app.client as SpecClient).spec!.headers['content-type'],
2017-09-24 04:12:53 +00:00
startsWith('application/json'));
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).spec!.path, '/api/todos/sleep');
expect(await read((app.client as SpecClient).spec!.request.finalize()),
'{}');
2017-09-24 04:12:53 +00:00
});
test('remove', () async {
await todoService.remove('sleep');
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).spec!.method, 'DELETE');
expect((app.client as SpecClient).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>');
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).spec!.path, '/auth/token');
2017-09-24 04:12:53 +00:00
});
test('sets type', () async {
await app.authenticate(type: 'local');
2023-06-10 04:10:21 +00:00
expect((app.client as SpecClient).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'});
2023-06-10 04:10:21 +00:00
print((app.client as SpecClient).spec?.headers);
2017-09-24 04:12:53 +00:00
expect(
2023-06-10 04:10:21 +00:00
await read((app.client as SpecClient).spec!.request.finalize()),
2018-06-23 00:18:38 +00:00
json.encode({'username': 'password'}),
2017-09-24 04:12:53 +00:00
);
});
});
}