Skip to content

Commit

Permalink
chore: leaves input.requestAlgorithmMember if checksum header is set
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Nov 22, 2024
1 parent 88166ae commit 969be81
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { setFeature } from "@aws-sdk/core";
import { afterEach, describe, expect, test as it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";

import { PreviouslyResolved } from "./configuration";
import { DEFAULT_CHECKSUM_ALGORITHM, RequestChecksumCalculation, ResponseChecksumValidation } from "./constants";
import { flexibleChecksumsInputMiddleware } from "./flexibleChecksumsInputMiddleware";
import { hasHeaderWithPrefix } from "./hasHeaderWithPrefix";

vi.mock("@aws-sdk/core");
vi.mock("./hasHeaderWithPrefix");

describe(flexibleChecksumsInputMiddleware.name, () => {
const mockNext = vi.fn();
Expand All @@ -19,6 +21,10 @@ describe(flexibleChecksumsInputMiddleware.name, () => {
responseChecksumValidation: () => Promise.resolve(ResponseChecksumValidation.WHEN_SUPPORTED),
} as PreviouslyResolved;

beforeEach(() => {
vi.mocked(hasHeaderWithPrefix).mockReturnValue(false);
});

afterEach(() => {
expect(mockNext).toHaveBeenCalledTimes(1);
vi.clearAllMocks();
Expand All @@ -36,8 +42,11 @@ describe(flexibleChecksumsInputMiddleware.name, () => {
mockNext,
{}
);
await handler({ input: {} });
expect(mockNext).toHaveBeenCalledWith({ input: { [mockRequestAlgorithmMember]: DEFAULT_CHECKSUM_ALGORITHM } });
await handler({ input: {}, request: {} });
expect(mockNext).toHaveBeenCalledWith({
input: { [mockRequestAlgorithmMember]: DEFAULT_CHECKSUM_ALGORITHM },
request: {},
});
});

it("requestChecksumRequired is set to true", async () => {
Expand All @@ -51,8 +60,11 @@ describe(flexibleChecksumsInputMiddleware.name, () => {
requestChecksumRequired: true,
})(mockNext, {});

await handler({ input: {} });
expect(mockNext).toHaveBeenCalledWith({ input: { [mockRequestAlgorithmMember]: DEFAULT_CHECKSUM_ALGORITHM } });
await handler({ input: {}, request: {} });
expect(mockNext).toHaveBeenCalledWith({
input: { [mockRequestAlgorithmMember]: DEFAULT_CHECKSUM_ALGORITHM },
request: {},
});
});
});
});
Expand Down Expand Up @@ -82,8 +94,21 @@ describe(flexibleChecksumsInputMiddleware.name, () => {
mockConfigReqChecksumCalculationWhenRequired,
mockMiddlewareConfigWithRequestAlgorithmMember
)(mockNext, {});
await handler({ input: {} });
expect(mockNext).toHaveBeenCalledWith({ input: {} });
await handler({ input: {}, request: {} });
expect(mockNext).toHaveBeenCalledWith({ input: {}, request: {} });
});

it("if checksum header is set", async () => {
const mockArgs = { input: {}, request: { key: "value" } };
vi.mocked(hasHeaderWithPrefix).mockReturnValue(true);

const handler = flexibleChecksumsInputMiddleware(mockConfig, {
...mockMiddlewareConfigWithRequestAlgorithmMember,
requestChecksumRequired: true,
})(mockNext, {});

await handler(mockArgs);
expect(mockNext).toHaveBeenCalledWith(mockArgs);
});
});

Expand All @@ -97,13 +122,13 @@ describe(flexibleChecksumsInputMiddleware.name, () => {
mockConfig,
mockMiddlewareConfigWithMockRequestValidationModeMember
)(mockNext, {});
await handler({ input: {} });
expect(mockNext).toHaveBeenCalledWith({ input: { [mockRequestValidationModeMember]: "ENABLED" } });
await handler({ input: {}, request: {} });
expect(mockNext).toHaveBeenCalledWith({ input: { [mockRequestValidationModeMember]: "ENABLED" }, request: {} });
});
});

describe("leaves input.requestValidationModeMember", () => {
const mockArgs = { input: {} };
const mockArgs = { input: {}, request: {} };

it("when requestValidationModeMember is not defined", async () => {
const handler = flexibleChecksumsInputMiddleware(mockConfig, mockMiddlewareConfig)(mockNext, {});
Expand Down Expand Up @@ -157,7 +182,7 @@ describe(flexibleChecksumsInputMiddleware.name, () => {
} as PreviouslyResolved;

const handler = flexibleChecksumsInputMiddleware(mockConfigOverride, mockMiddlewareConfig)(mockNext, {});
await handler({ input: {} });
await handler({ input: {}, request: {} });

expect(setFeature).toHaveBeenCalledTimes(2);
if (configKey === "requestChecksumCalculation") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setFeature } from "@aws-sdk/core";
import { HttpRequest } from "@smithy/protocol-http";
import {
HandlerExecutionContext,
MetadataBearer,
Expand All @@ -11,6 +12,7 @@ import {

import { PreviouslyResolved } from "./configuration";
import { DEFAULT_CHECKSUM_ALGORITHM, RequestChecksumCalculation, ResponseChecksumValidation } from "./constants";
import { hasHeaderWithPrefix } from "./hasHeaderWithPrefix";

export interface FlexibleChecksumsInputMiddlewareConfig {
/**
Expand Down Expand Up @@ -82,10 +84,13 @@ export const flexibleChecksumsInputMiddleware =

// The value for input member to configure flexible checksum is not set.
if (requestAlgorithmMember && !input[requestAlgorithmMember]) {
// Set requestAlgorithmMember as default checksum algorithm only if request checksum calculation is supported
// or request checksum is required.
// Set requestAlgorithmMember as default checksum algorithm only if request checksum algorithm is not supported,
// and either request checksumcalculation is supported or request checksum is required.
if (requestChecksumCalculation === RequestChecksumCalculation.WHEN_SUPPORTED || requestChecksumRequired) {
input[requestAlgorithmMember] = DEFAULT_CHECKSUM_ALGORITHM;
const request = args.request as HttpRequest;
if (!request.headers || hasHeaderWithPrefix("x-amz-checksum-", request.headers)) {
input[requestAlgorithmMember] = DEFAULT_CHECKSUM_ALGORITHM;
}
}
}

Expand Down

0 comments on commit 969be81

Please sign in to comment.