-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathfile.test.ts
More file actions
515 lines (427 loc) · 18.8 KB
/
Copy pathfile.test.ts
File metadata and controls
515 lines (427 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
import fs from "fs";
import { join } from "path";
import { Readable } from "stream";
import { toBinaryUploadRequest, type Uploadable } from "../../../src/core/file/index";
describe("toBinaryUploadRequest", () => {
const TEST_FILE_PATH = join(__dirname, "..", "test-file.txt");
const TEST_FILE_SIZE = fs.statSync(TEST_FILE_PATH).size.toString();
beforeEach(() => {
jest.clearAllMocks();
});
describe("Buffer input", () => {
it("should handle Buffer with all metadata", async () => {
const buffer = Buffer.from("test data");
const input: Uploadable.WithMetadata = {
data: buffer,
filename: "test.txt",
contentType: "text/plain",
contentLength: 42,
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(buffer);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="test.txt"',
"Content-Type": "text/plain",
"Content-Length": "42",
});
});
it("should handle Buffer without metadata", async () => {
const buffer = Buffer.from("test data");
const input: Uploadable.WithMetadata = {
data: buffer,
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(buffer);
expect(result.headers).toEqual({
"Content-Length": "9", // buffer.length
});
});
it("should handle Buffer passed directly", async () => {
const buffer = Buffer.from("test data");
const result = await toBinaryUploadRequest(buffer);
expect(result.body).toBe(buffer);
expect(result.headers).toEqual({
"Content-Length": "9", // buffer.length
});
});
});
describe("ArrayBuffer input", () => {
it("should handle ArrayBuffer with metadata", async () => {
const arrayBuffer = new ArrayBuffer(10);
const input: Uploadable.WithMetadata = {
data: arrayBuffer,
filename: "data.bin",
contentType: "application/octet-stream",
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(arrayBuffer);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="data.bin"',
"Content-Type": "application/octet-stream",
"Content-Length": "10", // arrayBuffer.byteLength
});
});
it("should handle ArrayBuffer passed directly", async () => {
const arrayBuffer = new ArrayBuffer(10);
const result = await toBinaryUploadRequest(arrayBuffer);
expect(result.body).toBe(arrayBuffer);
expect(result.headers).toEqual({
"Content-Length": "10", // arrayBuffer.byteLength
});
});
});
describe("Uint8Array input", () => {
it("should handle Uint8Array with metadata", async () => {
const uint8Array = new Uint8Array([1, 2, 3, 4, 5]);
const input: Uploadable.WithMetadata = {
data: uint8Array,
filename: "bytes.bin",
contentType: "application/octet-stream",
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(uint8Array);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="bytes.bin"',
"Content-Type": "application/octet-stream",
"Content-Length": "5", // uint8Array.byteLength
});
});
it("should handle Uint8Array passed directly", async () => {
const uint8Array = new Uint8Array([1, 2, 3, 4, 5]);
const result = await toBinaryUploadRequest(uint8Array);
expect(result.body).toBe(uint8Array);
expect(result.headers).toEqual({
"Content-Length": "5", // uint8Array.byteLength
});
});
});
describe("Blob input", () => {
it("should handle Blob with metadata", async () => {
const blob = new Blob(["test content"], { type: "text/plain" });
const input: Uploadable.WithMetadata = {
data: blob,
filename: "override.txt",
contentType: "text/html", // Override blob's type
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(blob);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="override.txt"',
"Content-Type": "text/html", // Should use provided contentType
"Content-Length": "12", // blob.size
});
});
it("should handle Blob with intrinsic type", async () => {
const blob = new Blob(["test content"], { type: "application/json" });
const input: Uploadable.WithMetadata = {
data: blob,
filename: "data.json",
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(blob);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="data.json"',
"Content-Type": "application/json", // Should use blob's type
"Content-Length": "12", // blob.size
});
});
it("should handle Blob passed directly", async () => {
const blob = new Blob(["test content"], { type: "text/plain" });
const result = await toBinaryUploadRequest(blob);
expect(result.body).toBe(blob);
expect(result.headers).toEqual({
"Content-Type": "text/plain", // Should use blob's type
"Content-Length": "12", // blob.size
});
});
});
describe("File input", () => {
it("should handle File with metadata", async () => {
const file = new File(["file content"], "original.txt", { type: "text/plain" });
const input: Uploadable.WithMetadata = {
data: file,
filename: "renamed.txt",
contentType: "text/html", // Override file's type
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(file);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="renamed.txt"',
"Content-Type": "text/html", // Should use provided contentType
"Content-Length": "12", // file.size
});
});
it("should handle File with intrinsic properties", async () => {
const file = new File(["file content"], "test.json", { type: "application/json" });
const input: Uploadable.WithMetadata = {
data: file,
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(file);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="test.json"', // Should use file's name
"Content-Type": "application/json", // Should use file's type
"Content-Length": "12", // file.size
});
});
it("should handle File passed directly", async () => {
const file = new File(["file content"], "direct.txt", { type: "text/plain" });
const result = await toBinaryUploadRequest(file);
expect(result.body).toBe(file);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="direct.txt"',
"Content-Type": "text/plain",
"Content-Length": "12", // file.size
});
});
});
describe("ReadableStream input", () => {
it("should handle ReadableStream with metadata", async () => {
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode("stream data"));
controller.close();
},
});
const input: Uploadable.WithMetadata = {
data: stream,
filename: "stream.txt",
contentType: "text/plain",
contentLength: 100,
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(stream);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="stream.txt"',
"Content-Type": "text/plain",
"Content-Length": "100", // Should use provided contentLength
});
});
it("should handle ReadableStream without size", async () => {
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode("stream data"));
controller.close();
},
});
const input: Uploadable.WithMetadata = {
data: stream,
filename: "stream.txt",
contentType: "text/plain",
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(stream);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="stream.txt"',
"Content-Type": "text/plain",
// No Content-Length header since it cannot be determined from ReadableStream
});
});
it("should handle ReadableStream passed directly", async () => {
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode("stream data"));
controller.close();
},
});
const result = await toBinaryUploadRequest(stream);
expect(result.body).toBe(stream);
expect(result.headers).toEqual({
// No headers since no metadata provided and cannot be determined
});
});
});
describe("Node.js Readable stream input", () => {
it("should handle Readable stream with metadata", async () => {
const readable = new Readable({
read() {
this.push("readable data");
this.push(null);
},
});
const input: Uploadable.WithMetadata = {
data: readable,
filename: "readable.txt",
contentType: "text/plain",
contentLength: 50,
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(readable);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="readable.txt"',
"Content-Type": "text/plain",
"Content-Length": "50", // Should use provided contentLength
});
});
it("should handle Readable stream without size", async () => {
const readable = new Readable({
read() {
this.push("readable data");
this.push(null);
},
});
const input: Uploadable.WithMetadata = {
data: readable,
filename: "readable.txt",
contentType: "text/plain",
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(readable);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="readable.txt"',
"Content-Type": "text/plain",
// No Content-Length header since it cannot be determined from Readable
});
});
it("should handle Readable stream passed directly", async () => {
const readable = new Readable({
read() {
this.push("readable data");
this.push(null);
},
});
const result = await toBinaryUploadRequest(readable);
expect(result.body).toBe(readable);
expect(result.headers).toEqual({
// No headers since no metadata provided and cannot be determined
});
});
});
describe("File path input (FromPath type)", () => {
it("should handle file path with all metadata", async () => {
const input: Uploadable.FromPath = {
path: TEST_FILE_PATH,
filename: "custom.txt",
contentType: "text/html",
contentLength: 42,
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBeInstanceOf(fs.ReadStream);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="custom.txt"',
"Content-Type": "text/html",
"Content-Length": "42", // Should use provided contentLength
});
});
it("should handle file path with minimal metadata", async () => {
const input: Uploadable.FromPath = {
path: TEST_FILE_PATH,
contentType: "text/plain",
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBeInstanceOf(fs.ReadStream);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="test-file.txt"', // Should extract from path
"Content-Type": "text/plain",
"Content-Length": TEST_FILE_SIZE, // Should determine from file system (OS-agnostic)
});
});
it("should handle file path with no metadata", async () => {
const input: Uploadable.FromPath = {
path: TEST_FILE_PATH,
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBeInstanceOf(fs.ReadStream);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="test-file.txt"', // Should extract from path
"Content-Length": TEST_FILE_SIZE, // Should determine from file system (OS-agnostic)
});
});
});
describe("ArrayBufferView input", () => {
it("should handle ArrayBufferView with metadata", async () => {
const arrayBuffer = new ArrayBuffer(10);
const arrayBufferView = new Int8Array(arrayBuffer);
const input: Uploadable.WithMetadata = {
data: arrayBufferView,
filename: "view.bin",
contentType: "application/octet-stream",
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(arrayBufferView);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="view.bin"',
"Content-Type": "application/octet-stream",
"Content-Length": "10", // arrayBufferView.byteLength
});
});
it("should handle ArrayBufferView passed directly", async () => {
const arrayBuffer = new ArrayBuffer(10);
const arrayBufferView = new Int8Array(arrayBuffer);
const result = await toBinaryUploadRequest(arrayBufferView);
expect(result.body).toBe(arrayBufferView);
expect(result.headers).toEqual({
"Content-Length": "10", // arrayBufferView.byteLength
});
});
});
describe("null and undefined input", () => {
it("should throw Error for undefined in binary upload", async () => {
await expect(toBinaryUploadRequest(undefined as unknown as Uploadable)).rejects.toThrow(Error);
await expect(toBinaryUploadRequest(undefined as unknown as Uploadable)).rejects.toThrow(
/but received undefined/,
);
});
it("should throw Error for null in binary upload", async () => {
await expect(toBinaryUploadRequest(null as unknown as Uploadable)).rejects.toThrow(Error);
await expect(toBinaryUploadRequest(null as unknown as Uploadable)).rejects.toThrow(
/but received null/,
);
});
});
describe("Edge cases", () => {
it("should handle empty headers when no metadata is available", async () => {
const buffer = Buffer.from("");
const input: Uploadable.WithMetadata = {
data: buffer,
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(buffer);
expect(result.headers).toEqual({
"Content-Length": "0",
});
});
it("should handle zero contentLength", async () => {
const buffer = Buffer.from("test");
const input: Uploadable.WithMetadata = {
data: buffer,
contentLength: 0,
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(buffer);
expect(result.headers).toEqual({
"Content-Length": "0", // Should use provided 0
});
});
it("should handle null filename", async () => {
const buffer = Buffer.from("test");
const input: Uploadable.WithMetadata = {
data: buffer,
filename: undefined,
contentType: "text/plain",
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(buffer);
expect(result.headers).toEqual({
"Content-Type": "text/plain",
"Content-Length": "4",
// No Content-Disposition since filename is undefined
});
});
it("should handle null contentType", async () => {
const buffer = Buffer.from("test");
const input: Uploadable.WithMetadata = {
data: buffer,
filename: "test.txt",
contentType: undefined,
};
const result = await toBinaryUploadRequest(input);
expect(result.body).toBe(buffer);
expect(result.headers).toEqual({
"Content-Disposition": 'attachment; filename="test.txt"',
"Content-Length": "4",
// No Content-Type since contentType is undefined
});
});
});
});