Skip to content

Commit 1ca37b9

Browse files
test: moving towards 100% coverage #60 (#64)
* Moving towards 100% coverage #60 * Final tests to get 100% coverage. Remove .taprc
1 parent a708263 commit 1ca37b9

File tree

3 files changed

+161
-1
lines changed

3 files changed

+161
-1
lines changed

.taprc

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/cache.test.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,104 @@ test('cache is usable', (t) => {
6565
})
6666
})
6767

68+
test('cache is usable with function as plugin default options input', (t) => {
69+
t.plan(3)
70+
const instance = fastify()
71+
instance
72+
.register((i, o, n) => {
73+
i.addHook('onRequest', function (req, reply, done) {
74+
t.notOk(i[Symbol.for('fastify-caching.registered')])
75+
done()
76+
})
77+
n()
78+
})
79+
.register(plugin, () => () => { })
80+
81+
instance.addHook('preParsing', function (req, reply, payload, done) {
82+
t.equal(this[Symbol.for('fastify-caching.registered')], true)
83+
done(null, payload)
84+
})
85+
86+
instance.get('/one', (req, reply) => {
87+
instance.cache.set('one', { one: true }, 100, (err) => {
88+
if (err) return reply.send(err)
89+
reply.redirect('/two')
90+
})
91+
})
92+
93+
instance.get('/two', (req, reply) => {
94+
instance.cache.get('one', (err, obj) => {
95+
if (err) t.threw(err)
96+
t.same(obj.item, { one: true })
97+
reply.send()
98+
})
99+
})
100+
101+
instance.listen(0, (err) => {
102+
if (err) t.threw(err)
103+
instance.server.unref()
104+
const portNum = instance.server.address().port
105+
const address = `http://127.0.0.1:${portNum}/one`
106+
http
107+
.get(address, (res) => {
108+
if (res.statusCode > 300 && res.statusCode < 400 && res.headers.location) {
109+
http.get(`http://127.0.0.1:${portNum}${res.headers.location}`, (res) => { }).on('error', t.threw)
110+
}
111+
})
112+
.on('error', t.threw)
113+
})
114+
})
115+
116+
test('getting cache item with error returns error', (t) => {
117+
t.plan(1)
118+
const mockCache = {
119+
get: (info, callback) => callback(new Error('cache.get always errors')),
120+
set: (key, value, ttl, callback) => callback()
121+
}
122+
123+
const instance = fastify()
124+
instance.register(plugin, { cache: mockCache })
125+
126+
instance.get('/one', (req, reply) => {
127+
instance.cache.set('one', { one: true }, 1000, (err) => {
128+
if (err) return reply.send(err)
129+
return reply
130+
.etag('123456')
131+
.send({ hello: 'world' })
132+
})
133+
})
134+
135+
instance.get('/two', (req, reply) => {
136+
instance.cache.get('one', (err, obj) => {
137+
t.notOk(err)
138+
t.notOk(obj)
139+
})
140+
})
141+
142+
instance.listen(0, (err) => {
143+
if (err) t.threw(err)
144+
instance.server.unref()
145+
const portNum = instance.server.address().port
146+
const address = `http://127.0.0.1:${portNum}/one`
147+
148+
http
149+
.get(address, (res) => {
150+
const opts = {
151+
host: '127.0.0.1',
152+
port: portNum,
153+
path: '/two',
154+
headers: {
155+
'if-none-match': '123456'
156+
}
157+
}
158+
http.get(opts, (res) => {
159+
t.equal(res.statusCode, 500)
160+
}).on('error', t.threw)
161+
})
162+
.on('error', t.threw)
163+
})
164+
})
165+
68166
test('etags get stored in cache', (t) => {
69167
t.plan(1)
70168
const instance = fastify()

test/headers.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ test('decorators add headers', (t) => {
4444
})
4545
})
4646

47+
test('sets etag header for falsy argument', (t) => {
48+
t.plan(1)
49+
const instance = fastify()
50+
instance.register(plugin)
51+
instance.get('/', (req, reply) => {
52+
reply
53+
.etag()
54+
.send()
55+
})
56+
instance.listen(0, (err) => {
57+
if (err) t.threw(err)
58+
instance.server.unref()
59+
const portNum = instance.server.address().port
60+
const address = `http://127.0.0.1:${portNum}`
61+
http.get(address, (res) => {
62+
t.ok(res.headers.etag)
63+
}).on('error', (err) => t.threw(err))
64+
})
65+
})
66+
4767
test('sets no-cache header', (t) => {
4868
t.plan(2)
4969
const instance = fastify()
@@ -130,3 +150,46 @@ test('sets the expires header', (t) => {
130150
}).on('error', (err) => t.threw(err))
131151
})
132152
})
153+
154+
test('sets the expires header to a falsy value', (t) => {
155+
t.plan(1)
156+
const instance = fastify()
157+
instance.register(plugin, { privacy: plugin.privacy.NOCACHE })
158+
instance.get('/', (req, reply) => {
159+
reply
160+
.expires()
161+
.send({ hello: 'world' })
162+
})
163+
instance.listen(0, (err) => {
164+
if (err) t.threw(err)
165+
instance.server.unref()
166+
const portNum = instance.server.address().port
167+
const address = `http://127.0.0.1:${portNum}`
168+
169+
http.get(address, (res) => {
170+
t.notOk(res.headers.expires)
171+
}).on('error', (err) => t.threw(err))
172+
})
173+
})
174+
175+
test('sets the expires header to a custom value', (t) => {
176+
t.plan(2)
177+
const instance = fastify()
178+
instance.register(plugin, { privacy: plugin.privacy.NOCACHE })
179+
instance.get('/', (req, reply) => {
180+
reply
181+
.expires('foobar')
182+
.send({ hello: 'world' })
183+
})
184+
instance.listen(0, (err) => {
185+
if (err) t.threw(err)
186+
instance.server.unref()
187+
const portNum = instance.server.address().port
188+
const address = `http://127.0.0.1:${portNum}`
189+
190+
http.get(address, (res) => {
191+
t.ok(res.headers.expires)
192+
t.equal(res.headers.expires, 'foobar')
193+
}).on('error', (err) => t.threw(err))
194+
})
195+
})

0 commit comments

Comments
 (0)