Skip to content

Commit

Permalink
fix(middleware-flexible-checksums): use object for requestAlgorithmMe…
Browse files Browse the repository at this point in the history
…mber
  • Loading branch information
trivikr committed Dec 17, 2024
1 parent 2381379 commit f2d74ac
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ vi.mock("@aws-sdk/core");

describe(flexibleChecksumsInputMiddleware.name, () => {
const mockNext = vi.fn();

const mockRequestAlgorithmMember = "mockRequestAlgorithmMember";
const mockRequestValidationModeMember = "mockRequestValidationModeMember";
const mockMiddlewareConfig = { requestChecksumRequired: false };

const mockConfig = {
requestChecksumCalculation: () => Promise.resolve(RequestChecksumCalculation.WHEN_SUPPORTED),
Expand All @@ -27,7 +24,6 @@ describe(flexibleChecksumsInputMiddleware.name, () => {
describe("sets input.requestValidationModeMember", () => {
it("when requestValidationModeMember is defined and responseChecksumValidation is supported", async () => {
const mockMiddlewareConfigWithMockRequestValidationModeMember = {
...mockMiddlewareConfig,
requestValidationModeMember: mockRequestValidationModeMember,
};
const handler = flexibleChecksumsInputMiddleware(
Expand All @@ -43,7 +39,7 @@ describe(flexibleChecksumsInputMiddleware.name, () => {
const mockArgs = { input: {} };

it("when requestValidationModeMember is not defined", async () => {
const handler = flexibleChecksumsInputMiddleware(mockConfig, mockMiddlewareConfig)(mockNext, {});
const handler = flexibleChecksumsInputMiddleware(mockConfig, {})(mockNext, {});
await handler(mockArgs);
expect(mockNext).toHaveBeenCalledWith(mockArgs);
});
Expand All @@ -54,7 +50,7 @@ describe(flexibleChecksumsInputMiddleware.name, () => {
responseChecksumValidation: () => Promise.resolve(ResponseChecksumValidation.WHEN_REQUIRED),
} as PreviouslyResolved;

const handler = flexibleChecksumsInputMiddleware(mockConfigResWhenRequired, mockMiddlewareConfig)(mockNext, {});
const handler = flexibleChecksumsInputMiddleware(mockConfigResWhenRequired, {})(mockNext, {});
await handler(mockArgs);

expect(mockNext).toHaveBeenCalledWith(mockArgs);
Expand Down Expand Up @@ -93,7 +89,7 @@ describe(flexibleChecksumsInputMiddleware.name, () => {
[configKey]: () => Promise.resolve(configValue),
} as PreviouslyResolved;

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

expect(setFeature).toHaveBeenCalledTimes(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,14 @@ import {
} from "@smithy/types";

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

export interface FlexibleChecksumsInputMiddlewareConfig {
/**
* Indicates an operation requires a checksum in its HTTP request.
*/
requestChecksumRequired: boolean;

/**
* Defines a top-level operation input member used to opt-in to best-effort validation
* of a checksum returned in the HTTP response of the operation.
*/
requestValidationModeMember?: string;

/**
* Defines a top-level operation input member that is used to configure request checksum behavior.
*/
requestAlgorithmMember?: string;
}

/**
Expand Down Expand Up @@ -57,7 +47,7 @@ export const flexibleChecksumsInputMiddleware =
): SerializeHandler<any, Output> =>
async (args: SerializeHandlerArguments<any>): Promise<SerializeHandlerOutput<Output>> => {
const input = args.input;
const { requestValidationModeMember, requestAlgorithmMember, requestChecksumRequired } = middlewareConfig;
const { requestValidationModeMember } = middlewareConfig;

const requestChecksumCalculation = await config.requestChecksumCalculation();
const responseChecksumValidation = await config.responseChecksumValidation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ describe(flexibleChecksumsMiddleware.name, () => {
describe("input[requestAlgorithmMember] is not defined and", () => {
const mockMwConfigWithReqAlgoMember = {
...mockMiddlewareConfig,
requestAlgorithmMember: mockRequestAlgorithmMember,
requestAlgorithmMemberHttpHeader: mockRequestAlgorithmMemberHttpHeader,
requestAlgorithmMember: {
name: mockRequestAlgorithmMember,
httpHeader: mockRequestAlgorithmMemberHttpHeader,
},
};

it("requestChecksumCalculation is supported", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,17 @@ export const flexibleChecksumsMiddleware =
const { request, input } = args;
const { body: requestBody, headers } = request;
const { base64Encoder, streamHasher } = config;
const { requestChecksumRequired, requestAlgorithmMember, requestAlgorithmMemberHttpHeader } = middlewareConfig;
const { requestChecksumRequired, requestAlgorithmMember } = middlewareConfig;
const requestChecksumCalculation = await config.requestChecksumCalculation();

const requestAlgorithmMemberName = requestAlgorithmMember?.name;
const requestAlgorithmMemberHttpHeader = requestAlgorithmMember?.httpHeader;
// The value for input member to configure flexible checksum is not set.
if (requestAlgorithmMember && !input[requestAlgorithmMember]) {
if (requestAlgorithmMemberName && !input[requestAlgorithmMemberName]) {
// Set requestAlgorithmMember as default checksum algorithm only if request checksum calculation is supported
// or request checksum is required.
if (requestChecksumCalculation === RequestChecksumCalculation.WHEN_SUPPORTED || requestChecksumRequired) {
input[requestAlgorithmMember] = DEFAULT_CHECKSUM_ALGORITHM;
input[requestAlgorithmMemberName] = DEFAULT_CHECKSUM_ALGORITHM;
if (requestAlgorithmMemberHttpHeader) {
headers[requestAlgorithmMemberHttpHeader] = DEFAULT_CHECKSUM_ALGORITHM;
}
Expand Down

0 comments on commit f2d74ac

Please sign in to comment.