Skip to content

Commit

Permalink
test: full code coverage (#77) (#82)
Browse files Browse the repository at this point in the history
* test: full code coverage (#77)

* test: moved vary to its own module (#77)

* test: added vary module
  • Loading branch information
alemagio authored Aug 5, 2020
1 parent 129e98f commit 67f0007
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 15 deletions.
15 changes: 1 addition & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const fp = require('fastify-plugin')
const append = require('vary').append
const vary = require('./vary')

function fastifyCors (fastify, opts, next) {
const {
Expand Down Expand Up @@ -156,19 +156,6 @@ function fastifyCors (fastify, opts, next) {
next()
}

// https://github.com/fastify/fastify-sensible/blob/master/lib/vary.js
function vary (reply, field) {
var value = reply.getHeader('Vary') || ''
var header = Array.isArray(value)
? value.join(', ')
: String(value)

// set new header
if ((value = append(header, field))) {
reply.header('Vary', value)
}
}

module.exports = fp(fastifyCors, {
fastify: '3.x',
name: 'fastify-cors'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint:standard": "standard",
"lint:fix": "standard --fix",
"lint:typescript": "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin test/types/*.ts",
"unit": "tap test/*.test.js",
"unit": "tap --100 test/*.test.js",
"typescript": "tsd",
"test": "npm run lint && npm run unit && npm run typescript",
"coverage": "tap --cov --coverage-report=html test"
Expand Down
92 changes: 92 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,95 @@ test('Should always add vary header to `Origin` by default', t => {
})
})
})

test('Should always add vary header to `Origin` by default (vary is array)', t => {
t.plan(4)

const fastify = Fastify()

// Mock getHeader function
fastify.decorateReply('getHeader', (name) => ['foo', 'bar'])

fastify.register(cors)

fastify.get('/', (req, reply) => {
reply.send('ok')
})

fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
delete res.headers.date
t.strictEqual(res.statusCode, 200)
t.strictEqual(res.payload, 'ok')
t.match(res.headers, {
vary: 'foo, bar, Origin'
})
})
})

test('Allow only request from with specific methods', t => {
t.plan(3)

const fastify = Fastify()
fastify.register(cors, { methods: ['GET', 'POST'] })

fastify.options('/', (req, reply) => {
reply.send('ok')
})

fastify.inject({
method: 'OPTIONS',
url: '/'
}, (err, res) => {
t.error(err)
delete res.headers.date
t.strictEqual(res.statusCode, 204)
t.match(res.headers, {
'access-control-allow-methods': 'GET, POST',
vary: 'Origin'
})
})
})

test('Allow only request from with specific headers', t => {
t.plan(7)

const fastify = Fastify()
fastify.register(cors, {
allowedHeaders: 'foo',
exposedHeaders: 'bar'
})

fastify.get('/', (req, reply) => {
reply.send('ok')
})

fastify.inject({
method: 'OPTIONS',
url: '/'
}, (err, res) => {
t.error(err)
delete res.headers.date
t.strictEqual(res.statusCode, 204)
t.match(res.headers, {
'access-control-allow-headers': 'foo',
vary: 'Origin'
})
})

fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
delete res.headers.date
t.strictEqual(res.statusCode, 200)
t.strictEqual(res.payload, 'ok')
t.match(res.headers, {
'access-control-expose-headers': 'bar'
})
})
})
20 changes: 20 additions & 0 deletions test/vary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const { test } = require('tap')
const vary = require('../vary')

test('Should not set reply header if none is passed', t => {
t.plan(1)

const replyMock = {
getHeader (name) {
return []
},
header (name, value) {
t.fail('Should not be here')
}
}

vary(replyMock, [])
t.pass()
})
16 changes: 16 additions & 0 deletions vary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const append = require('vary').append

// https://github.com/fastify/fastify-sensible/blob/master/lib/vary.js
module.exports = function vary (reply, field) {
var value = reply.getHeader('Vary') || ''
var header = Array.isArray(value)
? value.join(', ')
: String(value)

// set new header
if ((value = append(header, field))) {
reply.header('Vary', value)
}
}

0 comments on commit 67f0007

Please sign in to comment.