Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { contentHash } from './kv.ts'
4import { cachedJson, cachedText, ifNoneMatchSatisfied } from './http-cache.ts'
5
6const FETCHED = 1_781_150_000
7const STALE = FETCHED + 600
8
9const request = (headers: Record<string, string> = {}) =>
10 new Request('https://example.com/v1/schemas/anthropic', { headers })
11
12describe('cachedJson', () => {
13 it('serves 200 with ETag, Last-Modified, Cache-Control, and X- headers', async () => {
14 const value = { hello: 'world' }
15 const response = await cachedJson(request(), value, {
16 fetchedAt: FETCHED,
17 staleAt: STALE,
18 })
19 expect(response.status).toBe(200)
20 expect(await response.json()).toEqual(value)
21
22 const expectedTag = await contentHash(value)
23 expect(response.headers.get('etag')).toBe(`"${expectedTag}"`)
24 expect(response.headers.get('last-modified')).toBe(
25 new Date(FETCHED * 1000).toUTCString(),
26 )
27 expect(response.headers.get('cache-control')).toBe(
28 'public, max-age=60, stale-while-revalidate=600',
29 )
30 expect(response.headers.get('x-fetched-at')).toBe(String(FETCHED))
31 expect(response.headers.get('x-stale-at')).toBe(String(STALE))
32 })
33
34 it('returns 304 with no body on a matching If-None-Match', async () => {
35 const value = { hello: 'world' }
36 const etag = await contentHash(value)
37 const response = await cachedJson(
38 request({ 'if-none-match': `"${etag}"` }),
39 value,
40 { fetchedAt: FETCHED, staleAt: STALE },
41 )
42 expect(response.status).toBe(304)
43 expect(await response.text()).toBe('')
44 expect(response.headers.get('etag')).toBe(`"${etag}"`)
45 expect(response.headers.get('x-stale-at')).toBe(String(STALE))
46 })
47
48 it('returns 200 on a mismatched If-None-Match', async () => {
49 const response = await cachedJson(
50 request({ 'if-none-match': '"deadbeef"' }),
51 { hello: 'world' },
52 { fetchedAt: FETCHED, staleAt: STALE },
53 )
54 expect(response.status).toBe(200)
55 })
56
57 it('uses a provided etag (e.g. the stored schema content hash) verbatim', async () => {
58 const response = await cachedJson(
59 request(),
60 { any: 'body' },
61 { etag: 'a'.repeat(64), fetchedAt: FETCHED, staleAt: STALE },
62 )
63 expect(response.headers.get('etag')).toBe(`"${'a'.repeat(64)}"`)
64 })
65
66 it('honours custom max-age and stale-while-revalidate', async () => {
67 const response = await cachedJson(
68 request(),
69 {},
70 {
71 fetchedAt: FETCHED,
72 staleAt: STALE,
73 maxAge: 5,
74 staleWhileRevalidate: 30,
75 },
76 )
77 expect(response.headers.get('cache-control')).toBe(
78 'public, max-age=5, stale-while-revalidate=30',
79 )
80 })
81})
82
83describe('cachedText', () => {
84 it('serves a non-JSON body with the given content type and full cache headers', () => {
85 const response = cachedText(
86 request(),
87 'export type X = string\n',
88 'text/typescript; charset=utf-8',
89 { etag: 'hash-types-v1-exact', fetchedAt: FETCHED, staleAt: STALE },
90 )
91 expect(response.status).toBe(200)
92 expect(response.headers.get('content-type')).toBe(
93 'text/typescript; charset=utf-8',
94 )
95 expect(response.headers.get('etag')).toBe('"hash-types-v1-exact"')
96 expect(response.headers.get('x-stale-at')).toBe(String(STALE))
97 })
98
99 it('returns 304 with no body on a matching If-None-Match', async () => {
100 const response = cachedText(
101 request({ 'if-none-match': '"hash-types-v1-exact"' }),
102 'export type X = string\n',
103 'text/typescript; charset=utf-8',
104 { etag: 'hash-types-v1-exact', fetchedAt: FETCHED, staleAt: STALE },
105 )
106 expect(response.status).toBe(304)
107 expect(await response.text()).toBe('')
108 })
109})
110
111describe('ifNoneMatchSatisfied', () => {
112 it('handles lists, weak validators, and the wildcard', () => {
113 expect(ifNoneMatchSatisfied('"abc", "def"', 'def')).toBe(true)
114 expect(ifNoneMatchSatisfied('W/"abc"', 'abc')).toBe(true)
115 expect(ifNoneMatchSatisfied('*', 'anything')).toBe(true)
116 expect(ifNoneMatchSatisfied('"abc"', 'def')).toBe(false)
117 expect(ifNoneMatchSatisfied(null, 'abc')).toBe(false)
118 })
119})