From d79f7f1216600e325b8d7ca08fcc2858304c0159 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Fri, 21 Jul 2023 12:53:38 +0200 Subject: [PATCH] test: cors headers sent when payload is stream --- test/cors.test.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/cors.test.js b/test/cors.test.js index 5110126..074b133 100644 --- a/test/cors.test.js +++ b/test/cors.test.js @@ -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) @@ -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)