@@ -21,13 +21,14 @@ const putObjectTaggingMock = jest.fn().mockResolvedValue({
21
21
Success : "Tags have been applied!" ,
22
22
} ) ;
23
23
24
- const endpointMock = jest . fn ( ) . mockResolvedValue ( {
25
- hostname : "s3.region.amazonaws.com" ,
24
+ let hostname = "s3.region.amazonaws.com" ;
25
+ const endpointMock = jest . fn ( ) . mockImplementation ( ( ) => ( {
26
+ hostname,
26
27
port : undefined ,
27
28
protocol : "https:" ,
28
29
path : "/" ,
29
30
query : undefined ,
30
- } ) ;
31
+ } ) ) ;
31
32
32
33
import { EventEmitter , Readable } from "stream" ;
33
34
@@ -64,11 +65,11 @@ jest.mock("@aws-sdk/client-s3", () => ({
64
65
PutObjectCommand : putObjectMock ,
65
66
} ) ) ;
66
67
68
+ import { AbortController } from "@aws-sdk/abort-controller" ;
67
69
import { CompleteMultipartUploadCommandOutput , S3 , S3Client } from "@aws-sdk/client-s3" ;
68
70
import { createHash } from "crypto" ;
69
71
70
72
import { Progress , Upload } from "./index" ;
71
- import { AbortController } from "@aws-sdk/abort-controller" ;
72
73
73
74
const DEFAULT_PART_SIZE = 1024 * 1024 * 5 ;
74
75
@@ -245,6 +246,42 @@ describe(Upload.name, () => {
245
246
expect ( result . Location ) . toEqual ( "https://example-bucket.s3.region.amazonaws.com/example-key" ) ;
246
247
} ) ;
247
248
249
+ describe ( "bucket hostname deduplication" , ( ) => {
250
+ afterEach ( ( ) => {
251
+ hostname = "s3.region.amazonaws.com" ;
252
+ } ) ;
253
+ it ( "should dedupe bucket in endpoint hostname when forcePathStyle = false" , async ( ) => {
254
+ hostname = "example-bucket.example-host.com" ;
255
+ const buffer = Buffer . from ( "" ) ;
256
+ const actionParams = { ...params , Body : buffer } ;
257
+ const upload = new Upload ( {
258
+ params : actionParams ,
259
+ client : new S3 ( {
260
+ forcePathStyle : false ,
261
+ } ) ,
262
+ } ) ;
263
+ const result = ( await upload . done ( ) ) as CompleteMultipartUploadCommandOutput ;
264
+ expect ( result . Key ) . toEqual ( "example-key" ) ;
265
+ expect ( result . Bucket ) . toEqual ( "example-bucket" ) ;
266
+ expect ( result . Location ) . toEqual ( "https://example-bucket.example-host.com/example-key" ) ;
267
+ } ) ;
268
+ it ( "should prepend bucket in endpoint hostname when it does not already contain it and forcePathStyle = false" , async ( ) => {
269
+ hostname = "example-host.com" ;
270
+ const buffer = Buffer . from ( "" ) ;
271
+ const actionParams = { ...params , Body : buffer } ;
272
+ const upload = new Upload ( {
273
+ params : actionParams ,
274
+ client : new S3 ( {
275
+ forcePathStyle : false ,
276
+ } ) ,
277
+ } ) ;
278
+ const result = ( await upload . done ( ) ) as CompleteMultipartUploadCommandOutput ;
279
+ expect ( result . Key ) . toEqual ( "example-key" ) ;
280
+ expect ( result . Bucket ) . toEqual ( "example-bucket" ) ;
281
+ expect ( result . Location ) . toEqual ( "https://example-bucket.example-host.com/example-key" ) ;
282
+ } ) ;
283
+ } ) ;
284
+
248
285
it ( "should return a Location field formatted in path style when forcePathStyle is true" , async ( ) => {
249
286
const buffer = Buffer . from ( "" ) ;
250
287
const actionParams = { ...params , Body : buffer } ;
0 commit comments