Skip to content

Commit

Permalink
fix(signature-v4): mock Date in SigV4 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Dec 23, 2021
1 parent cac7ad0 commit 9c7538f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
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);
});

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, ""));
});
});
});

0 comments on commit 9c7538f

Please sign in to comment.