feat: disable cache with proper headers when CACHE_SECONDS env is zero (#4539)

* feat: disable cache with proper headers when CACHE_SECONDS env is zero

* Update src/common/cache.js

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Ophelia Goldstein
2025-10-08 01:19:54 +03:00
committed by GitHub
co-authored by Copilot Alexandr
parent 66764616fa
commit e8e5cf86fb
2 changed files with 71 additions and 4 deletions
+37 -1
View File
@@ -1,6 +1,6 @@
// @ts-check
import { jest } from "@jest/globals";
import { beforeEach, jest } from "@jest/globals";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import api from "../api/index.js";
@@ -99,6 +99,10 @@ const faker = (query, data) => {
return { req, res };
};
beforeEach(() => {
process.env.CACHE_SECONDS = undefined;
});
afterEach(() => {
mock.reset();
});
@@ -224,6 +228,38 @@ describe("Test /api/", () => {
]);
});
it("should properly set cache using CACHE_SECONDS env variable", async () => {
process.env.CACHE_SECONDS = "10000";
const { req, res } = faker({}, data_stats);
await api(req, res);
expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
`max-age=10000, s-maxage=10000, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
],
]);
});
it("should disable cache when CACHE_SECONDS is set to 0", async () => {
process.env.CACHE_SECONDS = "0";
const { req, res } = faker({}, data_stats);
await api(req, res);
expect(res.setHeader.mock.calls).toEqual([
["Content-Type", "image/svg+xml"],
[
"Cache-Control",
"no-cache, no-store, must-revalidate, max-age=0, s-maxage=0",
],
["Pragma", "no-cache"],
["Expires", "0"],
]);
});
it("should set proper cache with clamped values", async () => {
{
let { req, res } = faker({ cache_seconds: 200_000 }, data_stats);