Skip to content
Myr

Merge commit from fork

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(cache): skip responses with vary header * fix(cache): bypass cache for authorization requests * fix(cache): limit cache middleware to get requests

authored by Taku Amano · committed by GitHub · parent ff2b3d3 · Browse files · 2 files+83 added−74 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+83 added−74 removed

  • tests:+77 added−72 removed
  • other:+6 added−2 removed

2 files+83 added−74 removed

modified:src/middleware/cache/index.test.ts+77 added-72 removed
168 unmodified lines
169 c.header('Vary', '*')170 return c.text('cached')171 })172173 let varyWildcardOnlyCount = 0174 app.use('/vary-wildcard/*', cache({ cacheName: 'vary-wildcard-test', wait: true }))175 app.get('/vary-wildcard/only', (c) => {176 varyWildcardOnlyCount++177 c.header('X-Count', `${varyWildcardOnlyCount}`)178 c.header('Vary', '*')179 return c.text('response')180 })181182 let varyWildcardFirstCount = 0183 app.get('/vary-wildcard/first', (c) => {184 varyWildcardFirstCount++185 c.header('X-Count', `${varyWildcardFirstCount}`)186 c.header('Vary', '*, Accept')187 return c.text('response')188 })189190 let varyWildcardMiddleCount = 0191 app.get('/vary-wildcard/middle', (c) => {192 varyWildcardMiddleCount++193 c.header('X-Count', `${varyWildcardMiddleCount}`)194 c.header('Vary', 'Accept, *')195 return c.text('response')196 })197198 let varyWildcardComplexCount = 0199 app.get('/vary-wildcard/complex', (c) => {200 varyWildcardComplexCount++201 c.header('X-Count', `${varyWildcardComplexCount}`)202 c.header('Vary', 'Accept, *, Accept-Encoding')203 return c.text('response')204 })205206 let varyWildcardSpacesCount = 0207 app.get('/vary-wildcard/spaces', (c) => {208 varyWildcardSpacesCount++209 c.header('X-Count', `${varyWildcardSpacesCount}`)210 c.header('Vary', ' * ')211 return c.text('response')212 })213173 app.use('/default/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' }))174 app.all('/default/:code/', (c) => {175 const code = parseInt(c.req.param('code'))176 // Intended to avoid the following error: `RangeError: init[“status”] must be in the range of 200 to 599, inclusive.`
114 unmodified lines
291 expect(() => cache({ cacheName: 'my-app-v1', wait: true, vary: ['*'] })).toThrow()292 expect(() => cache({ cacheName: 'my-app-v1', wait: true, vary: '*' })).toThrow()293 })294336 it('Should not cache response when Vary: * is set', async () => {337 await app.request('http://localhost/vary-wildcard/only')338 const res = await app.request('http://localhost/vary-wildcard/only')339 expect(res.headers.get('x-count')).toBe('2')340 })341342 it('Should not cache response when Vary: *, Accept is set', async () => {343 await app.request('http://localhost/vary-wildcard/first')344 const res = await app.request('http://localhost/vary-wildcard/first')345 expect(res.headers.get('x-count')).toBe('2')346 })347348 it('Should not cache response when Vary: Accept, * is set', async () => {349 await app.request('http://localhost/vary-wildcard/middle')350 const res = await app.request('http://localhost/vary-wildcard/middle')351 expect(res.headers.get('x-count')).toBe('2')352 })353354 it('Should not cache response when Vary: Accept, *, Accept-Encoding is set', async () => {355 await app.request('http://localhost/vary-wildcard/complex')356 const res = await app.request('http://localhost/vary-wildcard/complex')357 expect(res.headers.get('x-count')).toBe('2')358 })359360 it('Should not cache response when Vary contains * with extra spaces', async () => {361 await app.request('http://localhost/vary-wildcard/spaces')362 const res = await app.request('http://localhost/vary-wildcard/spaces')363 expect(res.headers.get('x-count')).toBe('2')364 })365295 it.each([200])('Should cache %i in default cacheable status codes', async (code) => {296 await app.request(`http://localhost/default/${code}/`)297 const res = await app.request(`http://localhost/default/${code}/`)298 expect(res).not.toBeNull()
80 unmodified lines
379})380381describe('Cache Skipping Logic', () => {382 let putSpy: ReturnType<typeof vi.fn>383 let matchSpy: ReturnType<typeof vi.fn>384385 beforeEach(() => {386 matchSpy = vi.fn().mockResolvedValue(undefined)387 putSpy = vi.fn()388 const mockCache = {458 match: vi.fn().mockResolvedValue(undefined), // Always miss389 match: matchSpy, // Always miss unless a test overrides it390 put: putSpy, // We spy on this391 keys: vi.fn().mockResolvedValue([]),392 }393
66 unmodified lines
460 await app.request('/')461 // IMPORTANT: put() SHOULD be called for normal responses462 expect(putSpy).toHaveBeenCalled()463 })464465 it.each(['Accept-Language', 'Authorization', 'X-Tenant'])(466 'Should NOT cache response if Vary: %s is set',467 async (vary) => {468 const app = new Hono()469 app.use('*', cache({ cacheName: 'skip-test', wait: true }))470 app.get('/', (c) => {471 c.header('Vary', vary)472 return c.text('response')473 })474475 await app.request('/')476 expect(putSpy).not.toHaveBeenCalled()477 }478 )479480 it('Should set configured Vary header but not cache the response', async () => {481 const app = new Hono()482 let count = 0483 app.use('*', cache({ cacheName: 'skip-test', wait: true, vary: 'Accept' }))484 app.get('/', (c) => {485 count++486 c.header('X-Count', `${count}`)487 return c.text('response')488 })489490 await app.request('/')491 const res = await app.request('/')492 expect(res.headers.get('Vary')).toBe('accept')493 expect(res.headers.get('X-Count')).toBe('2')494 expect(putSpy).not.toHaveBeenCalled()495 })496497 it('Should bypass cache for Authorization requests', async () => {498 const app = new Hono()499 app.use('*', cache({ cacheName: 'skip-test', wait: true, cacheControl: 'max-age=10' }))500 app.get('/', (c) => {501 return c.text('fresh')502 })503504 const res = await app.request('/', {505 headers: {506 Authorization: 'Bearer token',507 },508 })509 expect(await res.text()).toBe('fresh')510 expect(caches.open).not.toHaveBeenCalled()511 expect(putSpy).not.toHaveBeenCalled()512 })513514 it('Should not use or store cache for POST requests', async () => {515 const app = new Hono()516 let postCount = 0517 app.use('*', cache({ cacheName: 'skip-test', wait: true, cacheControl: 'max-age=10' }))518 app.get('/resource', (c) => {519 return c.text('get response')520 })521 app.post('/resource', (c) => {522 postCount++523 c.header('X-Count', `${postCount}`)524 return c.text('post response')525 })526527 await app.request('/resource')528 putSpy.mockClear()529 matchSpy.mockClear()530531 await app.request('/resource', { method: 'POST' })532 const res = await app.request('/resource', { method: 'POST' })533 expect(res.headers.get('X-Count')).toBe('2')534 expect(res.headers.get('Cache-Control')).toBeNull()535 expect(matchSpy).not.toHaveBeenCalled()536 expect(putSpy).not.toHaveBeenCalled()537 })538})
modified:src/middleware/cache/index.ts+6 added-2 removed