platform/packages/client/test/common.dart

74 lines
1.9 KiB
Dart
Raw Permalink Normal View History

2017-09-24 04:12:53 +00:00
import 'dart:async';
import 'package:protevus_client/base_protevus_client.dart';
2018-08-26 22:41:01 +00:00
import 'dart:convert';
2017-09-24 04:12:53 +00:00
import 'package:http/src/base_client.dart' as http;
import 'package:http/src/base_request.dart' as http;
import 'package:http/src/streamed_response.dart' as http;
Future<String> read(Stream<List<int>> stream) =>
2018-06-23 00:18:38 +00:00
stream.transform(utf8.decoder).join();
2017-09-24 04:12:53 +00:00
class MockProtevus extends BaseProtevusClient {
2023-06-10 04:10:21 +00:00
final SpecClient specClient = SpecClient();
2017-09-24 04:12:53 +00:00
@override
2023-06-10 04:10:21 +00:00
get client => specClient;
2017-09-24 04:12:53 +00:00
MockProtevus() : super(SpecClient(), 'http://localhost:3000');
2017-09-24 04:12:53 +00:00
@override
2021-04-10 13:22:20 +00:00
Stream<String> authenticateViaPopup(String url,
{String eventName = 'token'}) {
throw UnsupportedError('Nope');
2017-09-24 04:12:53 +00:00
}
}
class SpecClient extends http.BaseClient {
2021-04-10 13:22:20 +00:00
Spec? _spec;
2017-09-24 04:12:53 +00:00
2021-04-10 13:22:20 +00:00
Spec? get spec => _spec;
2017-09-24 04:12:53 +00:00
@override
2021-04-10 13:22:20 +00:00
Future<http.StreamedResponse> send(http.BaseRequest request) {
_spec = Spec(request, request.method, request.url.path, request.headers,
2017-09-24 04:12:53 +00:00
request.contentLength);
2018-11-04 01:34:21 +00:00
dynamic data = {'text': 'Clean your room!', 'completed': true};
2017-09-24 04:12:53 +00:00
2018-11-04 01:34:21 +00:00
if (request.url.path.contains('auth')) {
2017-09-24 04:12:53 +00:00
data = {
'token': '<jwt>',
'data': {'username': 'password'}
};
2018-11-04 01:34:21 +00:00
} else if (request.url.path == '/api/todos' && request.method == 'GET') {
data = [data];
}
2017-09-24 04:12:53 +00:00
2021-04-10 13:22:20 +00:00
return Future<http.StreamedResponse>.value(http.StreamedResponse(
Stream<List<int>>.fromIterable([utf8.encode(json.encode(data))]),
2017-09-24 04:12:53 +00:00
200,
headers: {
'content-type': 'application/json',
},
));
}
}
class Spec {
final http.BaseRequest request;
final String method, path;
final Map<String, String> headers;
2021-04-10 13:22:20 +00:00
final int? contentLength;
2017-09-24 04:12:53 +00:00
Spec(this.request, this.method, this.path, this.headers, this.contentLength);
@override
String toString() {
return {
'method': method,
'path': path,
'headers': headers,
'content_length': contentLength,
}.toString();
}
}