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

Set exposeHeadRoutes: true by default #2826

Merged
merged 4 commits into from
Feb 2, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions build/build-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const defaultInitOptions = {
pluginTimeout: 10000,
requestIdHeader: 'request-id',
requestIdLogLabel: 'reqId',
http2SessionTimeout: 5000
http2SessionTimeout: 5000,
exposeHeadRoutes: true
}

function customRule0 (schemaParamValue, validatedParamValue, validationSchemaObject, currentDataPath, validatedParamObject, validatedParam) {
Expand Down Expand Up @@ -79,7 +80,8 @@ const schema = {
pluginTimeout: { type: 'integer', default: defaultInitOptions.pluginTimeout },
requestIdHeader: { type: 'string', default: defaultInitOptions.requestIdHeader },
requestIdLogLabel: { type: 'string', default: defaultInitOptions.requestIdLogLabel },
http2SessionTimeout: { type: 'integer', default: defaultInitOptions.http2SessionTimeout }
http2SessionTimeout: { type: 'integer', default: defaultInitOptions.http2SessionTimeout },
exposeHeadRoutes: { type: 'boolean', default: defaultInitOptions.exposeHeadRoutes }
}
}

Expand Down
2 changes: 1 addition & 1 deletion docs/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ const fastify = require('fastify')({

Automatically creates a sibling `HEAD` route for each `GET` route defined. If you want a custom `HEAD` handler without disabling this option, make sure to define it before the `GET` route.

+ Default: `false`
+ Default: `true`

<a name="versioning"></a>
### `versioning`
Expand Down
5 changes: 3 additions & 2 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ function fastify (options) {
const requestIdLogLabel = options.requestIdLogLabel || 'reqId'
const bodyLimit = options.bodyLimit || defaultInitOptions.bodyLimit
const disableRequestLogging = options.disableRequestLogging || false
const exposeHeadRoutes = options.exposeHeadRoutes != null ? options.exposeHeadRoutes : false

const ajvOptions = Object.assign({
customOptions: {},
Expand Down Expand Up @@ -130,10 +129,12 @@ function fastify (options) {
options.disableRequestLogging = disableRequestLogging
options.ajv = ajvOptions
options.clientErrorHandler = options.clientErrorHandler || defaultClientErrorHandler
options.exposeHeadRoutes = exposeHeadRoutes

const initialConfig = getSecuredInitialConfig(options)

// exposeHeadRoutes have its defult set from the validatorfrom the validatorfrom the validatorfrom the validator
options.exposeHeadRoutes = initialConfig.exposeHeadRoutes

// Default router
const router = buildRouting({
config: {
Expand Down
185 changes: 131 additions & 54 deletions lib/configValidator.js

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions test/context-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,55 @@ test('config with exposeHeadRoutes', t => {
t.deepEquals(JSON.parse(response.payload), { url: '/no-config', method: 'GET' })
})
})

test('config without exposeHeadRoutes', t => {
t.plan(9)
const fastify = Fastify({ exposeHeadRoutes: false })

fastify.get('/get', {
schema: schema.schema,
config: Object.assign({}, schema.config)
}, handler)

fastify.route({
method: 'GET',
url: '/route',
schema: schema.schema,
handler: handler,
config: Object.assign({}, schema.config)
})

fastify.route({
method: 'GET',
url: '/no-config',
schema: schema.schema,
handler: handler
})

fastify.inject({
method: 'GET',
url: '/get'
}, (err, response) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEquals(JSON.parse(response.payload), Object.assign({ url: '/get', method: 'GET' }, schema.config))
})

fastify.inject({
method: 'GET',
url: '/route'
}, (err, response) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEquals(JSON.parse(response.payload), Object.assign({ url: '/route', method: 'GET' }, schema.config))
})

fastify.inject({
method: 'GET',
url: '/no-config'
}, (err, response) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEquals(JSON.parse(response.payload), { url: '/no-config', method: 'GET' })
})
})
2 changes: 1 addition & 1 deletion test/hooks-async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ process.removeAllListeners('warning')
test('async hooks', t => {
t.plan(21)

const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.addHook('onRequest', async function (request, reply) {
await sleep(1)
request.test = 'the request is coming'
Expand Down
38 changes: 19 additions & 19 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ process.removeAllListeners('warning')

test('hooks', t => {
t.plan(43)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })

try {
fastify.addHook('preHandler', function (request, reply, done) {
Expand Down Expand Up @@ -342,7 +342,7 @@ test('preHandler hook should support encapsulation / 5', t => {

test('onRoute hook should be called / 1', t => {
t.plan(2)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })

fastify.register((instance, opts, done) => {
instance.addHook('onRoute', () => {
Expand All @@ -363,7 +363,7 @@ test('onRoute hook should be called / 2', t => {
t.plan(5)
let firstHandler = 0
let secondHandler = 0
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.addHook('onRoute', (route) => {
t.pass()
firstHandler++
Expand Down Expand Up @@ -391,7 +391,7 @@ test('onRoute hook should be called / 2', t => {

test('onRoute hook should be called / 3', t => {
t.plan(5)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })

function handler (req, reply) {
reply.send()
Expand Down Expand Up @@ -423,7 +423,7 @@ test('onRoute hook should be called / 3', t => {

test('onRoute hook should be called (encapsulation support) / 4', t => {
t.plan(4)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })

fastify.addHook('onRoute', () => {
t.pass()
Expand All @@ -450,7 +450,7 @@ test('onRoute hook should be called (encapsulation support) / 4', t => {

test('onRoute hook should be called (encapsulation support) / 5', t => {
t.plan(2)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })

fastify.get('/first', function (req, reply) {
reply.send()
Expand All @@ -477,7 +477,7 @@ test('onRoute hook should be called (encapsulation support) / 5', t => {

test('onRoute hook should be called (encapsulation support) / 6', t => {
t.plan(1)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })

fastify.get('/first', function (req, reply) {
reply.send()
Expand All @@ -494,7 +494,7 @@ test('onRoute hook should be called (encapsulation support) / 6', t => {

test('onRoute should keep the context', t => {
t.plan(4)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.register((instance, opts, done) => {
instance.decorate('test', true)
instance.addHook('onRoute', onRoute)
Expand All @@ -519,7 +519,7 @@ test('onRoute should keep the context', t => {

test('onRoute hook should pass correct route', t => {
t.plan(9)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.addHook('onRoute', (route) => {
t.strictEqual(route.method, 'GET')
t.strictEqual(route.url, '/')
Expand Down Expand Up @@ -547,7 +547,7 @@ test('onRoute hook should pass correct route', t => {

test('onRoute hook should pass correct route with custom prefix', t => {
t.plan(11)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.addHook('onRoute', function (route) {
t.strictEqual(route.method, 'GET')
t.strictEqual(route.url, '/v1/foo')
Expand Down Expand Up @@ -577,7 +577,7 @@ test('onRoute hook should pass correct route with custom prefix', t => {

test('onRoute hook should pass correct route with custom options', t => {
t.plan(6)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.register((instance, opts, done) => {
instance.addHook('onRoute', function (route) {
t.strictEqual(route.method, 'GET')
Expand Down Expand Up @@ -605,7 +605,7 @@ test('onRoute hook should pass correct route with custom options', t => {

test('onRoute hook should receive any route option', t => {
t.plan(5)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.register((instance, opts, done) => {
instance.addHook('onRoute', function (route) {
t.strictEqual(route.method, 'GET')
Expand All @@ -626,7 +626,7 @@ test('onRoute hook should receive any route option', t => {

test('onRoute hook should preserve system route configuration', t => {
t.plan(5)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.register((instance, opts, done) => {
instance.addHook('onRoute', function (route) {
t.strictEqual(route.method, 'GET')
Expand All @@ -650,7 +650,7 @@ test('onRoute hook should preserve handler function in options of shorthand rout

const handler = (req, reply) => {}

const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.register((instance, opts, done) => {
instance.addHook('onRoute', function (route) {
t.strictEqual(route.handler, handler)
Expand All @@ -671,7 +671,7 @@ test('onRoute hook should be called once when prefixTrailingSlash', t => {
let onRouteCalled = 0
let routePatched = 0

const fastify = Fastify({ ignoreTrailingSlash: false })
const fastify = Fastify({ ignoreTrailingSlash: false, exposeHeadRoutes: false })

// a plugin that patches route options, similar to fastify-compress
fastify.register(fp(function myPlugin (instance, opts, next) {
Expand Down Expand Up @@ -710,7 +710,7 @@ test('onRoute hook should be called once when prefixTrailingSlash', t => {
test('onRoute hook should able to change the route url', t => {
t.plan(5)

const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })

fastify.register((instance, opts, done) => {
instance.addHook('onRoute', (route) => {
Expand Down Expand Up @@ -740,9 +740,9 @@ test('onRoute hook should able to change the route url', t => {
})
})

test('onRoute hook that throws should be caught ', t => {
test('onRoute hook that throws should be caught', t => {
t.plan(1)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })

fastify.register((instance, opts, done) => {
instance.addHook('onRoute', () => {
Expand All @@ -761,7 +761,7 @@ test('onRoute hook that throws should be caught ', t => {

test('onRoute hook with many prefix', t => {
t.plan(3)
const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
const handler = (req, reply) => { reply.send({}) }

const onRouteChecks = [
Expand Down
6 changes: 4 additions & 2 deletions test/internals/initialConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ test('without options passed to Fastify, initialConfig should expose default val
pluginTimeout: 10000,
requestIdHeader: 'request-id',
requestIdLogLabel: 'reqId',
http2SessionTimeout: 5000
http2SessionTimeout: 5000,
exposeHeadRoutes: true
}

t.deepEquals(Fastify().initialConfig, fastifyDefaultOptions)
Expand Down Expand Up @@ -242,7 +243,8 @@ test('Should not have issues when passing stream options to Pino.js', t => {
pluginTimeout: 10000,
requestIdHeader: 'request-id',
requestIdLogLabel: 'reqId',
http2SessionTimeout: 5000
http2SessionTimeout: 5000,
exposeHeadRoutes: true
})
} catch (error) {
t.fail()
Expand Down
8 changes: 4 additions & 4 deletions test/pretty-print.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Fastify = require('..')
test('pretty print - static routes', t => {
t.plan(2)

const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.get('/test', () => {})
fastify.get('/test/hello', () => {})
fastify.get('/hello/world', () => {})
Expand All @@ -29,7 +29,7 @@ test('pretty print - static routes', t => {
test('pretty print - parametric routes', t => {
t.plan(2)

const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.get('/test', () => {})
fastify.get('/test/:hello', () => {})
fastify.get('/hello/:world', () => {})
Expand All @@ -51,7 +51,7 @@ test('pretty print - parametric routes', t => {
test('pretty print - mixed parametric routes', t => {
t.plan(2)

const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.get('/test', () => {})
fastify.get('/test/:hello', () => {})
fastify.post('/test/:hello', () => {})
Expand All @@ -75,7 +75,7 @@ test('pretty print - mixed parametric routes', t => {
test('pretty print - wildcard routes', t => {
t.plan(2)

const fastify = Fastify()
const fastify = Fastify({ exposeHeadRoutes: false })
fastify.get('/test', () => {})
fastify.get('/test/*', () => {})
fastify.get('/hello/*', () => {})
Expand Down
3 changes: 2 additions & 1 deletion test/route-prefix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,8 @@ test('matches only /prefix/ with a / route - prefixTrailingSlash: "slash", igno
test('calls onRoute only once when prefixing', async t => {
t.plan(1)
const fastify = Fastify({
ignoreTrailingSlash: false
ignoreTrailingSlash: false,
exposeHeadRoutes: false
})

let onRouteCalled = 0
Expand Down