diff --git a/spec/file_caching_test.js b/spec/file_caching_test.js index 6cee9dc38712513079712398aaf74678f0caac58..65359a874245b3d97a59c19bfaa7c8b4d5858d65 100644 --- a/spec/file_caching_test.js +++ b/spec/file_caching_test.js @@ -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) + }) }) diff --git a/spec/media/image.png b/spec/media/image.png new file mode 100644 index 0000000000000000000000000000000000000000..ead835866c1185dfc6ae3cf7dc3bd350b53ade18 Binary files /dev/null and b/spec/media/image.png differ diff --git a/src/files.js b/src/files.js index b4ec6cdbb516fcaf6bcbeb759912122b1e9ac500..f5fc29f2458c43444f267e642de6eaf4e8a5ace6 100644 --- a/src/files.js +++ b/src/files.js @@ -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'),