Skip to content

Commit

Permalink
inject function return normal value with non-ready app (#2417)
Browse files Browse the repository at this point in the history
* inject return normal value with non-ready app

* remove async/await using in node 6 version
  • Loading branch information
zhangwinning committed Jul 18, 2020
1 parent e513a59 commit 3969012
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 9 additions & 2 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,15 @@ function build (options) {
else lightMyRequest(httpHandler, opts, cb)
})
} else {
return this.ready()
.then(() => lightMyRequest(httpHandler, opts))
return lightMyRequest((req, res) => {
this.ready(function (err) {
if (err) {
res.emit('error', err)
return
}
httpHandler(req, res)
})
}, opts)
}
}

Expand Down
15 changes: 15 additions & 0 deletions test/inject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,18 @@ test('should throw error if callback specified and if ready errors', t => {
t.strictEqual(err, error)
})
})

test('should support builder-style injection with non-ready app', (t) => {
t.plan(3)
const fastify = Fastify()
const payload = { hello: 'world' }

fastify.get('/', (req, reply) => {
reply.send(payload)
})
fastify.inject().get('/').end().then((res) => {
t.deepEqual(payload, JSON.parse(res.payload))
t.strictEqual(res.statusCode, 200)
t.strictEqual(res.headers['content-length'], '17')
})
})

0 comments on commit 3969012

Please sign in to comment.