Make pyx URL parsing tests hermetic

flutter_test blocks real HTTP, so the two pyx tests could never pass in CI.
Inject an overridable http.Client and drive them with MockClient.

(cherry picked from commit fb032ef0ee)
This commit is contained in:
Gianluca Boiano committed 2026-09-17 23:15:29 +02:00
1 parent 83be065c86
commit e169f7c639
2 files changed
+27 -7

No files matched your search

+10 -5
View File
@@ -16,6 +16,12 @@ class MessageUrlImageHelper {
static final LinkedHashMap<String, Future<String?>> _messageImageCache = static final LinkedHashMap<String, Future<String?>> _messageImageCache =
LinkedHashMap<String, Future<String?>>(); LinkedHashMap<String, Future<String?>>();
/// HTTP client used to resolve sharing pages.
///
/// Tests override this because `flutter_test` blocks real network requests.
@visibleForTesting
static http.Client? httpClient;
static final RegExp _pyxPattern = RegExp( static final RegExp _pyxPattern = RegExp(
r'''https?://pyx\.li/\?i=([^\s<>'"`]+)''', r'''https?://pyx\.li/\?i=([^\s<>'"`]+)''',
caseSensitive: false, caseSensitive: false,
@@ -132,8 +138,9 @@ class MessageUrlImageHelper {
static Future<String?> _extractFromUrl(String pageUrl, RegExp pattern) async { static Future<String?> _extractFromUrl(String pageUrl, RegExp pattern) async {
try { try {
final response = await http final uri = Uri.parse(pageUrl);
.get(Uri.parse(pageUrl)) final client = httpClient;
final response = await (client == null ? http.get(uri) : client.get(uri))
.timeout(const Duration(seconds: 8)); .timeout(const Duration(seconds: 8));
if (response.statusCode < 200 || response.statusCode >= 300) { if (response.statusCode < 200 || response.statusCode >= 300) {
return null; return null;
@@ -141,9 +148,7 @@ class MessageUrlImageHelper {
final value = pattern.firstMatch(response.body)?.group(1); final value = pattern.firstMatch(response.body)?.group(1);
if (value == null || value.isEmpty) return null; if (value == null || value.isEmpty) return null;
final imageUrl = Uri.parse( final imageUrl = uri.resolve(value.replaceAll('&amp;', '&'));
pageUrl,
).resolve(value.replaceAll('&amp;', '&'));
return imageUrl.scheme == 'http' || imageUrl.scheme == 'https' return imageUrl.scheme == 'http' || imageUrl.scheme == 'https'
? imageUrl.toString() ? imageUrl.toString()
: null; : null;
@@ -1,23 +1,38 @@
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:meshcore_open/helpers/message_url_image_helper.dart'; import 'package:meshcore_open/helpers/message_url_image_helper.dart';
void main() { void main() {
group('MessageUrlImageHelper', () { group('MessageUrlImageHelper', () {
tearDown(() => MessageUrlImageHelper.httpClient = null);
void mockPyxPage(String id) {
MessageUrlImageHelper.httpClient = MockClient((request) async {
expect(request.url.toString(), 'https://pyx.li/?i=$id');
return http.Response(
'<html><body><img src="/i/$id.jpg"></body></html>',
200,
headers: {'content-type': 'text/html; charset=utf-8'},
);
});
}
test('parses short pyx ids', () async { test('parses short pyx ids', () async {
mockPyxPage('RzmdkTsE');
final attachment = await MessageUrlImageHelper.parse( final attachment = await MessageUrlImageHelper.parse(
'my picture: https://pyx.li/?i=RzmdkTsE text', 'my picture: https://pyx.li/?i=RzmdkTsE text',
); );
expect(attachment, isNotNull);
expect(attachment, 'https://pyx.li/i/RzmdkTsE.jpg'); expect(attachment, 'https://pyx.li/i/RzmdkTsE.jpg');
}); });
test('parses direct pyx image urls', () async { test('parses direct pyx image urls', () async {
mockPyxPage('Cd1KFiwu');
final attachment = await MessageUrlImageHelper.parse( final attachment = await MessageUrlImageHelper.parse(
'https://pyx.li/?i=Cd1KFiwu', 'https://pyx.li/?i=Cd1KFiwu',
); );
expect(attachment, isNotNull);
expect(attachment, 'https://pyx.li/i/Cd1KFiwu.jpg'); expect(attachment, 'https://pyx.li/i/Cd1KFiwu.jpg');
}); });