From 5c422b749785503762523f0cea32be0659139c3f Mon Sep 17 00:00:00 2001 From: alemagio Date: Wed, 5 Aug 2020 13:09:12 +0200 Subject: [PATCH] test: full code coverage (#77) --- index.js | 2 + package.json | 2 +- test/index.test.js | 92 ++++++++++++++++++++++++++++++++++++++++++++++ test/vary.test.js | 20 ++++++++++ 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 test/vary.test.js diff --git a/index.js b/index.js index 7879ad6..33ef620 100644 --- a/index.js +++ b/index.js @@ -173,3 +173,5 @@ module.exports = fp(fastifyCors, { fastify: '3.x', name: 'fastify-cors' }) + +module.exports.vary = vary diff --git a/package.json b/package.json index fa79937..819a419 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/index.test.js b/test/index.test.js index 7c6253c..0a1a59a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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' + }) + }) +}) diff --git a/test/vary.test.js b/test/vary.test.js new file mode 100644 index 0000000..797381d --- /dev/null +++ b/test/vary.test.js @@ -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() +})