platform/test/all_test.dart

86 lines
2.7 KiB
Dart
Raw Normal View History

2017-09-24 04:12:53 +00:00
import 'package:angel_client/angel_client.dart';
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';
main() {
var app = new MockAngel();
Service todoService = app.service('api/todos');
test('sets method,body,headers,path', () async {
await app.post('/post', headers: {'method': 'post'}, body: 'post');
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');
});
group('service methods', () {
test('index', () async {
await todoService.index();
expect(app.client.spec.method, 'GET');
expect(app.client.spec.path, '/api/todos');
});
test('read', () async {
await todoService.read('sleep');
expect(app.client.spec.method, 'GET');
expect(app.client.spec.path, '/api/todos/sleep');
});
test('create', () async {
await todoService.create({});
expect(app.client.spec.method, 'POST');
expect(app.client.spec.headers['content-type'],
startsWith('application/json'));
2019-01-06 02:08:31 +00:00
expect(app.client.spec.path, '/api/todos');
2017-09-24 04:12:53 +00:00
expect(await read(app.client.spec.request.finalize()), '{}');
});
test('modify', () async {
await todoService.modify('sleep', {});
expect(app.client.spec.method, 'PATCH');
expect(app.client.spec.headers['content-type'],
startsWith('application/json'));
expect(app.client.spec.path, '/api/todos/sleep');
expect(await read(app.client.spec.request.finalize()), '{}');
});
test('update', () async {
await todoService.update('sleep', {});
expect(app.client.spec.method, 'POST');
expect(app.client.spec.headers['content-type'],
startsWith('application/json'));
expect(app.client.spec.path, '/api/todos/sleep');
expect(await read(app.client.spec.request.finalize()), '{}');
});
test('remove', () async {
await todoService.remove('sleep');
expect(app.client.spec.method, 'DELETE');
expect(app.client.spec.path, '/api/todos/sleep');
});
});
group('authentication', () {
test('no type defaults to token', () async {
await app.authenticate(credentials: '<jwt>');
expect(app.client.spec.path, '/auth/token');
});
test('sets type', () async {
await app.authenticate(type: 'local');
expect(app.client.spec.path, '/auth/local');
});
test('credentials send right body', () async {
await app
.authenticate(type: 'local', credentials: {'username': 'password'});
expect(
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
);
});
});
}