Skip to content

Commit

Permalink
fix coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeCheneler committed Jan 4, 2025
1 parent 7f6b81f commit 8f12286
Show file tree
Hide file tree
Showing 2 changed files with 307 additions and 22 deletions.
74 changes: 55 additions & 19 deletions src/headers/cache-control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,59 @@ export const cacheControl = (
context: MageContext,
options: CacheControlOptions,
): void => {
const value = [
`max-age=${options.maxAge}`,
`s-maxage=${options.sMaxAge}`,
options.noCache ? "no-cache" : "",
options.noStore ? "no-store" : "",
options.noTransform ? "no-transform" : "",
options.mustRevalidate ? "must-revalidate" : "",
options.proxyRevalidate ? "proxy-revalidate" : "",
options.mustUnderstand ? "must-understand" : "",
options.private ? "private" : "",
options.public ? "public" : "",
options.immutable ? "immutable" : "",
`stale-while-revalidate=${options.staleWhileRevalidate}`,
`stale-if-error=${options.staleIfError}`,
]
.filter(Boolean)
.join(", ");

context.response.headers.set("Cache-Control", value);
const values = [];

if (options.maxAge) {
values.push(`max-age=${options.maxAge}`);
}

if (options.sMaxAge) {
values.push(`s-maxage=${options.sMaxAge}`);
}

if (options.noCache) {
values.push("no-cache");
}

if (options.noStore) {
values.push("no-store");
}

if (options.noTransform) {
values.push("no-transform");
}

if (options.mustRevalidate) {
values.push("must-revalidate");
}

if (options.proxyRevalidate) {
values.push("proxy-revalidate");
}

if (options.mustUnderstand) {
values.push("must-understand");
}

if (options.private) {
values.push("private");
}

if (options.public) {
values.push("public");
}

if (options.immutable) {
values.push("immutable");
}

if (options.staleWhileRevalidate) {
values.push(`stale-while-revalidate=${options.staleWhileRevalidate}`);
}

if (options.staleIfError) {
values.push(`stale-if-error=${options.staleIfError}`);
}

context.response.headers.set("Cache-Control", values.join(", "));
};
255 changes: 252 additions & 3 deletions tests/headers/cache-control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let server: MageTestServer;
beforeAll(() => {
server = new MageTestServer();

server.app.get("/", (context) => {
server.app.get("/all", (context) => {
cacheControl(context, {
immutable: true,
maxAge: 60,
Expand All @@ -28,6 +28,110 @@ beforeAll(() => {
context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/immutable", (context) => {
cacheControl(context, {
immutable: true,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/max-age", (context) => {
cacheControl(context, {
maxAge: 60,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/must-revalidate", (context) => {
cacheControl(context, {
mustRevalidate: true,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/must-understand", (context) => {
cacheControl(context, {
mustUnderstand: true,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/no-cache", (context) => {
cacheControl(context, {
noCache: true,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/no-store", (context) => {
cacheControl(context, {
noStore: true,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/no-transform", (context) => {
cacheControl(context, {
noTransform: true,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/proxy-revalidate", (context) => {
cacheControl(context, {
proxyRevalidate: true,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/public", (context) => {
cacheControl(context, {
public: true,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/private", (context) => {
cacheControl(context, {
private: true,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/s-max-age", (context) => {
cacheControl(context, {
sMaxAge: 60,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/stale-if-error", (context) => {
cacheControl(context, {
staleIfError: 60,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.app.get("/stale-while-revalidate", (context) => {
cacheControl(context, {
staleWhileRevalidate: 60,
});

context.text(StatusCode.OK, "Hello, World!");
});

server.start();
});

Expand All @@ -36,8 +140,8 @@ afterAll(async () => {
});

describe("headers - cache-control", () => {
it("should set cache control headers", async () => {
const response = await fetch(server.url("/"), {
it("should set cache control header (all)", async () => {
const response = await fetch(server.url("/all"), {
method: "GET",
});

Expand All @@ -48,4 +152,149 @@ describe("headers - cache-control", () => {
"max-age=60, s-maxage=60, no-cache, no-store, no-transform, must-revalidate, proxy-revalidate, must-understand, private, public, immutable, stale-while-revalidate=60, stale-if-error=60",
);
});

it("should set cache control header (immutable)", async () => {
const response = await fetch(server.url("/immutable"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("immutable");
});

it("should set cache control header (max-age)", async () => {
const response = await fetch(server.url("/max-age"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("max-age=60");
});

it("should set cache control header (must-revalidate)", async () => {
const response = await fetch(server.url("/must-revalidate"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("must-revalidate");
});

it("should set cache control header (must-understand)", async () => {
const response = await fetch(server.url("/must-understand"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("must-understand");
});

it("should set cache control header (no-cache)", async () => {
const response = await fetch(server.url("/no-cache"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("no-cache");
});

it("should set cache control header (no-store)", async () => {
const response = await fetch(server.url("/no-store"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("no-store");
});

it("should set cache control header (no-transform)", async () => {
const response = await fetch(server.url("/no-transform"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("no-transform");
});

it("should set cache control header (proxy-revalidate)", async () => {
const response = await fetch(server.url("/proxy-revalidate"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("proxy-revalidate");
});

it("should set cache control header (public)", async () => {
const response = await fetch(server.url("/public"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("public");
});

it("should set cache control header (private)", async () => {
const response = await fetch(server.url("/private"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("private");
});

it("should set cache control header (s-max-age)", async () => {
const response = await fetch(server.url("/s-max-age"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("s-maxage=60");
});

it("should set cache control header (stale-if-error)", async () => {
const response = await fetch(server.url("/stale-if-error"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual("stale-if-error=60");
});

it("should set cache control header (stale-while-revalidate)", async () => {
const response = await fetch(server.url("/stale-while-revalidate"), {
method: "GET",
});

// drain response to ensure no memory leak
await response.text();

expect(response.headers.get("Cache-Control")).toEqual(
"stale-while-revalidate=60",
);
});
});

0 comments on commit 8f12286

Please sign in to comment.