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: cors headers sent when payload is stream #260

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Changes from all commits
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
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