@@ -3,19 +3,24 @@ import { r2ObjectExecute } from './R2ObjectExecute';
33import {
44 cloudflareApiRequest ,
55 cloudflareApiRequestAllItems ,
6+ cloudflareApiRequestDownload ,
67 cloudflareApiRequestRaw ,
78} from '../shared/GenericFunctions' ;
89
910jest . mock ( '../shared/GenericFunctions' , ( ) => ( {
1011 cloudflareApiRequest : jest . fn ( ) ,
1112 cloudflareApiRequestAllItems : jest . fn ( ) ,
13+ cloudflareApiRequestDownload : jest . fn ( ) ,
1214 cloudflareApiRequestRaw : jest . fn ( ) ,
1315} ) ) ;
1416
1517const mockCloudflareApiRequest = cloudflareApiRequest as jest . MockedFunction < typeof cloudflareApiRequest > ;
1618const mockCloudflareApiRequestAllItems = cloudflareApiRequestAllItems as jest . MockedFunction <
1719 typeof cloudflareApiRequestAllItems
1820> ;
21+ const mockCloudflareApiRequestDownload = cloudflareApiRequestDownload as jest . MockedFunction <
22+ typeof cloudflareApiRequestDownload
23+ > ;
1924const mockCloudflareApiRequestRaw = cloudflareApiRequestRaw as jest . MockedFunction <
2025 typeof cloudflareApiRequestRaw
2126> ;
@@ -30,6 +35,11 @@ function createExecuteContext(
3035 helpers : {
3136 getBinaryDataBuffer : jest . fn ( async ( ) => binaryBuffer ) ,
3237 assertBinaryData : jest . fn ( ( ) => binaryData ) ,
38+ prepareBinaryData : jest . fn ( async ( data : Buffer , fileName : string , mimeType ?: string ) => ( {
39+ data : data . toString ( 'base64' ) ,
40+ fileName,
41+ mimeType : mimeType ?? 'application/octet-stream' ,
42+ } ) ) ,
3343 } ,
3444 } as unknown as IExecuteFunctions ;
3545}
@@ -190,3 +200,89 @@ describe('r2ObjectExecute upload', () => {
190200 ] ) ;
191201 } ) ;
192202} ) ;
203+
204+ describe ( 'r2ObjectExecute get (download)' , ( ) => {
205+ beforeEach ( ( ) => {
206+ jest . clearAllMocks ( ) ;
207+ } ) ;
208+
209+ it ( 'downloads object content and returns it as text by default' , async ( ) => {
210+ mockCloudflareApiRequestDownload . mockResolvedValueOnce ( {
211+ body : Buffer . from ( '# Hello\nWorld' , 'utf-8' ) ,
212+ contentType : 'text/markdown' ,
213+ } ) ;
214+
215+ const context = createExecuteContext ( {
216+ operation : 'get' ,
217+ accountId : 'acc123' ,
218+ bucketName : 'my-bucket' ,
219+ objectKey : 'docs/readme.md' ,
220+ responseFormat : 'text' ,
221+ } ) ;
222+
223+ const result = await r2ObjectExecute . call ( context , 0 ) ;
224+
225+ expect ( mockCloudflareApiRequestDownload ) . toHaveBeenCalledWith (
226+ 'GET' ,
227+ '/accounts/acc123/r2/buckets/my-bucket/objects/docs%2Freadme.md' ,
228+ { } ,
229+ 0 ,
230+ ) ;
231+ expect ( mockCloudflareApiRequest ) . not . toHaveBeenCalled ( ) ;
232+ expect ( result ) . toEqual ( [
233+ {
234+ json : {
235+ success : true ,
236+ key : 'docs/readme.md' ,
237+ content : '# Hello\nWorld' ,
238+ contentType : 'text/markdown' ,
239+ size : Buffer . from ( '# Hello\nWorld' , 'utf-8' ) . length ,
240+ } ,
241+ pairedItem : { item : 0 } ,
242+ } ,
243+ ] ) ;
244+ } ) ;
245+
246+ it ( 'returns object content as a binary property when requested' , async ( ) => {
247+ const fileBytes = Buffer . from ( [ 0 , 1 , 2 , 3 , 4 ] ) ;
248+ mockCloudflareApiRequestDownload . mockResolvedValueOnce ( {
249+ body : fileBytes ,
250+ contentType : 'application/octet-stream' ,
251+ } ) ;
252+
253+ const context = createExecuteContext ( {
254+ operation : 'get' ,
255+ accountId : 'acc123' ,
256+ bucketName : 'my-bucket' ,
257+ objectKey : 'data/blob.bin' ,
258+ responseFormat : 'binary' ,
259+ binaryPropertyName : 'data' ,
260+ } ) ;
261+
262+ const result = await r2ObjectExecute . call ( context , 0 ) ;
263+
264+ expect ( context . helpers . prepareBinaryData ) . toHaveBeenCalledWith (
265+ fileBytes ,
266+ 'data/blob.bin' ,
267+ 'application/octet-stream' ,
268+ ) ;
269+ expect ( result ) . toEqual ( [
270+ {
271+ json : {
272+ success : true ,
273+ key : 'data/blob.bin' ,
274+ contentType : 'application/octet-stream' ,
275+ size : fileBytes . length ,
276+ } ,
277+ binary : {
278+ data : {
279+ data : fileBytes . toString ( 'base64' ) ,
280+ fileName : 'data/blob.bin' ,
281+ mimeType : 'application/octet-stream' ,
282+ } ,
283+ } ,
284+ pairedItem : { item : 0 } ,
285+ } ,
286+ ] ) ;
287+ } ) ;
288+ } ) ;
0 commit comments