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.
This commit is contained in:
2 files changed
+27
-7
No files matched your search
@@ -16,6 +16,12 @@ class MessageUrlImageHelper {
|
||||
static final LinkedHashMap<String, Future<String?>> _messageImageCache =
|
||||
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(
|
||||
r'''https?://pyx\.li/\?i=([^\s<>'"`]+)''',
|
||||
caseSensitive: false,
|
||||
@@ -132,8 +138,9 @@ class MessageUrlImageHelper {
|
||||
|
||||
static Future<String?> _extractFromUrl(String pageUrl, RegExp pattern) async {
|
||||
try {
|
||||
final response = await http
|
||||
.get(Uri.parse(pageUrl))
|
||||
final uri = Uri.parse(pageUrl);
|
||||
final client = httpClient;
|
||||
final response = await (client == null ? http.get(uri) : client.get(uri))
|
||||
.timeout(const Duration(seconds: 8));
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
return null;
|
||||
@@ -141,9 +148,7 @@ class MessageUrlImageHelper {
|
||||
final value = pattern.firstMatch(response.body)?.group(1);
|
||||
if (value == null || value.isEmpty) return null;
|
||||
|
||||
final imageUrl = Uri.parse(
|
||||
pageUrl,
|
||||
).resolve(value.replaceAll('&', '&'));
|
||||
final imageUrl = uri.resolve(value.replaceAll('&', '&'));
|
||||
return imageUrl.scheme == 'http' || imageUrl.scheme == 'https'
|
||||
? imageUrl.toString()
|
||||
: null;
|
||||
|
||||
Reference in new issue
Block a user