Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(signature-v4): mock Date in SigV4 tests #2880

Merged
merged 2 commits into from Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/signature-v4/package.json
Expand Up @@ -11,7 +11,6 @@
"build:es": "tsc -p tsconfig.es.json",
"build:types": "tsc -p tsconfig.types.json",
"downlevel-dts": "downlevel-dts dist-types dist-types/ts3.4",
"pretest": "yarn build",
"test": "jest --coverage"
},
"author": {
Expand Down
15 changes: 10 additions & 5 deletions packages/signature-v4/src/SignatureV4.spec.ts
Expand Up @@ -709,21 +709,26 @@ describe("SignatureV4", () => {
});

describe("ambient Date usage", () => {
const knownDate = new Date("1999-12-31T23:59:59.999Z");
let dateSpy;
const mockDate = new Date();

beforeEach(() => {
Date.now = jest.fn().mockReturnValue(knownDate) as any;
dateSpy = jest.spyOn(global, "Date").mockImplementation(() => mockDate as unknown as string);
});

afterEach(() => {
expect(dateSpy).toHaveBeenCalledTimes(1);
jest.clearAllMocks();
});

it("should use the current date for presigning if no signing date was supplied", async () => {
const date = new Date();
const { query } = await signer.presign(minimalRequest);
expect((query as any)[AMZ_DATE_QUERY_PARAM]).toBe(iso8601(date).replace(/[\-:]/g, ""));
expect((query as any)[AMZ_DATE_QUERY_PARAM]).toBe(iso8601(mockDate).replace(/[\-:]/g, ""));
});

it("should use the current date for signing if no signing date supplied", async () => {
const { headers } = await signer.sign(minimalRequest);
expect(headers[AMZ_DATE_HEADER]).toBe(iso8601(new Date()).replace(/[\-:]/g, ""));
expect(headers[AMZ_DATE_HEADER]).toBe(iso8601(mockDate).replace(/[\-:]/g, ""));
});
});
});