Skip to content

Commit

Permalink
test: test for array and regex origin options (#77) (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
alemagio authored Aug 5, 2020
1 parent a479093 commit 129e98f
Showing 1 changed file with 68 additions and 1 deletion.
69 changes: 68 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ test('Should not add cors headers other than `vary` when origin is false', t =>
})
})

test('Allow only request from a specifc origin', t => {
test('Allow only request from a specific origin', t => {
t.plan(4)

const fastify = Fastify()
Expand All @@ -503,6 +503,73 @@ test('Allow only request from a specifc origin', t => {
})
})

test('Allow only request from multiple specific origin', t => {
t.plan(8)

const fastify = Fastify()
fastify.register(cors, { origin: ['other.io', 'example.com'] })

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

fastify.inject({
method: 'GET',
url: '/',
headers: { origin: 'other.io' }
}, (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-allow-origin': 'other.io',
vary: 'Origin'
})
})

fastify.inject({
method: 'GET',
url: '/',
headers: { origin: 'foo.com' }
}, (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-allow-origin': false,
vary: 'Origin'
})
})
})

test('Allow only request from a specific origin using regex', t => {
t.plan(4)

const fastify = Fastify()
fastify.register(cors, { origin: new RegExp(/^(example|other)\.com/) })

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

fastify.inject({
method: 'GET',
url: '/',
headers: { origin: 'example.com' }
}, (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-allow-origin': 'example.com',
vary: 'Origin'
})
})
})

test('Disable preflight', t => {
t.plan(7)

Expand Down

0 comments on commit 129e98f

Please sign in to comment.