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

test: full code coverage (#77) #82

Merged
merged 3 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move vary to its own file so you can test it separately without exposing it to the public?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why we can't use the vary function of the vary module?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that works with node core ServerResponse object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think I should add more tests for the vary function itself?

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()
})