Skip to content

Commit

Permalink
weak etag match support (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
lqqyt2423 authored Apr 22, 2024
1 parent 8b6a37e commit 9eccf40
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function fastifyEtag (app, { algorithm, weak, replyWith304 = true }) {
reply.header('etag', etag)
}

if (replyWith304 && req.headers['if-none-match'] === etag) {
if (replyWith304 && (req.headers['if-none-match'] === etag || req.headers['if-none-match'] === 'W/' + etag || 'W/' + req.headers['if-none-match'] === etag)) {
reply.code(304)
newPayload = ''
}
Expand Down
48 changes: 48 additions & 0 deletions test/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,52 @@ module.exports = function ({ test }, etagOpts, hashFn) {
etag: 'W/' + hash
})
})

test('returns a 304 if weak etag matches', async (t) => {
const res = await build({ weak: true }).inject({
url: '/',
headers: {
'If-None-Match': 'W/' + hash
}
})

t.equal(res.statusCode, 304)
t.equal(res.body, '')
t.match(res.headers, {
'content-length': '0',
etag: 'W/' + hash
})
})

test('returns a 304 if etag is strong and If-None-Match is weak', async (t) => {
const res = await build().inject({
url: '/',
headers: {
'If-None-Match': 'W/' + hash
}
})

t.equal(res.statusCode, 304)
t.equal(res.body, '')
t.match(res.headers, {
'content-length': '0',
etag: hash
})
})

test('returns a 304 if etag is weak and If-None-Match is strong', async (t) => {
const res = await build({ weak: true }).inject({
url: '/',
headers: {
'If-None-Match': hash
}
})

t.equal(res.statusCode, 304)
t.equal(res.body, '')
t.match(res.headers, {
'content-length': '0',
etag: 'W/' + hash
})
})
}

0 comments on commit 9eccf40

Please sign in to comment.