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

fix: add support for nested index routes (fixes #281) #282

Closed
Closed
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
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,21 @@ function fastifyCors (fastify, opts, next) {
// preflight requests BEFORE possible authentication plugins. If the preflight reply
// occurred in this handler, other plugins may deny the request since the browser will
// remove most headers (such as the Authentication header).
//
// This route simply enables fastify to accept preflight requests.
fastify.options('/*', { schema: { hide: hideOptionsRoute } }, (req, reply) => {
const handlerOptions = { schema: { hide: hideOptionsRoute } }
const handler = (req, reply) => {
if (!req.corsPreflightEnabled) {
// Do not handle preflight requests if the origin option disabled CORS
reply.callNotFound()
return
}

reply.send()
})
}

// This route enables fastify to accept preflight requests on all nested routes, as well as the index route in prefixed setups
// https://github.com/fastify/fastify-cors/issues/281
fastify.options('/*', handlerOptions, handler)
if (fastify.prefix) { fastify.options('/', handlerOptions, handler) }
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without doing this if check existing tests break - this literally just fixes the case for the test I added.

I think on root index routes, fastify applies /* to the index route, but on prefixed routes, it doesn't?


next()
}
Expand Down
35 changes: 35 additions & 0 deletions test/preflight.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,38 @@ test('Can override preflight response with preflightContinue', t => {
})
})
})

test('Can get a preflight response on prefixed index routes', t => {
t.plan(4)

const fastify = Fastify()
const noop = () => {}
const prefixRoutes = (app, options, done) => {
app.register(cors)
app.get('/', noop)
done()
}
fastify.register(prefixRoutes, { prefix: 'test' })

fastify.ready(() => {
fastify.inject({
method: 'OPTIONS',
url: '/test',
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'
})
})
})
})