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

Cache new response if successful #1322

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add test cases for cache changes
  • Loading branch information
Ajeyakrishna-k committed Jul 24, 2023
commit da06fc2a4243fc009caf1b1dfa2c565efebeac05
83 changes: 82 additions & 1 deletion test/unit/middlewares/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("Middleware | Utils | cache", function () {
};

const response = {
statusCode: 200,
send: sinon.spy(),
};

Expand All @@ -39,14 +40,16 @@ describe("Middleware | Utils | cache", function () {
expect(response.send.callCount).to.equal(2);
});

it("should invalidate stale the response", function () {
it("should invalidate stale response", function () {
const cacheTestKey = "__cache__2";

const request = {
method: "GET",
originalUrl: "/test2",
};
const response = {
on: sinon.spy(),
statusCode: 200,
send: sinon.spy(),
};

Expand All @@ -66,6 +69,9 @@ describe("Middleware | Utils | cache", function () {
cacheMiddlewareForInvalidation(request, response, nextSpy);
response.send(responseBody);

response.on.withArgs("finish").yield();

expect(response.on.callCount).to.equal(1);
expect(nextSpy.callCount).to.equal(2);
expect(response.send.callCount).to.equal(2);

Expand All @@ -75,4 +81,79 @@ describe("Middleware | Utils | cache", function () {
expect(nextSpy.callCount).to.equal(3);
expect(response.send.callCount).to.equal(3);
});

it("should not cache the response", function () {
const cacheTestKey = "__cache__3";
const request = {
method: "GET",
originalUrl: "/test3",
};

const response = {
statusCode: 400,
send: sinon.spy(),
};

const nextSpy = sinon.spy();

const cacheMiddleware = cacheResponse({ invalidationKey: cacheTestKey });

cacheMiddleware(request, response, nextSpy);

response.send(responseBody);

expect(nextSpy.callCount).to.equal(1);
expect(response.send.callCount).to.equal(1);

cacheMiddleware(request, response, nextSpy);
response.send(responseBody);

expect(nextSpy.callCount).to.equal(2);

expect(response.send.callCount).to.equal(2);
});

it("should not invalidate stale the response if theres an error", function () {
const cacheTestKey = "__cache__4";

const request = {
method: "GET",
originalUrl: "/test4",
};
const response = {
on: sinon.spy(),
statusCode: 200,
send: sinon.spy(),
};

const nextSpy = sinon.spy();

const cacheMiddlewareForCache = cacheResponse({ invalidationKey: cacheTestKey });

cacheMiddlewareForCache(request, response, nextSpy);

response.send(responseBody);

expect(nextSpy.callCount).to.equal(1);
expect(response.send.callCount).to.equal(1);

response.statusCode = 400;

const cacheMiddlewareForInvalidation = invalidateCache({ invalidationKeys: [cacheTestKey] });

cacheMiddlewareForInvalidation(request, response, nextSpy);
response.send(responseBody);

response.on.withArgs("finish").yield();

expect(response.on.callCount).to.equal(1);
expect(nextSpy.callCount).to.equal(2);
expect(response.send.callCount).to.equal(2);

response.statusCode = 200;
cacheMiddlewareForCache(request, response, nextSpy);

expect(nextSpy.callCount).to.equal(2);
expect(response.send.callCount).to.equal(3);
});
});
Loading