@fastify/reply-from
Version:
forward your HTTP request to another server, for fastify
48 lines (40 loc) • 1.1 kB
JavaScript
const { test } = require('tap')
const Fastify = require('fastify')
const got = require('got')
const proxyquire = require('proxyquire')
// Stub request to throw error 'foo'
const From = proxyquire('..', {
'./lib/request': function () {
return {
request: (_opts, callback) => { callback(new Error('foo')) },
close: () => {}
}
}
})
test('unexpected error renders 500', async (t) => {
const instance = Fastify()
t.teardown(instance.close.bind(instance))
instance.get('/', (_request, reply) => {
reply.code(201)
reply.from()
})
instance.register(From, {
base: 'http://localhost'
})
await instance.listen({ port: 0 })
try {
await got(`http://localhost:${instance.server.address().port}`)
} catch (err) {
t.equal(err.response.statusCode, 500)
t.match(err.response.headers['content-type'], /application\/json/)
t.same(JSON.parse(err.response.body), {
statusCode: 500,
code: 'FST_REPLY_FROM_INTERNAL_SERVER_ERROR',
error: 'Internal Server Error',
message: 'foo'
})
return
}
t.fail()
})