Skip to content

Commit

Permalink
Add ETag tests to asset-worker (#7166)
Browse files Browse the repository at this point in the history
* Add skipIfNoneMatchHeaderHandling parameter to asset-worker handler

* Back out of asset-worker etag changes to leave just tests
  • Loading branch information
GregBrimble authored Nov 12, 2024
1 parent 96c2b77 commit ce3cccc
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions packages/workers-shared/asset-worker/tests/handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { vi } from "vitest";
import { handleRequest } from "../src/handler";
import type { AssetConfig } from "../../utils/types";

describe("[Asset Worker] `handleRequest`", () => {
it("attaches ETag headers to responses", async () => {
const configuration: Required<AssetConfig> = {
html_handling: "none",
not_found_handling: "none",
};
const eTag = "some-etag";
const exists = vi.fn().mockReturnValue(eTag);
const getByETag = vi.fn().mockReturnValue({
readableStream: new ReadableStream(),
contentType: "text/html",
});

const response = await handleRequest(
new Request("https://example.com/"),
configuration,
exists,
getByETag
);

expect(response.headers.get("ETag")).toBe(`"${eTag}"`);
});

it("returns 304 Not Modified responses for a valid strong ETag in If-None-Match", async () => {
const configuration: Required<AssetConfig> = {
html_handling: "none",
not_found_handling: "none",
};
const eTag = "some-etag";
const exists = vi.fn().mockReturnValue(eTag);
const getByETag = vi.fn().mockReturnValue({
readableStream: new ReadableStream(),
contentType: "text/html",
});

const response = await handleRequest(
new Request("https://example.com/", {
headers: { "If-None-Match": `"${eTag}"` },
}),
configuration,
exists,
getByETag
);

expect(response.status).toBe(304);
});

it("returns 304 Not Modified responses for a valid weak ETag in If-None-Match", async () => {
const configuration: Required<AssetConfig> = {
html_handling: "none",
not_found_handling: "none",
};
const eTag = "some-etag";
const exists = vi.fn().mockReturnValue(eTag);
const getByETag = vi.fn().mockReturnValue({
readableStream: new ReadableStream(),
contentType: "text/html",
});

const response = await handleRequest(
new Request("https://example.com/", {
headers: { "If-None-Match": `W/"${eTag}"` },
}),
configuration,
exists,
getByETag
);

expect(response.status).toBe(304);
});

it("returns 200 OK responses for an invalid ETag in If-None-Match", async () => {
const configuration: Required<AssetConfig> = {
html_handling: "none",
not_found_handling: "none",
};
const eTag = "some-etag";
const exists = vi.fn().mockReturnValue(eTag);
const getByETag = vi.fn().mockReturnValue({
readableStream: new ReadableStream(),
contentType: "text/html",
});

const response = await handleRequest(
new Request("https://example.com/", {
headers: { "If-None-Match": "a fake etag!" },
}),
configuration,
exists,
getByETag
);

expect(response.status).toBe(200);
});
});

0 comments on commit ce3cccc

Please sign in to comment.