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: request set headers #2817

Merged
merged 11 commits into from
Aug 9, 2021
19 changes: 18 additions & 1 deletion docs/Request.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Request is a core Fastify object containing the following fields:
- `query` - the parsed querystring
- `body` - the body
- `params` - the params matching the URL
- `headers` - the headers
- [`headers`](#headers) - the headers getter and setter
- `raw` - the incoming HTTP request from Node core
- `req` *(deprecated, use `.raw` instead)* - the incoming HTTP request from Node core
- `server` - The Fastify server instance, scoped to the current [encapsulation context](Encapsulation.md)
Expand All @@ -24,6 +24,20 @@ Request is a core Fastify object containing the following fields:
- `connection` - Deprecated, use `socket` instead. The underlying connection of the incoming request.
- `socket` - the underlying connection of the incoming request

### Headers

The `request.headers` is a getter that return an Object with the headers of the incoming request.
You can set custom headers like this:

```js
request.headers = {
'foo': 'bar',
'baz': 'qux'
}
```

This operation will add to the request headers the new values that can be read calling `request.headers.bar`.
Moreover, you can still access the standard request's headers with the `request.raw.headers` property.

```js
fastify.post('/:params', options, function (request, reply) {
Expand All @@ -38,6 +52,9 @@ fastify.post('/:params', options, function (request, reply) {
console.log(request.ips)
console.log(request.hostname)
console.log(request.protocol)
console.log(request.url)
console.log(request.routerMethod)
console.log(request.routerPath)
request.log.info('some info')
})
```
6 changes: 6 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,13 @@ Object.defineProperties(Request.prototype, {
},
headers: {
get () {
if (this.additionalHeaders) {
return Object.assign({}, this.raw.headers, this.additionalHeaders)
}
return this.raw.headers
},
set (headers) {
this.additionalHeaders = headers
}
},
server: {
Expand Down
32 changes: 32 additions & 0 deletions test/schema-special-usage.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { test } = require('tap')
const Joi = require('@hapi/joi')
const AJV = require('ajv')
const S = require('fluent-json-schema')
const Fastify = require('..')
Expand Down Expand Up @@ -748,3 +749,34 @@ test('multiple refs with the same ids', t => {
t.same(res.json(), { hello: 'world' })
})
})

test('JOI validation overwrite request headers', t => {
t.plan(3)
const schemaValidator = ({ schema }) => data => {
const validationResult = schema.validate(data)
return validationResult
}

const fastify = Fastify()
fastify.setValidatorCompiler(schemaValidator)

fastify.get('/', {
schema: {
headers: Joi.object({
'user-agent': Joi.string().required(),
host: Joi.string().required()
})
}
}, (request, reply) => {
reply.send(request.headers)
})

fastify.inject('/', (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.json(), {
'user-agent': 'lightMyRequest',
host: 'localhost:80'
})
})
})