Skip to content

Commit

Permalink
Moving towards 100% coverage fastify#60
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyringoet committed May 14, 2021
1 parent a708263 commit e8dad64
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
50 changes: 50 additions & 0 deletions test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,56 @@ test('cache is usable', (t) => {
})
})

test('getting cache item with error returns error', (t) => {
t.plan(1)
const mockCache = {
get: (info, callback) => callback(new Error('cache.get always errors')),
set: (key, value, ttl, callback) => callback()
}

const instance = fastify()
instance.register(plugin, { cache: mockCache })

instance.get('/one', (req, reply) => {
instance.cache.set('one', { one: true }, 1000, (err) => {
if (err) return reply.send(err)
return reply
.etag('123456')
.send({ hello: 'world' })
})
})

instance.get('/two', (req, reply) => {
instance.cache.get('one', (err, obj) => {
t.notOk(err)
t.notOk(obj)
})
})

instance.listen(0, (err) => {
if (err) t.threw(err)
instance.server.unref()
const portNum = instance.server.address().port
const address = `http://127.0.0.1:${portNum}/one`

http
.get(address, (res) => {
const opts = {
host: '127.0.0.1',
port: portNum,
path: '/two',
headers: {
'if-none-match': '123456'
}
}
http.get(opts, (res) => {
t.equal(res.statusCode, 500)
}).on('error', t.threw)
})
.on('error', t.threw)
})
})

test('etags get stored in cache', (t) => {
t.plan(1)
const instance = fastify()
Expand Down
41 changes: 41 additions & 0 deletions test/headers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ test('decorators add headers', (t) => {
})
})

test('sets etag header for falsy argument', (t) => {
t.plan(1)
const instance = fastify()
instance.register(plugin)
instance.get('/', (req, reply) => {
reply
.etag()
.send()
})
instance.listen(0, (err) => {
if (err) t.threw(err)
instance.server.unref()
const portNum = instance.server.address().port
const address = `http://127.0.0.1:${portNum}`
http.get(address, (res) => {
t.ok(res.headers.etag)
}).on('error', (err) => t.threw(err))
})
})

test('sets no-cache header', (t) => {
t.plan(2)
const instance = fastify()
Expand Down Expand Up @@ -130,3 +150,24 @@ test('sets the expires header', (t) => {
}).on('error', (err) => t.threw(err))
})
})

test('sets the expires header to a falsy value', (t) => {
t.plan(1)
const instance = fastify()
instance.register(plugin, { privacy: plugin.privacy.NOCACHE })
instance.get('/', (req, reply) => {
reply
.expires()
.send({ hello: 'world' })
})
instance.listen(0, (err) => {
if (err) t.threw(err)
instance.server.unref()
const portNum = instance.server.address().port
const address = `http://127.0.0.1:${portNum}`

http.get(address, (res) => {
t.notOk(res.headers.expires)
}).on('error', (err) => t.threw(err))
})
})

0 comments on commit e8dad64

Please sign in to comment.