todo-app/test/images/fake_dufs.dart
Krzysztof kuhy Rudnicki 0798f7b1d0 Attach images to notes, stored on dufs and kept offline
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-25 14:11:15 +02:00

49 lines
1.5 KiB
Dart

/// An in-memory dufs for image-sync tests: a map of `todo-images/...` paths
/// to bytes behind a MockClient, with an [online] switch.
///
/// Deliberately NOT named `*_test.dart`: the runner would collect it.
library;
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:todo/images/dufs_image_client.dart';
/// Fake server state plus a client factory over it.
class FakeDufs {
/// Stored files, keyed by the path after `/todo-images/`.
final Map<String, List<int>> files = {};
/// When false every request fails like a dropped connection.
bool online = true;
/// Every request's `METHOD path`, in order.
final List<String> requests = [];
/// The HTTP client the image client talks through.
http.Client httpClient() => MockClient((request) async {
requests.add('${request.method} ${request.url.path}');
if (!online) throw const SocketException('offline');
final key = request.url.path.replaceFirst('/todo-images/', '');
switch (request.method) {
case 'PUT':
files[key] = request.bodyBytes;
return http.Response('', 201);
case 'GET':
final bytes = files[key];
return bytes == null
? http.Response('', 404)
: http.Response.bytes(bytes, 200);
default:
return http.Response('', 207);
}
});
/// A client over this fake.
DufsImageClient client() => DufsImageClient(
const DufsLogin(user: 'todo', password: 'pw', baseUrl: 'https://d'),
httpClient: httpClient(),
);
}