Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { bucketEndpointMiddleware } from "./bucketEndpointMiddleware";

describe("bucketEndpointMiddleware", () => {
const input = { Bucket: "bucket" };
const mockRegion = "us-foo-1";
const requestInput = {
method: "GET",
headers: {},
Expand All @@ -27,10 +28,11 @@ describe("bucketEndpointMiddleware", () => {
};
const next = jest.fn();
const previouslyResolvedConfig = {
region: jest.fn().mockResolvedValue("us-foo-1"),
isCustomEndpoint: false,
region: jest.fn().mockResolvedValue(mockRegion),
regionInfoProvider: jest
.fn()
.mockResolvedValue({ hostname: "foo.us-foo-2.amazonaws.com", partition: "aws-foo", signingRegion: "us-foo-1" }),
.mockResolvedValue({ hostname: "foo.us-foo-2.amazonaws.com", partition: "aws-foo", signingRegion: mockRegion }),
useArnRegion: jest.fn().mockResolvedValue(false),
};

Expand Down Expand Up @@ -61,10 +63,12 @@ describe("bucketEndpointMiddleware", () => {
expect(param).toEqual({
bucketName: input.Bucket,
baseHostname: requestInput.hostname,
clientRegion: mockRegion,
accelerateEndpoint: false,
dualstackEndpoint: false,
pathStyleEndpoint: false,
tlsCompatible: true,
isCustomEndpoint: false,
});
});

Expand All @@ -77,6 +81,7 @@ describe("bucketEndpointMiddleware", () => {
useAccelerateEndpoint: true,
useDualstackEndpoint: true,
forcePathStyle: true,
isCustomEndpoint: true,
})
)(next, {} as any);
await handler({ input, request });
Expand All @@ -85,10 +90,12 @@ describe("bucketEndpointMiddleware", () => {
expect(param).toEqual({
bucketName: input.Bucket,
baseHostname: requestInput.hostname,
clientRegion: mockRegion,
accelerateEndpoint: true,
dualstackEndpoint: true,
pathStyleEndpoint: true,
tlsCompatible: false,
isCustomEndpoint: true,
});
});
});
Expand Down Expand Up @@ -118,13 +125,15 @@ describe("bucketEndpointMiddleware", () => {
expect(param).toEqual({
bucketName: mockBucketArn,
baseHostname: requestInput.hostname,
clientRegion: mockRegion,
accelerateEndpoint: false,
dualstackEndpoint: false,
pathStyleEndpoint: false,
tlsCompatible: true,
clientPartition: "aws-foo",
clientSigningRegion: "us-foo-1",
clientSigningRegion: mockRegion,
useArnRegion: false,
isCustomEndpoint: false,
});
expect(previouslyResolvedConfig.region).toBeCalled();
expect(previouslyResolvedConfig.regionInfoProvider).toBeCalled();
Expand All @@ -144,7 +153,7 @@ describe("bucketEndpointMiddleware", () => {
request,
});
expect(previouslyResolvedConfig.regionInfoProvider).toBeCalled();
expect(previouslyResolvedConfig.regionInfoProvider.mock.calls[0][0]).toBe("us-foo-1");
expect(previouslyResolvedConfig.regionInfoProvider.mock.calls[0][0]).toBe(mockRegion);
});

it("should supply bucketHostname in ARN object if bucket name string is a valid ARN", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export const bucketEndpointMiddleware = (options: BucketEndpointResolvedConfig):
useArnRegion,
clientPartition: partition,
clientSigningRegion: signingRegion,
clientRegion: clientRegion,
isCustomEndpoint: options.isCustomEndpoint,
});

// If the request needs to use a region or service name inferred from ARN that different from client region, we
Expand All @@ -56,13 +58,16 @@ export const bucketEndpointMiddleware = (options: BucketEndpointResolvedConfig):
request.hostname = hostname;
replaceBucketInPath = bucketEndpoint;
} else {
const clientRegion = getPseudoRegion(await options.region());
const { hostname, bucketEndpoint } = bucketHostname({
bucketName,
clientRegion,
baseHostname: request.hostname,
accelerateEndpoint: options.useAccelerateEndpoint,
dualstackEndpoint: options.useDualstackEndpoint,
pathStyleEndpoint: options.forcePathStyle,
tlsCompatible: request.protocol === "https:",
isCustomEndpoint: options.isCustomEndpoint,
});

request.hostname = hostname;
Expand Down
Loading