diff --git a/test/cache.test.js b/test/cache.test.js index 00c701a..570cf38 100644 --- a/test/cache.test.js +++ b/test/cache.test.js @@ -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() diff --git a/test/headers.test.js b/test/headers.test.js index a85ac74..5196097 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -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() @@ -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)) + }) +})