Skip to content

Commit

Permalink
chore: populate requestAlgorithmMemberwhen conditions are met
Browse files Browse the repository at this point in the history
It sets input[requestAlgorithmMember] to default checksum algorithm
if it's not set and either requestChecksumCalculation is supported
or requestChecksumRequired is set to true.
  • Loading branch information
trivikr committed Nov 19, 2024
1 parent bcffb1b commit 317926c
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { setFeature } from "@aws-sdk/core";
import { afterEach, describe, expect, test as it, vi } from "vitest";

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

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

describe(flexibleChecksumsInputMiddleware.name, () => {
const mockNext = vi.fn();
const mockMiddlewareConfig = {
requestValidationModeMember: "requestValidationModeMember",
};

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

const mockConfig = {
requestChecksumCalculation: () => Promise.resolve(RequestChecksumCalculation.WHEN_SUPPORTED),
responseChecksumValidation: () => Promise.resolve(ResponseChecksumValidation.WHEN_SUPPORTED),
} as PreviouslyResolved;

Expand All @@ -21,32 +24,100 @@ describe(flexibleChecksumsInputMiddleware.name, () => {
vi.clearAllMocks();
});

describe("sets input.requestAlgorithmMember", () => {
describe("input[requestAlgorithmMember] is not defined and", () => {
const mockMiddlewareConfigWithRequestAlgorithmMember = {
...mockMiddlewareConfig,
requestAlgorithmMember: mockRequestAlgorithmMember,
};

it("requestChecksumCalculation is supported", async () => {
const handler = flexibleChecksumsInputMiddleware(mockConfig, mockMiddlewareConfigWithRequestAlgorithmMember)(
mockNext,
{}
);
await handler({ input: {} });
expect(mockNext).toHaveBeenCalledWith({ input: { [mockRequestAlgorithmMember]: DEFAULT_CHECKSUM_ALGORITHM } });
});

it("requestChecksumRequired is set to true", async () => {
const mockConfigWithReqChecksumCalculationWhenRequired = {
...mockConfig,
requestChecksumCalculation: () => Promise.resolve(RequestChecksumCalculation.WHEN_REQUIRED),
};

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

await handler({ input: {} });
expect(mockNext).toHaveBeenCalledWith({ input: { [mockRequestAlgorithmMember]: DEFAULT_CHECKSUM_ALGORITHM } });
});
});
});

describe("leaves input.requestAlgorithmMember", () => {
const mockMiddlewareConfigWithRequestAlgorithmMember = {
...mockMiddlewareConfig,
requestAlgorithmMember: mockRequestAlgorithmMember,
};

it("when input[requestAlgorithmMember] is defined", async () => {
const handler = flexibleChecksumsInputMiddleware(mockConfig, mockMiddlewareConfigWithRequestAlgorithmMember)(
mockNext,
{}
);
await handler({ input: { [mockRequestAlgorithmMember]: "SHA256" } });
expect(mockNext).toHaveBeenCalledWith({ input: { [mockRequestAlgorithmMember]: "SHA256" } });
});

it("if requestChecksumCalculation is required and requestChecksumRequired is false", async () => {
const mockConfigReqChecksumCalculationWhenRequired = {
...mockConfig,
requestChecksumCalculation: () => Promise.resolve(RequestChecksumCalculation.WHEN_REQUIRED),
} as PreviouslyResolved;

const handler = flexibleChecksumsInputMiddleware(
mockConfigReqChecksumCalculationWhenRequired,
mockMiddlewareConfigWithRequestAlgorithmMember
)(mockNext, {});
await handler({ input: {} });
expect(mockNext).toHaveBeenCalledWith({ input: {} });
});
});

describe("sets input.requestValidationModeMember", () => {
it("when requestValidationModeMember is defined and responseChecksumValidation is supported", async () => {
const handler = flexibleChecksumsInputMiddleware(mockConfig, mockMiddlewareConfig)(mockNext, {});
const mockMiddlewareConfigWithMockRequestValidationModeMember = {
...mockMiddlewareConfig,
requestValidationModeMember: mockRequestValidationModeMember,
};
const handler = flexibleChecksumsInputMiddleware(
mockConfig,
mockMiddlewareConfigWithMockRequestValidationModeMember
)(mockNext, {});
await handler({ input: {} });
expect(mockNext).toHaveBeenCalledWith({ input: { requestValidationModeMember: "ENABLED" } });
expect(mockNext).toHaveBeenCalledWith({ input: { [mockRequestValidationModeMember]: "ENABLED" } });
});
});

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

it("when requestValidationModeMember is not defined", async () => {
const mockMiddlewareConfig = {};

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

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

it("when responseChecksumValidation is required", async () => {
const mockConfig = {
const mockConfigResWhenRequired = {
...mockConfig,
responseChecksumValidation: () => Promise.resolve(ResponseChecksumValidation.WHEN_REQUIRED),
} as PreviouslyResolved;

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

expect(mockNext).toHaveBeenCalledWith(mockArgs);
Expand All @@ -55,15 +126,45 @@ describe(flexibleChecksumsInputMiddleware.name, () => {

describe("set feature", () => {
it.each([
["FLEXIBLE_CHECKSUMS_RES_WHEN_REQUIRED", "c", ResponseChecksumValidation.WHEN_REQUIRED],
["FLEXIBLE_CHECKSUMS_RES_WHEN_SUPPORTED", "b", ResponseChecksumValidation.WHEN_SUPPORTED],
])("logs %s and %s when ResponseChecksumValidation=%s", async (feature, value, responseChecksumValidation) => {
const mockConfig = {
responseChecksumValidation: () => Promise.resolve(responseChecksumValidation),
[
"FLEXIBLE_CHECKSUMS_REQ_WHEN_REQUIRED",
"a",
"requestChecksumCalculation",
RequestChecksumCalculation.WHEN_REQUIRED,
],
[
"FLEXIBLE_CHECKSUMS_REQ_WHEN_SUPPORTED",
"Z",
"requestChecksumCalculation",
RequestChecksumCalculation.WHEN_SUPPORTED,
],
[
"FLEXIBLE_CHECKSUMS_RES_WHEN_REQUIRED",
"c",
"responseChecksumValidation",
ResponseChecksumValidation.WHEN_REQUIRED,
],
[
"FLEXIBLE_CHECKSUMS_RES_WHEN_SUPPORTED",
"b",
"responseChecksumValidation",
ResponseChecksumValidation.WHEN_SUPPORTED,
],
])("logs %s:%s when %s=%s", async (feature, value, configKey, configValue) => {
const mockConfigOverride = {
...mockConfig,
[configKey]: () => Promise.resolve(configValue),
} as PreviouslyResolved;
const handler = flexibleChecksumsInputMiddleware(mockConfig, mockMiddlewareConfig)(mockNext, {});

const handler = flexibleChecksumsInputMiddleware(mockConfigOverride, mockMiddlewareConfig)(mockNext, {});
await handler({ input: {} });
expect(setFeature).toHaveBeenCalledWith(expect.anything(), feature, value);

expect(setFeature).toHaveBeenCalledTimes(2);
if (configKey === "requestChecksumCalculation") {
expect(setFeature).toHaveBeenNthCalledWith(1, expect.anything(), feature, value);
} else {
expect(setFeature).toHaveBeenNthCalledWith(2, expect.anything(), feature, value);
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@ import {
} from "@smithy/types";

import { PreviouslyResolved } from "./configuration";
import { ResponseChecksumValidation } from "./constants";
import { DEFAULT_CHECKSUM_ALGORITHM, 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 @@ -47,17 +57,44 @@ export const flexibleChecksumsInputMiddleware =
): SerializeHandler<any, Output> =>
async (args: SerializeHandlerArguments<any>): Promise<SerializeHandlerOutput<Output>> => {
const input = args.input;
const { requestValidationModeMember } = middlewareConfig;
const { requestValidationModeMember, requestAlgorithmMember, requestChecksumRequired } = middlewareConfig;

const requestChecksumCalculation = await config.requestChecksumCalculation();
const responseChecksumValidation = await config.responseChecksumValidation();

if (responseChecksumValidation === ResponseChecksumValidation.WHEN_REQUIRED) {
setFeature(context, "FLEXIBLE_CHECKSUMS_RES_WHEN_REQUIRED", "c");
} else {
setFeature(context, "FLEXIBLE_CHECKSUMS_RES_WHEN_SUPPORTED", "b");
switch (requestChecksumCalculation) {
case RequestChecksumCalculation.WHEN_REQUIRED:
setFeature(context, "FLEXIBLE_CHECKSUMS_REQ_WHEN_REQUIRED", "a");
break;
case RequestChecksumCalculation.WHEN_SUPPORTED:
setFeature(context, "FLEXIBLE_CHECKSUMS_REQ_WHEN_SUPPORTED", "Z");
break;
}

switch (responseChecksumValidation) {
case ResponseChecksumValidation.WHEN_REQUIRED:
setFeature(context, "FLEXIBLE_CHECKSUMS_RES_WHEN_REQUIRED", "c");
break;
case ResponseChecksumValidation.WHEN_SUPPORTED:
setFeature(context, "FLEXIBLE_CHECKSUMS_RES_WHEN_SUPPORTED", "b");
break;
}

// 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.
if (requestChecksumCalculation === RequestChecksumCalculation.WHEN_SUPPORTED || requestChecksumRequired) {
input[requestAlgorithmMember] = DEFAULT_CHECKSUM_ALGORITHM;
}
}

if (requestValidationModeMember && responseChecksumValidation === ResponseChecksumValidation.WHEN_SUPPORTED) {
input[requestValidationModeMember] = "ENABLED";
// The value for input member to opt-in to best-effort validation of a checksum returned in the HTTP response is not set.
if (requestValidationModeMember && !input[requestValidationModeMember]) {
// Set requestValidationModeMember as ENABLED only if response checksum validation is supported.
if (responseChecksumValidation === ResponseChecksumValidation.WHEN_SUPPORTED) {
input[requestValidationModeMember] = "ENABLED";
}
}

return next(args);
Expand Down

0 comments on commit 317926c

Please sign in to comment.