Skip to content

Commit

Permalink
test: full code coverage (fastify#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
alemagio committed Aug 5, 2020
1 parent d79926b commit dd09bc9
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,5 @@ module.exports = fp(fastifyCors, {
fastify: '3.x',
name: 'fastify-cors'
})

module.exports.vary = vary
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()
})

0 comments on commit dd09bc9

Please sign in to comment.