-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45aefc6
commit 827b4fc
Showing
12 changed files
with
463 additions
and
50 deletions.
There are no files selected for viewing
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
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
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
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
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
Empty file.
143 changes: 143 additions & 0 deletions
143
packages/workers-shared/asset-server-worker/tests/headers.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,143 @@ | ||
import { getAdditionalHeaders, getMergedHeaders } from "../src/utils/headers"; | ||
import type { AssetMetadata } from "../src/utils/kv"; | ||
|
||
describe("[Asset Worker] Response Headers", () => { | ||
describe("getMergedHeaders()", () => { | ||
it("should merge headers with override", () => { | ||
const existingHeaders = new Headers({ | ||
"Accept-Encoding": "gzip", | ||
"Cache-Control": "max-age=180, public", | ||
"Content-Type": "text/html; charset=utf-8", | ||
}); | ||
|
||
const additionalHeaders = new Headers({ | ||
"Accept-Encoding": "*", | ||
"Content-Type": "text/javascript; charset=utf-8", | ||
"Keep-Alive": "timeout=5, max=1000", | ||
}); | ||
|
||
const mergedHeaders = getMergedHeaders( | ||
existingHeaders, | ||
additionalHeaders | ||
); | ||
expect(mergedHeaders).toEqual( | ||
new Headers({ | ||
"Accept-Encoding": "*", | ||
"Cache-Control": "max-age=180, public", | ||
"Content-Type": "text/javascript; charset=utf-8", | ||
"Keep-Alive": "timeout=5, max=1000", | ||
}) | ||
); | ||
}); | ||
}); | ||
|
||
describe("getAdditionalHeaders()", () => { | ||
it("should return the default headers the Asset Worker should set on every response", () => { | ||
const request = new Request("https://example.com", { | ||
method: "GET", | ||
headers: { | ||
"Accept-Encoding": "*", | ||
}, | ||
}); | ||
const assetMetadata: AssetMetadata = { | ||
contentType: "text/html; charset=utf-8", | ||
}; | ||
const additionalHeaders = getAdditionalHeaders( | ||
"33a64df551425fcc55e4d42a148795d9f25f89d4", | ||
assetMetadata, | ||
request | ||
); | ||
|
||
expect(additionalHeaders).toEqual( | ||
new Headers({ | ||
"Access-Control-Allow-Origin": "*", | ||
"Content-Type": "text/html; charset=utf-8", | ||
"Referrer-Policy": "strict-origin-when-cross-origin", | ||
"X-Content-Type-Options": "nosniff", | ||
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4", | ||
"Cache-Control": "public, max-age=0, must-revalidate", | ||
}) | ||
); | ||
}); | ||
|
||
it("should default 'Content-Type' to 'application/octet-stream' if not specified by asset metadata", () => { | ||
const request = new Request("https://example.com", { | ||
method: "GET", | ||
headers: { | ||
"Accept-Encoding": "*", | ||
}, | ||
}); | ||
const additionalHeaders = getAdditionalHeaders( | ||
"33a64df551425fcc55e4d42a148795d9f25f89d4", | ||
null, | ||
request | ||
); | ||
|
||
expect(additionalHeaders).toEqual( | ||
new Headers({ | ||
"Access-Control-Allow-Origin": "*", | ||
"Content-Type": "application/octet-stream", | ||
"Referrer-Policy": "strict-origin-when-cross-origin", | ||
"X-Content-Type-Options": "nosniff", | ||
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4", | ||
"Cache-Control": "public, max-age=0, must-revalidate", | ||
}) | ||
); | ||
}); | ||
|
||
it("should set the 'charset' to 'utf-8' when appropriate, if not specified", () => { | ||
const request = new Request("https://example.com", { | ||
method: "GET", | ||
headers: { | ||
"Accept-Encoding": "*", | ||
}, | ||
}); | ||
const assetMetadata: AssetMetadata = { contentType: "text/html" }; | ||
const additionalHeaders = getAdditionalHeaders( | ||
"33a64df551425fcc55e4d42a148795d9f25f89d4", | ||
assetMetadata, | ||
request | ||
); | ||
|
||
expect(additionalHeaders).toEqual( | ||
new Headers({ | ||
"Access-Control-Allow-Origin": "*", | ||
"Content-Type": "text/html; charset=utf-8", | ||
"Referrer-Policy": "strict-origin-when-cross-origin", | ||
"X-Content-Type-Options": "nosniff", | ||
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4", | ||
"Cache-Control": "public, max-age=0, must-revalidate", | ||
}) | ||
); | ||
}); | ||
|
||
it("should not set the 'Cache-Control' header, if 'Authorization' and 'Range' headers are present in the request", () => { | ||
const request = new Request("https://example.com", { | ||
method: "GET", | ||
headers: { | ||
"Accept-Encoding": "*", | ||
Authorization: "Basic 123", | ||
Range: "bytes=0-499", | ||
}, | ||
}); | ||
const assetMetadata: AssetMetadata = { | ||
contentType: "text/html; charset=utf-8", | ||
}; | ||
const additionalHeaders = getAdditionalHeaders( | ||
"33a64df551425fcc55e4d42a148795d9f25f89d4", | ||
assetMetadata, | ||
request | ||
); | ||
|
||
expect(additionalHeaders).toEqual( | ||
new Headers({ | ||
"Access-Control-Allow-Origin": "*", | ||
"Content-Type": "text/html; charset=utf-8", | ||
"Referrer-Policy": "strict-origin-when-cross-origin", | ||
"X-Content-Type-Options": "nosniff", | ||
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4", | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
75 changes: 75 additions & 0 deletions
75
packages/workers-shared/asset-server-worker/tests/kv.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,75 @@ | ||
import { getAssetWithMetadataFromKV } from "../src/utils/kv"; | ||
import type { AssetMetadata } from "../src/utils/kv"; | ||
import type { MockInstance } from "vitest"; | ||
|
||
describe("[Asset Worker] Fetching assets from KV", () => { | ||
describe("getAssetWithMetadataFromKV()", () => { | ||
let mockKVNamespace: KVNamespace; | ||
let spy: MockInstance; | ||
|
||
beforeEach(() => { | ||
mockKVNamespace = { | ||
getWithMetadata: () => Promise.resolve(), | ||
} as unknown as KVNamespace; | ||
|
||
spy = vi.spyOn(mockKVNamespace, "getWithMetadata"); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it("should return the asset value and metadata, if asset was found in the KV store", async () => { | ||
spy.mockReturnValueOnce( | ||
Promise.resolve({ | ||
value: "<html>Hello world</html>", | ||
metadata: { | ||
contentType: "text/html", | ||
}, | ||
}) as unknown as Promise< | ||
KVNamespaceGetWithMetadataResult<ReadableStream, AssetMetadata> | ||
> | ||
); | ||
|
||
const asset = await getAssetWithMetadataFromKV(mockKVNamespace, "abcd"); | ||
expect(asset).toBeDefined(); | ||
expect(asset?.value).toEqual("<html>Hello world</html>"); | ||
expect(asset?.metadata).toEqual({ | ||
contentType: "text/html", | ||
}); | ||
expect(spy).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
it("should throw an error if something went wrong while fetching the asset", async () => { | ||
spy.mockReturnValue(Promise.reject("Oeps! Something went wrong")); | ||
|
||
await expect(() => | ||
getAssetWithMetadataFromKV(mockKVNamespace, "abcd") | ||
).rejects.toThrowError( | ||
"Requested asset abcd could not be fetched from KV namespace." | ||
); | ||
}); | ||
|
||
it("should retry once by default if something went wrong while fetching the asset", async () => { | ||
spy.mockReturnValue(Promise.reject("Oeps! Something went wrong")); | ||
|
||
await expect(() => | ||
getAssetWithMetadataFromKV(mockKVNamespace, "abcd") | ||
).rejects.toThrowError( | ||
"Requested asset abcd could not be fetched from KV namespace." | ||
); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("should support custom number of retries", async () => { | ||
spy.mockReturnValue(Promise.reject("Oeps! Something went wrong")); | ||
|
||
await expect(() => | ||
getAssetWithMetadataFromKV(mockKVNamespace, "abcd", 2) | ||
).rejects.toThrowError( | ||
"Requested asset abcd could not be fetched from KV namespace." | ||
); | ||
expect(spy).toHaveBeenCalledTimes(3); | ||
}); | ||
}); | ||
}); |
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
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
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,12 @@ | ||
import { defineProject, mergeConfig } from "vitest/config"; | ||
import configShared from "../../vitest.shared"; | ||
|
||
export default mergeConfig( | ||
configShared, | ||
defineProject({ | ||
test: { | ||
include: ["asset-server-worker/tests/**.{test,spec}.{ts,js}"], | ||
globals: true, | ||
}, | ||
}) | ||
); |
Oops, something went wrong.