Skip to content

Commit

Permalink
Correctly support 304s
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Apr 17, 2018
1 parent 460102e commit b30b7e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ function fastifyStatic (fastify, opts, next) {
const wrap = new PassThrough({
flush (cb) {
this.finished = true
if (reply.res.statusCode === 304) {
reply.send('')
}
cb()
}
})
Expand Down
27 changes: 26 additions & 1 deletion test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ t.test('register /static', t => {
})

t.test('register /static/', t => {
t.plan(9)
t.plan(10)

const pluginOptions = {
root: path.join(__dirname, '/static'),
Expand Down Expand Up @@ -278,6 +278,31 @@ t.test('register /static/', t => {
genericErrorResponseChecks(t, response)
})
})

t.test('304', t => {
t.plan(5 + GENERIC_RESPONSE_CHECK_COUNT)
simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html'
}, (err, response, body) => {
t.error(err)
const etag = response.headers.etag
t.strictEqual(response.statusCode, 200)
t.strictEqual(body.toString(), indexContent)
genericResponseChecks(t, response)

simple.concat({
method: 'GET',
url: 'http://localhost:' + fastify.server.address().port + '/static/index.html',
headers: {
'if-none-match': etag
}
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 304)
})
})
})
})
})

Expand Down

0 comments on commit b30b7e3

Please sign in to comment.