-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ETag tests to asset-worker (#7166)
* Add skipIfNoneMatchHeaderHandling parameter to asset-worker handler * Back out of asset-worker etag changes to leave just tests
- Loading branch information
1 parent
96c2b77
commit ce3cccc
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
packages/workers-shared/asset-worker/tests/handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |