Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Expect return true immediately if If-None-Match matches the ETag header (#35) #38

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,27 @@ function fresh (reqHeaders, resHeaders) {
}

// if-none-match
if (noneMatch && noneMatch !== '*') {
if (noneMatch) {
if (noneMatch === '*') {
return true
}
var etag = resHeaders.etag

if (!etag) {
return false
}

var etagStale = true
var etagFresh = false
var matches = parseTokenList(noneMatch)
for (var i = 0; i < matches.length; i++) {
var match = matches[i]
if (match === etag || match === 'W/' + etag || 'W/' + match === etag) {
etagStale = false
etagFresh = true
break
}
}

if (etagStale) {
return false
}
return etagFresh
}

// if-modified-since
Expand Down
4 changes: 2 additions & 2 deletions test/fresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ describe('fresh(reqHeaders, resHeaders)', function () {
})

describe('when only ETag matches', function () {
it('should be stale', function () {
it('should be fresh', function () {
var reqHeaders = { 'if-none-match': '"foo"', 'if-modified-since': 'Sat, 01 Jan 2000 00:00:00 GMT' }
var resHeaders = { etag: '"foo"', 'last-modified': 'Sat, 01 Jan 2000 01:00:00 GMT' }
assert.ok(!fresh(reqHeaders, resHeaders))
assert.ok(fresh(reqHeaders, resHeaders))
})
})

Expand Down
Loading