Skip to content

Commit

Permalink
build: handle unexpected situations
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jun 26, 2019
1 parent f431ece commit 5dda36e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -89,6 +89,8 @@ module.exports = ({
const isHit = !hasForce && cachedResult !== undefined
const result = isHit ? cachedResult : await get({ req, res, ...opts })

if (!result) return

const {
etag: cachedEtag,
ttl = defaultTtl,
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Expand Up @@ -217,3 +217,23 @@ test('compress support', async t => {
t.is(headersTwo['x-cache-status'], 'HIT')
t.is(headersOne.etag, headersTwo.etag)
})

test('prevent send if data is undefined', async t => {
let isSendCalled = false
const url = await createServer({
compress: true,
get: ({ req, res }) => {
throw Error()
},
send: ({ data, headers, res, req, ...props }) => {
isSendCalled = true
res.end('Welcome to Micro')
}
})

try {
await got(`${url}/kikobeats`, { retry: 0 })
} catch (err) {
t.false(isSendCalled)
}
})

0 comments on commit 5dda36e

Please sign in to comment.