Skip to content

Commit

Permalink
test: cors headers sent when payload is stream (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Jul 24, 2023
1 parent d377285 commit 533ed08
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/cors.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict'

const { test } = require('tap')
const { createReadStream, statSync, readFileSync } = require('fs')
const Fastify = require('fastify')
const cors = require('../')
const { resolve } = require('path')

test('Should add cors headers', t => {
t.plan(4)
Expand All @@ -28,6 +30,38 @@ test('Should add cors headers', t => {
})
})

test('Should add cors headers when payload is a stream', t => {
t.plan(4)

const fastify = Fastify()
fastify.register(cors)
const filePath = resolve(__dirname, __filename)

fastify.get('/', (req, reply) => {
const stream = createReadStream(filePath)
reply
.type('application/json')
.header('Content-Length', statSync(filePath).size)
.send(stream)
})

const fileContent = readFileSync(filePath, 'utf-8')

fastify.inject({
method: 'GET',
url: '/'
}, (err, res) => {
t.error(err)
delete res.headers.date
t.equal(res.statusCode, 200)
t.equal(res.payload, fileContent)
t.match(res.headers, {
'access-control-allow-origin': '*',
'content-length': statSync(filePath).size
})
})
})

test('Should add cors headers (custom values)', t => {
t.plan(8)

Expand Down

0 comments on commit 533ed08

Please sign in to comment.