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
@@ -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:http/http.dart' as http;
|
||||
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 {
|
||||
final attachment = await MessageUrlImageHelper.parse(
|
||||
'see this image https://example.com/picture.png?size=large additional text',
|
||||
|
||||
Reference in new issue
Block a user