Skip to content
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

Revert "fix: support for embedded cors for route params (#278)" #285

Merged
merged 2 commits into from
Dec 18, 2023
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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
35 changes: 1 addition & 34 deletions test/cors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -942,36 +942,3 @@ 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(3)

const fastify = Fastify()

const custom = async (instance, opts) => {
instance.register(cors, {
origin: ['example.com']
})

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)
t.equal(res.headers['access-control-allow-origin'], 'example.com')
})
})
77 changes: 75 additions & 2 deletions test/preflight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand All @@ -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)
}
})
Expand Down Expand Up @@ -360,3 +360,76 @@ test('Can override preflight response with preflightContinue', t => {
})
})
})

test('Should support ongoing prefix ', t => {
t.plan(12)

const fastify = Fastify()

fastify.register(async (instance) => {
instance.register(cors)
}, { prefix: '/prefix' })

// support prefixed route
fastify.inject({
method: 'OPTIONS',
url: '/prefix',
headers: {
'access-control-request-method': 'GET',
origin: 'example.com'
}
}, (err, res) => {
t.error(err)
delete res.headers.date
t.equal(res.statusCode, 204)
t.equal(res.payload, '')
t.match(res.headers, {
'access-control-allow-origin': '*',
'access-control-allow-methods': 'GET,HEAD,PUT,PATCH,POST,DELETE',
vary: 'Origin, Access-Control-Request-Headers',
'content-length': '0'
})
})

// support prefixed route without / continue
fastify.inject({
method: 'OPTIONS',
url: '/prefixfoo',
headers: {
'access-control-request-method': 'GET',
origin: 'example.com'
}
}, (err, res) => {
t.error(err)
delete res.headers.date
t.equal(res.statusCode, 204)
t.equal(res.payload, '')
t.match(res.headers, {
'access-control-allow-origin': '*',
'access-control-allow-methods': 'GET,HEAD,PUT,PATCH,POST,DELETE',
vary: 'Origin, Access-Control-Request-Headers',
'content-length': '0'
})
})

// support prefixed route with / continue
fastify.inject({
method: 'OPTIONS',
url: '/prefix/foo',
headers: {
'access-control-request-method': 'GET',
origin: 'example.com'
}
}, (err, res) => {
t.error(err)
delete res.headers.date
t.equal(res.statusCode, 204)
t.equal(res.payload, '')
t.match(res.headers, {
'access-control-allow-origin': '*',
'access-control-allow-methods': 'GET,HEAD,PUT,PATCH,POST,DELETE',
vary: 'Origin, Access-Control-Request-Headers',
'content-length': '0'
})
})
})
Loading