Test IpfsUploadHelper directly and build the upload request once
Drop the test-only upload facade on MessageUrlImageHelper that overwrote the shared HTTP client, move its tests to IpfsUploadHelper, and build the multipart request with the final upload URI instead of cloning it.
This commit is contained in:
1 parent
7e7e7a48db
commit
c2d18b431e
4 files changed
+109
-107
No files matched your search
@@ -6,6 +6,7 @@ import 'package:http/http.dart' as http;
|
|||||||
import '../models/app_settings.dart';
|
import '../models/app_settings.dart';
|
||||||
|
|
||||||
class IpfsUploadHelper {
|
class IpfsUploadHelper {
|
||||||
|
@visibleForTesting
|
||||||
static http.Client? httpClient;
|
static http.Client? httpClient;
|
||||||
|
|
||||||
static String mimeTypeForFileName(String fileName) {
|
static String mimeTypeForFileName(String fileName) {
|
||||||
@@ -69,40 +70,37 @@ class IpfsUploadHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Uri uploadUri;
|
final Uri uploadUri;
|
||||||
final request = http.MultipartRequest('POST', endpointUri)
|
|
||||||
..files.add(
|
|
||||||
http.MultipartFile.fromBytes('file', bytes, filename: safeFileName),
|
|
||||||
);
|
|
||||||
|
|
||||||
if (isPinata) {
|
if (isPinata) {
|
||||||
if (token.isEmpty) {
|
if (token.isEmpty) {
|
||||||
throw ArgumentError('Pinata requires an API key or JWT.');
|
throw ArgumentError('Pinata requires an API key or JWT.');
|
||||||
}
|
}
|
||||||
request.headers['Authorization'] = 'Bearer $token';
|
|
||||||
request.fields['pinataOptions'] = jsonEncode({'cidVersion': 1});
|
|
||||||
request.fields['pinataMetadata'] = jsonEncode({'name': safeFileName});
|
|
||||||
uploadUri = endpointUri;
|
uploadUri = endpointUri;
|
||||||
} else {
|
} else {
|
||||||
final parameters = <String, String>{'cid-version': '1'};
|
|
||||||
if (isKubo) parameters['pin'] = 'true';
|
|
||||||
uploadUri = endpointUri.replace(
|
uploadUri = endpointUri.replace(
|
||||||
queryParameters: {...endpointUri.queryParameters, ...parameters},
|
queryParameters: {
|
||||||
|
...endpointUri.queryParameters,
|
||||||
|
'cid-version': '1',
|
||||||
|
if (isKubo) 'pin': 'true',
|
||||||
|
},
|
||||||
);
|
);
|
||||||
if (token.isNotEmpty) {
|
|
||||||
request.headers['Authorization'] = 'Bearer $token';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final finalRequest = request.url == uploadUri
|
final request = http.MultipartRequest('POST', uploadUri)
|
||||||
? request
|
..files.add(
|
||||||
: (http.MultipartRequest('POST', uploadUri)
|
http.MultipartFile.fromBytes('file', bytes, filename: safeFileName),
|
||||||
..headers.addAll(request.headers)
|
);
|
||||||
..fields.addAll(request.fields)
|
if (token.isNotEmpty) {
|
||||||
..files.addAll(request.files));
|
request.headers['Authorization'] = 'Bearer $token';
|
||||||
|
}
|
||||||
|
if (isPinata) {
|
||||||
|
request.fields['pinataOptions'] = jsonEncode({'cidVersion': 1});
|
||||||
|
request.fields['pinataMetadata'] = jsonEncode({'name': safeFileName});
|
||||||
|
}
|
||||||
|
|
||||||
final client = httpClient;
|
final client = httpClient;
|
||||||
final response = await (client == null
|
final response = await (client == null
|
||||||
? finalRequest.send()
|
? request.send()
|
||||||
: client.send(finalRequest));
|
: client.send(request));
|
||||||
final body = await response.stream.bytesToString();
|
final body = await response.stream.bytesToString();
|
||||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||||
throw Exception('IPFS upload failed (${response.statusCode}): $body');
|
throw Exception('IPFS upload failed (${response.statusCode}): $body');
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:collection';
|
import 'dart:collection';
|
||||||
import 'dart:typed_data';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:cached_network_image/cached_network_image.dart';
|
import 'package:cached_network_image/cached_network_image.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
import 'ipfs_upload_helper.dart';
|
|
||||||
import '../models/app_settings.dart';
|
import '../models/app_settings.dart';
|
||||||
|
|
||||||
/// Finds and renders image URLs embedded in ordinary text messages.
|
/// Finds and renders image URLs embedded in ordinary text messages.
|
||||||
@@ -26,41 +24,6 @@ class MessageUrlImageHelper {
|
|||||||
@visibleForTesting
|
@visibleForTesting
|
||||||
static http.Client? httpClient;
|
static http.Client? httpClient;
|
||||||
|
|
||||||
/// Compatibility facade for the dedicated IPFS upload helper.
|
|
||||||
static Future<String> uploadImageWithSettings({
|
|
||||||
required AppSettings settings,
|
|
||||||
required Uint8List bytes,
|
|
||||||
required String fileName,
|
|
||||||
required String mimeType,
|
|
||||||
}) {
|
|
||||||
IpfsUploadHelper.httpClient = httpClient;
|
|
||||||
return IpfsUploadHelper.uploadImageWithSettings(
|
|
||||||
settings: settings,
|
|
||||||
bytes: bytes,
|
|
||||||
fileName: fileName,
|
|
||||||
mimeType: mimeType,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future<String> uploadImage({
|
|
||||||
required Uint8List bytes,
|
|
||||||
required String fileName,
|
|
||||||
required String mimeType,
|
|
||||||
required String uploadEndpoint,
|
|
||||||
required String uploadApiKey,
|
|
||||||
String uploadGateway = '',
|
|
||||||
}) async {
|
|
||||||
IpfsUploadHelper.httpClient = httpClient;
|
|
||||||
return IpfsUploadHelper.uploadImage(
|
|
||||||
bytes: bytes,
|
|
||||||
fileName: fileName,
|
|
||||||
mimeType: mimeType,
|
|
||||||
uploadEndpoint: uploadEndpoint,
|
|
||||||
uploadApiKey: uploadApiKey,
|
|
||||||
uploadGateway: uploadGateway,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
static final RegExp _pyxPattern = RegExp(
|
static final RegExp _pyxPattern = RegExp(
|
||||||
r'''https?://pyx\.li/\?i=([A-Za-z0-9_-]+)''',
|
r'''https?://pyx\.li/\?i=([A-Za-z0-9_-]+)''',
|
||||||
caseSensitive: false,
|
caseSensitive: false,
|
||||||
|
|||||||
@@ -0,0 +1,89 @@
|
|||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:http/testing.dart';
|
||||||
|
import 'package:meshcore_open/helpers/ipfs_upload_helper.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('IpfsUploadHelper', () {
|
||||||
|
tearDown(() => IpfsUploadHelper.httpClient = null);
|
||||||
|
|
||||||
|
test('does not upload when the upload endpoint is empty', () async {
|
||||||
|
var requestCount = 0;
|
||||||
|
IpfsUploadHelper.httpClient = MockClient((request) async {
|
||||||
|
requestCount++;
|
||||||
|
return http.Response('{}', 200);
|
||||||
|
});
|
||||||
|
|
||||||
|
await expectLater(
|
||||||
|
IpfsUploadHelper.uploadImage(
|
||||||
|
bytes: Uint8List.fromList(<int>[1, 2, 3]),
|
||||||
|
fileName: 'image.png',
|
||||||
|
mimeType: 'image/png',
|
||||||
|
uploadEndpoint: '',
|
||||||
|
uploadApiKey: '',
|
||||||
|
),
|
||||||
|
throwsArgumentError,
|
||||||
|
);
|
||||||
|
expect(requestCount, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(
|
||||||
|
'uploads and returns an ipfs scheme when upload gateway is empty',
|
||||||
|
() async {
|
||||||
|
final requests = <http.BaseRequest>[];
|
||||||
|
IpfsUploadHelper.httpClient = MockClient((request) async {
|
||||||
|
requests.add(request);
|
||||||
|
expect(request.method, 'POST');
|
||||||
|
return http.Response(
|
||||||
|
'{"Hash":"bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly"}',
|
||||||
|
200,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
final message = await IpfsUploadHelper.uploadImage(
|
||||||
|
bytes: Uint8List.fromList(<int>[1, 2, 3]),
|
||||||
|
fileName: 'image.png',
|
||||||
|
mimeType: 'image/png',
|
||||||
|
uploadEndpoint: 'http://127.0.0.1:5001/api/v0/add',
|
||||||
|
uploadApiKey: '',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
message,
|
||||||
|
'ipfs://bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly',
|
||||||
|
);
|
||||||
|
expect(requests.first.url.path, '/api/v0/add');
|
||||||
|
expect(requests.first.url.queryParameters, {
|
||||||
|
'cid-version': '1',
|
||||||
|
'pin': 'true',
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
test('sends the Pinata request to the endpoint unchanged', () async {
|
||||||
|
late http.BaseRequest upload;
|
||||||
|
IpfsUploadHelper.httpClient = MockClient((request) async {
|
||||||
|
upload = request;
|
||||||
|
return http.Response('{"IpfsHash":"bafytest"}', 200);
|
||||||
|
});
|
||||||
|
|
||||||
|
final message = await IpfsUploadHelper.uploadImage(
|
||||||
|
bytes: Uint8List.fromList(<int>[1, 2, 3]),
|
||||||
|
fileName: 'image.png',
|
||||||
|
mimeType: 'image/png',
|
||||||
|
uploadEndpoint: 'https://api.pinata.cloud/pinning/pinFileToIPFS',
|
||||||
|
uploadApiKey: 'jwt',
|
||||||
|
uploadGateway: 'https://gateway.example.com',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
upload.url.toString(),
|
||||||
|
'https://api.pinata.cloud/pinning/pinFileToIPFS',
|
||||||
|
);
|
||||||
|
expect(upload.headers['Authorization'], 'Bearer jwt');
|
||||||
|
expect(message, 'https://gateway.example.com/ipfs/bafytest');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
import 'dart:typed_data';
|
|
||||||
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:http/testing.dart';
|
import 'package:http/testing.dart';
|
||||||
@@ -92,52 +90,6 @@ void main() {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
test('does not upload when the upload endpoint is empty', () async {
|
|
||||||
var requestCount = 0;
|
|
||||||
MessageUrlImageHelper.httpClient = MockClient((request) async {
|
|
||||||
requestCount++;
|
|
||||||
return http.Response('{}', 200);
|
|
||||||
});
|
|
||||||
|
|
||||||
await expectLater(
|
|
||||||
MessageUrlImageHelper.uploadImage(
|
|
||||||
bytes: Uint8List.fromList(<int>[1, 2, 3]),
|
|
||||||
fileName: 'image.png',
|
|
||||||
mimeType: 'image/png',
|
|
||||||
uploadEndpoint: '',
|
|
||||||
uploadApiKey: '',
|
|
||||||
),
|
|
||||||
throwsArgumentError,
|
|
||||||
);
|
|
||||||
expect(requestCount, 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
test(
|
|
||||||
'uploads and returns an ipfs scheme when upload gateway is empty',
|
|
||||||
() async {
|
|
||||||
MessageUrlImageHelper.httpClient = MockClient((request) async {
|
|
||||||
expect(request.method, 'POST');
|
|
||||||
return http.Response(
|
|
||||||
'{"Hash":"bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly"}',
|
|
||||||
200,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
final message = await MessageUrlImageHelper.uploadImage(
|
|
||||||
bytes: Uint8List.fromList(<int>[1, 2, 3]),
|
|
||||||
fileName: 'image.png',
|
|
||||||
mimeType: 'image/png',
|
|
||||||
uploadEndpoint: 'http://127.0.0.1:5001/api/v0/add',
|
|
||||||
uploadApiKey: '',
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(
|
|
||||||
message,
|
|
||||||
'ipfs://bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly',
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
test('parses http image urls', () async {
|
test('parses http image urls', () async {
|
||||||
final attachment = await MessageUrlImageHelper.parse(
|
final attachment = await MessageUrlImageHelper.parse(
|
||||||
'see this image https://example.com/picture.png?size=large additional text',
|
'see this image https://example.com/picture.png?size=large additional text',
|
||||||
|
|||||||
Reference in new issue
Block a user