Skip to content
Snippets Groups Projects
Commit 9b33933e authored by richard.petersen's avatar richard.petersen :sailboat:
Browse files

Fix: Binary handling without .buffer()

parent 458e66c6
No related branches found
No related tags found
Loading
......@@ -3,6 +3,10 @@ import mockfs from 'mock-fs'
import request from 'supertest'
import { createApp } from '../src/createApp'
import { createMockServer, generateSimpleViteManifest, getRandomPort } from './util.js'
import fs from 'fs'
const image = fs.readFileSync('./spec/media/image.png')
const imageStat = fs.statSync('./spec/media/image.png')
describe('File caching service', () => {
let app
......@@ -35,15 +39,23 @@ describe('File caching service', () => {
isEntry: true,
imports: ['example.js'],
css: ['main.css']
}
},
'image.png': {}
}),
'/example.js': (req, res) => res.setHeader('content-type', 'application/javascript').status(200).send('this is example'),
'/test.txt': (req, res) => res.setHeader('content-type', 'text/plain').status(200).send('this is test'),
'/index.html.js': (req, res) => res.setHeader('content-type', 'application/javascript').status(200).send('this is index.html.js'),
'/index.html': (req, res) => res.setHeader('content-type', 'text/html').status(200).send('<html><head></head><body>it\'s me</body></html>'),
'/main.css': (req, res) => res.setHeader('content-type', 'text/css').status(200).send('.foo { color: #000; }'),
'/favicon.ico': 'not really a favicon, though'
'/favicon.ico': 'not really a favicon, though',
'/image.png': (req, res) => {
// need to do this like this, because jest messes up file system within tests
res.set({
'Content-Type': 'image/png',
'Content-Length': imageStat.size
})
res.end(image)
}
})
await request(app).get('/ready')
})
......@@ -96,4 +108,10 @@ describe('File caching service', () => {
const response = await request(app).get('/unknown-file.txt')
expect(response.statusCode).toBe(404)
})
it('serves binary files', async () => {
const response = await request(app).get('/image.png')
expect(response.statusCode).toBe(200)
expect(response.body.length === imageStat.size)
})
})
spec/media/image.png

10.1 KiB

......@@ -9,7 +9,7 @@ const logger = getLogger()
async function fetchData (path, baseUrl) {
const response = await fetch(new URL(path, baseUrl))
if (!response.ok) throw new Error(`Error fetching file: ${path}`)
const content = await response.text()
const content = Buffer.from(await response.arrayBuffer())
const sha256Sum = crypto.createHash('sha256').update(content).digest('base64')
return [path, {
'content-type': response.headers.get('content-type'),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment