From 923437f401d78eee7b461bdcdf9a559626d451b6 Mon Sep 17 00:00:00 2001 From: Mohammed Slimani Date: Fri, 17 Nov 2023 18:57:53 +0100 Subject: [PATCH 1/3] add support for imbedded cors for route params --- index.js | 2 +- test/cors.test.js | 38 ++++++++++++++++++++++++++++++++++++++ test/preflight.test.js | 4 ++-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 28dfc9a..f113e81 100644 --- a/index.js +++ b/index.js @@ -72,7 +72,7 @@ function fastifyCors (fastify, opts, next) { // remove most headers (such as the Authentication header). // // This route simply enables fastify to accept preflight requests. - fastify.options('*', { schema: { hide: hideOptionsRoute } }, (req, reply) => { + fastify.options('/*', { schema: { hide: hideOptionsRoute } }, (req, reply) => { if (!req.corsPreflightEnabled) { // Do not handle preflight requests if the origin option disabled CORS reply.callNotFound() diff --git a/test/cors.test.js b/test/cors.test.js index 8819277..6be32a8 100644 --- a/test/cors.test.js +++ b/test/cors.test.js @@ -942,3 +942,41 @@ test('Should support wildcard config /2', t => { t.equal(res.headers['access-control-allow-origin'], '*') }) }) + +test('should support embedded cors registration with route params', t => { + t.plan(2) + + const fastify = Fastify() + + const custom = async (instance, opts) => { + instance.register(cors, instance => async (req, cb) => { + const corsOptions = { + credentials: true, + origin: ['example1'], + methods: 'PUT,GET,POST,DELETE', + preflightContinue: false + } + cb(null, corsOptions) + }) + + instance.get('/route1', (req, reply) => { + reply.send('ok') + }) + } + + fastify.register(custom, { + prefix: '/:id' + }) + + fastify.inject({ + method: 'OPTIONS', + url: '/id1/route1', + headers: { + 'access-control-request-method': 'GET', + origin: 'example.com' + } + }, (err, res) => { + t.error(err) + t.equal(res.statusCode, 204) + }) +}) diff --git a/test/preflight.test.js b/test/preflight.test.js index f138915..42ed499 100644 --- a/test/preflight.test.js +++ b/test/preflight.test.js @@ -189,7 +189,7 @@ test('hide options route by default', t => { const fastify = Fastify() fastify.addHook('onRoute', (route) => { - if (route.method === 'OPTIONS' && route.url === '*') { + if (route.method === 'OPTIONS' && route.url === '/*') { t.equal(route.schema.hide, true) } }) @@ -206,7 +206,7 @@ test('show options route', t => { const fastify = Fastify() fastify.addHook('onRoute', (route) => { - if (route.method === 'OPTIONS' && route.url === '*') { + if (route.method === 'OPTIONS' && route.url === '/*') { t.equal(route.schema.hide, false) } }) From ab7623ad503aa4f471ed9b46f3d5b05a4379f215 Mon Sep 17 00:00:00 2001 From: Mohammed Slimani Date: Fri, 17 Nov 2023 19:07:28 +0100 Subject: [PATCH 2/3] embedded cors in route params enhanced tests --- test/cors.test.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/cors.test.js b/test/cors.test.js index 6be32a8..306aa02 100644 --- a/test/cors.test.js +++ b/test/cors.test.js @@ -354,7 +354,7 @@ test('Should support dynamic config. (Invalid function)', t => { t.plan(2) const fastify = Fastify() - fastify.register(cors, () => (a, b, c) => {}) + fastify.register(cors, () => (a, b, c) => { }) fastify.get('/', (req, reply) => { reply.send('ok') @@ -944,17 +944,14 @@ test('Should support wildcard config /2', t => { }) test('should support embedded cors registration with route params', t => { - t.plan(2) + t.plan(3) const fastify = Fastify() const custom = async (instance, opts) => { instance.register(cors, instance => async (req, cb) => { const corsOptions = { - credentials: true, - origin: ['example1'], - methods: 'PUT,GET,POST,DELETE', - preflightContinue: false + origin: ['example.com'] } cb(null, corsOptions) }) @@ -978,5 +975,6 @@ test('should support embedded cors registration with route params', t => { }, (err, res) => { t.error(err) t.equal(res.statusCode, 204) + t.equal(res.headers['access-control-allow-origin'], 'example.com') }) }) From 62c3b4762f7dd394a869be2fdf50564c88d3b75e Mon Sep 17 00:00:00 2001 From: Slimani Mohammed <45685744+mohammedSlimani@users.noreply.github.com> Date: Sun, 19 Nov 2023 23:43:16 +0100 Subject: [PATCH 3/3] Update test/cors.test.js Co-authored-by: Manuel Spigolon --- test/cors.test.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test/cors.test.js b/test/cors.test.js index 306aa02..76e227f 100644 --- a/test/cors.test.js +++ b/test/cors.test.js @@ -949,11 +949,8 @@ test('should support embedded cors registration with route params', t => { const fastify = Fastify() const custom = async (instance, opts) => { - instance.register(cors, instance => async (req, cb) => { - const corsOptions = { - origin: ['example.com'] - } - cb(null, corsOptions) + instance.register(cors, { + origin: ['example.com'] }) instance.get('/route1', (req, reply) => {