Skip to content

Add constraints option #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface FastifyHttpProxyOptions extends FastifyReplyFromOptions {
wsClientOptions?: ClientOptions;
wsServerOptions?: ServerOptions;
httpMethods?: string[];
constraints?: { [name: string]: any };
}

declare const fastifyHttpProxy: FastifyPlugin<FastifyHttpProxyOptions>;
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,15 @@ async function httpProxy (fastify, opts) {
method: opts.httpMethods || httpMethods,
preHandler,
config: opts.config || {},
constraints: opts.constraints || {},
handler
})
fastify.route({
url: '/*',
method: opts.httpMethods || httpMethods,
preHandler,
config: opts.config || {},
constraints: opts.constraints || {},
handler
})

Expand Down
3 changes: 2 additions & 1 deletion test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ app.register(fastifyHttpProxy, {
maxFreeSockets: 10,
maxSockets: 20,
rejectUnauthorized: true,
sessionTimeout: 30000
sessionTimeout: 30000,
constraints: { version: '1.0.2' }
});
87 changes: 87 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()