diff --git a/index.d.ts b/index.d.ts index 1a1a62f..508ae71 100644 --- a/index.d.ts +++ b/index.d.ts @@ -22,6 +22,7 @@ export interface FastifyHttpProxyOptions extends FastifyReplyFromOptions { wsClientOptions?: ClientOptions; wsServerOptions?: ServerOptions; httpMethods?: string[]; + constraints?: { [name: string]: any }; } declare const fastifyHttpProxy: FastifyPlugin; diff --git a/index.js b/index.js index 29806dc..b65b371 100644 --- a/index.js +++ b/index.js @@ -171,6 +171,7 @@ async function httpProxy (fastify, opts) { method: opts.httpMethods || httpMethods, preHandler, config: opts.config || {}, + constraints: opts.constraints || {}, handler }) fastify.route({ @@ -178,6 +179,7 @@ async function httpProxy (fastify, opts) { method: opts.httpMethods || httpMethods, preHandler, config: opts.config || {}, + constraints: opts.constraints || {}, handler }) diff --git a/test/index.test-d.ts b/test/index.test-d.ts index b4941e2..5a02bec 100644 --- a/test/index.test-d.ts +++ b/test/index.test-d.ts @@ -39,5 +39,6 @@ app.register(fastifyHttpProxy, { maxFreeSockets: 10, maxSockets: 20, rejectUnauthorized: true, - sessionTimeout: 30000 + sessionTimeout: 30000, + constraints: { version: '1.0.2' } }); diff --git a/test/test.js b/test/test.js index 896f50b..56d1263 100644 --- a/test/test.js +++ b/test/test.js @@ -567,6 +567,93 @@ async function run () { } t.ok(errored) }) + + const getTestConstraint = () => ({ + name: 'testConstraint', + storage: () => { + let headerValues = {} + return { + get: (value) => { return headerValues[value] || null }, + set: (value, store) => { headerValues[value] = store }, + del: (value) => { delete headerValues[value] }, + empty: () => { headerValues = {} } + } + }, + validate (value) { return true }, + deriveConstraint: (req, ctx) => { + return req.headers['test-header'] + } + }) + + test('constraints', async t => { + const server = Fastify({ + constraints: { + testConstraint: getTestConstraint() + } + }) + server.register(proxy, { + upstream: `http://localhost:${origin.server.address().port}`, + constraints: { testConstraint: 'valid-value' } + }) + + await server.listen(0) + t.teardown(server.close.bind(server)) + await got(`http://localhost:${server.server.address().port}/a`, { + headers: { + 'test-header': 'valid-value' + } + }) + + try { + await got(`http://localhost:${server.server.address().port}/a`, { + headers: { + 'test-header': 'invalid-value' + } + }) + t.fail() + } catch (err) { + t.equal(err.response.statusCode, 404) + } + + try { + await got(`http://localhost:${server.server.address().port}/a`) + t.fail() + } catch (err) { + t.equal(err.response.statusCode, 404) + } + }) + + test('constraints with unconstrained routes', async t => { + const server = Fastify({ + constraints: { + testConstraint: getTestConstraint() + } + }) + server.get('/a', { + constraints: { testConstraint: 'without-proxy' } + }, async () => 'this is unproxied a') + server.register(proxy, { + upstream: `http://localhost:${origin.server.address().port}`, + constraints: { testConstraint: 'with-proxy' } + }) + + await server.listen(0) + t.teardown(server.close.bind(server)) + + const resultProxied = await got(`http://localhost:${server.server.address().port}/a`, { + headers: { + 'test-header': 'with-proxy' + } + }) + t.equal(resultProxied.body, 'this is a') + + const resultUnproxied = await got(`http://localhost:${server.server.address().port}/a`, { + headers: { + 'test-header': 'without-proxy' + } + }) + t.equal(resultUnproxied.body, 'this is unproxied a') + }) } run()