Skip to content

Commit

Permalink
Header name is case insensitive in the schema definition (#816)
Browse files Browse the repository at this point in the history
* Add failing test

* Fix the simplest case

* Only if properties is set. Link to rfc

* Check for own properties in loops
  • Loading branch information
allevo authored and jsumners committed Mar 1, 2018
1 parent 1b5e476 commit 746e307
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
17 changes: 16 additions & 1 deletion lib/validation.js
Expand Up @@ -29,7 +29,22 @@ function build (context, compile, schemas) {
context.schema = schemas.resolveRefs(context.schema)

if (context.schema.headers) {
context[headersSchema] = compile(context.schema.headers)
// The header keys are case insensitive
// https://tools.ietf.org/html/rfc2616#section-4.2
const headers = context.schema.headers
const headersSchemaLowerCase = {}
for (const k in headers) {
if (headers.hasOwnProperty(k) !== true) continue
headersSchemaLowerCase[k] = headers[k]
}
if (headers.properties) {
const properties = headers.properties
for (const k in properties) {
if (properties.hasOwnProperty(k) !== true) continue
headersSchemaLowerCase.properties[k.toLowerCase()] = properties[k]
}
}
context[headersSchema] = compile(headersSchemaLowerCase)
}

if (context.schema.response) {
Expand Down
11 changes: 8 additions & 3 deletions test/get.test.js
Expand Up @@ -81,6 +81,9 @@ const headersSchema = {
properties: {
'x-test': {
type: 'number'
},
'Y-Test': {
type: 'number'
}
}
}
Expand Down Expand Up @@ -257,14 +260,16 @@ fastify.listen(0, err => {
sget({
method: 'GET',
headers: {
'x-test': 1
'x-test': '1',
'Y-Test': '3'
},
json: true,
url: 'http://localhost:' + fastify.server.address().port + '/headers'
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-length'], '' + body.length)
t.strictEqual(JSON.parse(body)['x-test'], 1)
t.strictEqual(body['x-test'], 1)
t.strictEqual(body['y-test'], 3)
})
})

Expand Down

0 comments on commit 746e307

Please sign in to comment.