fix(etag): skip unsafe methods or error responses on non-* case (#5196)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by k. Naka · committed by GitHub · parent cd31bc1 · Browse files · 2 files·+29 added−9 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+29 added−9 removed
- tests:+21 added−5 removed
- other:+8 added−4 removed
Provenance
- Commit
f6aa913- Tree
3dd156a- Author
k. Naka <100704180+na-trium-144@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
cd31bc1
2 files changed+29 added−9 removed
modified:src/middleware/etag/index.test.ts+21 added-5 removed
271 unmodified lines272 })273 expect(res.status).toBe(304)274 })275 276- it('Should not return 304 for `If-None-Match: *` on unsafe methods or error responses', async () => {276+ it('Should not return 304 on unsafe methods or error responses', async () => {277 const app = new Hono()278 app.use('/etag/*', etag())279- app.put('/etag/put', (c) => c.text('created', 201))280- app.get('/etag/not-found', (c) => c.text('not found', 404))279+ app.put('/etag/put', (c) => c.text('created', 201, { ETag: '"etag-123"' }))280+ app.get('/etag/not-found', (c) => c.text('not found', 404, { ETag: '"etag-123"' }))281 282- // `*` does not apply to unsafe methods:282+ // Unsafe methods:283 let res = await app.request('http://localhost/etag/put', {284 method: 'PUT',285 headers: {286 'If-None-Match': '*',1 unmodified line288 })289 expect(res.status).toBe(201)290 expect(await res.text()).toBe('created')291 292- // `*` does not apply when there is no current representation:292+ res = await app.request('http://localhost/etag/put', {293+ method: 'PUT',294+ headers: {295+ 'If-None-Match': '"etag-123"',296+ },297+ })298+ expect(res.status).toBe(201)299+ expect(await res.text()).toBe('created')300+301+ // Error responses:302 res = await app.request('http://localhost/etag/not-found', {303 headers: {304 'If-None-Match': '*',305 },306 })307 expect(res.status).toBe(404)308+309+ res = await app.request('http://localhost/etag/not-found', {310+ headers: {311+ 'If-None-Match': '"etag-123"',312+ },313+ })314+ expect(res.status).toBe(404)315 })316 317 it('Should not return duplicate etag header values', async () => {318 const app = new Hono()95 unmodified linesmodified:src/middleware/etag/index.ts+8 added-4 removed
84 unmodified lines85868788899091929394959697989910 unmodified lines11011111211310710810911011411511611711815 unmodified lines84 unmodified lines const ifNoneMatch = c.req.header('If-None-Match') ?? null
await next()
if ( !(c.req.method === 'GET' || c.req.method === 'HEAD' || c.req.method === 'QUERY') || !c.res.ok ) { return }
const res = c.res as Response let etag = res.headers.get('ETag')
if (!etag) {10 unmodified lines } etag = weak ? `W/"${hash}"` : `"${hash}"` }
const matched = ifNoneMatch === '*' ? (c.req.method === 'GET' || c.req.method === 'HEAD' || c.req.method === 'QUERY') && res.ok : etagMatches(etag, ifNoneMatch) const matched = ifNoneMatch === '*' || etagMatches(etag, ifNoneMatch)
if (matched) { c.res = new Response(null, { status: 304,15 unmodified lines