56 lines
1.4 KiB
Dart
56 lines
1.4 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
/// Sequential fake adapter for deterministic Dio unit tests.
|
|
typedef AdapterHandler =
|
|
Future<ResponseBody> Function(
|
|
RequestOptions options,
|
|
Stream<Uint8List>? requestStream,
|
|
Future<void>? cancelFuture,
|
|
);
|
|
|
|
class QueueHttpClientAdapter implements HttpClientAdapter {
|
|
QueueHttpClientAdapter(List<AdapterHandler> handlers)
|
|
: _handlers = List<AdapterHandler>.from(handlers);
|
|
|
|
final List<AdapterHandler> _handlers;
|
|
int fetchCount = 0;
|
|
|
|
@override
|
|
void close({bool force = false}) {}
|
|
|
|
@override
|
|
Future<ResponseBody> fetch(
|
|
RequestOptions options,
|
|
Stream<Uint8List>? requestStream,
|
|
Future<void>? cancelFuture,
|
|
) async {
|
|
if (fetchCount >= _handlers.length) {
|
|
throw StateError(
|
|
'No queued response for ${options.method} ${options.path} at call $fetchCount',
|
|
);
|
|
}
|
|
|
|
final AdapterHandler handler = _handlers[fetchCount];
|
|
fetchCount += 1;
|
|
return handler(options, requestStream, cancelFuture);
|
|
}
|
|
}
|
|
|
|
ResponseBody jsonResponse(
|
|
int statusCode,
|
|
Map<String, dynamic> body, {
|
|
Map<String, List<String>> headers = const <String, List<String>>{},
|
|
}) {
|
|
return ResponseBody.fromString(
|
|
jsonEncode(body),
|
|
statusCode,
|
|
headers: <String, List<String>>{
|
|
Headers.contentTypeHeader: <String>[Headers.jsonContentType],
|
|
...headers,
|
|
},
|
|
);
|
|
}
|