|
| 1 | +import { |
| 2 | + CompleteMultipartUploadCommand, |
| 3 | + CompleteMultipartUploadCommandOutput, |
| 4 | + CreateMultipartUploadCommand, |
| 5 | + PutObjectCommand, |
| 6 | + PutObjectTaggingCommand, |
| 7 | + S3, |
| 8 | + S3Client, |
| 9 | + UploadPartCommand, |
| 10 | +} from "@aws-sdk/client-s3"; |
| 11 | +import { AbortController } from "@smithy/abort-controller"; |
| 12 | +import { EventEmitter, Readable } from "stream"; |
1 | 13 | import { afterAll, afterEach, beforeEach, describe, expect, test as it, vi } from "vitest"; |
2 | 14 |
|
| 15 | +import { Progress, Upload } from "./index"; |
| 16 | + |
3 | 17 | /* eslint-disable no-var */ |
4 | 18 | var hostname = "s3.region.amazonaws.com"; |
5 | 19 | var port: number | undefined; |
6 | 20 |
|
7 | | -import { EventEmitter, Readable } from "stream"; |
8 | | - |
9 | 21 | vi.mock("@aws-sdk/client-s3", async () => { |
10 | 22 | const sendMock = vi.fn().mockImplementation(async (x) => x); |
11 | 23 | const endpointMock = vi.fn().mockImplementation(() => ({ |
@@ -61,24 +73,20 @@ vi.mock("@aws-sdk/client-s3", async () => { |
61 | 73 | }; |
62 | 74 | }); |
63 | 75 |
|
64 | | -import { |
65 | | - CompleteMultipartUploadCommand, |
66 | | - CompleteMultipartUploadCommandOutput, |
67 | | - CreateMultipartUploadCommand, |
68 | | - PutObjectCommand, |
69 | | - PutObjectTaggingCommand, |
70 | | - S3, |
71 | | - S3Client, |
72 | | - UploadPartCommand, |
73 | | -} from "@aws-sdk/client-s3"; |
74 | | -import { AbortController } from "@smithy/abort-controller"; |
75 | | - |
76 | | -import { Progress, Upload } from "./index"; |
77 | | - |
78 | 76 | const DEFAULT_PART_SIZE = 1024 * 1024 * 5; |
79 | 77 |
|
| 78 | +type Expose = { |
| 79 | + totalBytes: number | undefined; |
| 80 | +}; |
| 81 | +type VisibleForTesting = Omit<Upload, keyof Expose> & Expose; |
| 82 | + |
80 | 83 | describe(Upload.name, () => { |
81 | | - const s3MockInstance = new S3Client(); |
| 84 | + const s3MockInstance = new S3Client({ |
| 85 | + credentials: { |
| 86 | + accessKeyId: "UNIT", |
| 87 | + secretAccessKey: "UNIT", |
| 88 | + }, |
| 89 | + }); |
82 | 90 |
|
83 | 91 | beforeEach(() => { |
84 | 92 | vi.clearAllMocks(); |
@@ -107,6 +115,43 @@ describe(Upload.name, () => { |
107 | 115 | Body: "this-is-a-sample-payload", |
108 | 116 | }; |
109 | 117 |
|
| 118 | + it("uses the input parameters for object length if provided", async () => { |
| 119 | + const falseFileStream = Object.assign(Readable.from("abcd"), { |
| 120 | + path: "/dev/null", |
| 121 | + }); |
| 122 | + let upload = new Upload({ |
| 123 | + params: { |
| 124 | + Bucket: "", |
| 125 | + Key: "", |
| 126 | + Body: falseFileStream, |
| 127 | + MpuObjectSize: 6 * 1024 * 1024, |
| 128 | + }, |
| 129 | + client: s3MockInstance, |
| 130 | + }) as unknown as VisibleForTesting; |
| 131 | + expect(upload.totalBytes).toEqual(6 * 1024 * 1024); |
| 132 | + |
| 133 | + upload = new Upload({ |
| 134 | + params: { |
| 135 | + Bucket: "", |
| 136 | + Key: "", |
| 137 | + Body: falseFileStream, |
| 138 | + ContentLength: 6 * 1024 * 1024, |
| 139 | + }, |
| 140 | + client: s3MockInstance, |
| 141 | + }) as unknown as VisibleForTesting; |
| 142 | + expect(upload.totalBytes).toEqual(6 * 1024 * 1024); |
| 143 | + |
| 144 | + upload = new Upload({ |
| 145 | + params: { |
| 146 | + Bucket: "", |
| 147 | + Key: "", |
| 148 | + Body: falseFileStream, |
| 149 | + }, |
| 150 | + client: s3MockInstance, |
| 151 | + }) as unknown as VisibleForTesting; |
| 152 | + expect(upload.totalBytes).toEqual(0); |
| 153 | + }); |
| 154 | + |
110 | 155 | it("correctly exposes the event emitter API", () => { |
111 | 156 | const upload = new Upload({ |
112 | 157 | params, |
|
0 commit comments