Skip to content

Commit 04e49eb

Browse files
Add constraints option (#182)
* Add constraints to options * Add tests and typescript declarations * Add multiple routes tests
1 parent 579df91 commit 04e49eb

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface FastifyHttpProxyOptions extends FastifyReplyFromOptions {
2222
wsClientOptions?: ClientOptions;
2323
wsServerOptions?: ServerOptions;
2424
httpMethods?: string[];
25+
constraints?: { [name: string]: any };
2526
}
2627

2728
declare const fastifyHttpProxy: FastifyPlugin<FastifyHttpProxyOptions>;

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,15 @@ async function httpProxy (fastify, opts) {
171171
method: opts.httpMethods || httpMethods,
172172
preHandler,
173173
config: opts.config || {},
174+
constraints: opts.constraints || {},
174175
handler
175176
})
176177
fastify.route({
177178
url: '/*',
178179
method: opts.httpMethods || httpMethods,
179180
preHandler,
180181
config: opts.config || {},
182+
constraints: opts.constraints || {},
181183
handler
182184
})
183185

test/index.test-d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ app.register(fastifyHttpProxy, {
3939
maxFreeSockets: 10,
4040
maxSockets: 20,
4141
rejectUnauthorized: true,
42-
sessionTimeout: 30000
42+
sessionTimeout: 30000,
43+
constraints: { version: '1.0.2' }
4344
});

test/test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,93 @@ async function run () {
567567
}
568568
t.ok(errored)
569569
})
570+
571+
const getTestConstraint = () => ({
572+
name: 'testConstraint',
573+
storage: () => {
574+
let headerValues = {}
575+
return {
576+
get: (value) => { return headerValues[value] || null },
577+
set: (value, store) => { headerValues[value] = store },
578+
del: (value) => { delete headerValues[value] },
579+
empty: () => { headerValues = {} }
580+
}
581+
},
582+
validate (value) { return true },
583+
deriveConstraint: (req, ctx) => {
584+
return req.headers['test-header']
585+
}
586+
})
587+
588+
test('constraints', async t => {
589+
const server = Fastify({
590+
constraints: {
591+
testConstraint: getTestConstraint()
592+
}
593+
})
594+
server.register(proxy, {
595+
upstream: `http://localhost:${origin.server.address().port}`,
596+
constraints: { testConstraint: 'valid-value' }
597+
})
598+
599+
await server.listen(0)
600+
t.teardown(server.close.bind(server))
601+
await got(`http://localhost:${server.server.address().port}/a`, {
602+
headers: {
603+
'test-header': 'valid-value'
604+
}
605+
})
606+
607+
try {
608+
await got(`http://localhost:${server.server.address().port}/a`, {
609+
headers: {
610+
'test-header': 'invalid-value'
611+
}
612+
})
613+
t.fail()
614+
} catch (err) {
615+
t.equal(err.response.statusCode, 404)
616+
}
617+
618+
try {
619+
await got(`http://localhost:${server.server.address().port}/a`)
620+
t.fail()
621+
} catch (err) {
622+
t.equal(err.response.statusCode, 404)
623+
}
624+
})
625+
626+
test('constraints with unconstrained routes', async t => {
627+
const server = Fastify({
628+
constraints: {
629+
testConstraint: getTestConstraint()
630+
}
631+
})
632+
server.get('/a', {
633+
constraints: { testConstraint: 'without-proxy' }
634+
}, async () => 'this is unproxied a')
635+
server.register(proxy, {
636+
upstream: `http://localhost:${origin.server.address().port}`,
637+
constraints: { testConstraint: 'with-proxy' }
638+
})
639+
640+
await server.listen(0)
641+
t.teardown(server.close.bind(server))
642+
643+
const resultProxied = await got(`http://localhost:${server.server.address().port}/a`, {
644+
headers: {
645+
'test-header': 'with-proxy'
646+
}
647+
})
648+
t.equal(resultProxied.body, 'this is a')
649+
650+
const resultUnproxied = await got(`http://localhost:${server.server.address().port}/a`, {
651+
headers: {
652+
'test-header': 'without-proxy'
653+
}
654+
})
655+
t.equal(resultUnproxied.body, 'this is unproxied a')
656+
})
570657
}
571658

572659
run()

0 commit comments

Comments
 (0)