Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
file_caching_test.js 14.70 KiB
import request from 'supertest'
import { generateSimpleViteManifest, mockApp, mockConfig, mockFetch, mockRedis } from './util.js'
import fs from 'fs'
import { expect } from 'chai'
import * as td from 'testdouble'
import RedisMock from 'ioredis-mock'
import sinon from 'sinon'
import zlib from 'node:zlib'

const image = fs.readFileSync('./spec/media/image.png')
const imageStat = fs.statSync('./spec/media/image.png')
const sandbox = sinon.createSandbox()

describe('File caching service', function () {
  let app
  let redis

  beforeEach(async function () {
    let count = 0
    mockConfig({ urls: ['http://ui-server/'] })
    redis = mockRedis()
    mockFetch({
      'http://ui-server': {
        '/manifest.json': generateSimpleViteManifest({
          'example.js': { imports: ['test.txt'] },
          'test.txt': { },
          'main.css': {},
          'index.html': {
            file: 'index.html.js',
            isEntry: true,
            imports: ['example.js'],
            css: ['main.css']
          },
          'image.png': {}
        }),
        '/example.js': () => new Response('this is example', { headers: { 'content-type': 'application/javascript' } }),
        '/test.txt': () => new Response('this is test', { headers: { 'content-type': 'text/plain' } }),
        '/index.html.js': () => new Response('this is index.html.js', { headers: { 'content-type': 'application/javascript' } }),
        '/index.html': () => new Response('<html><head></head><body>it\'s me</body></html>', { headers: { 'content-type': 'text/html' } }),
        '/main.css': () => new Response('.foo { color: #000; }', { headers: { 'content-type': 'text/css' } }),
        '/favicon.ico': 'not really a favicon, though',
        '/test.svg': () => {
          if (count > 0) {
            return new Response(null, { status: 404 })
          }
          count++
          return new Response('<svg></svg>', { headers: { 'content-type': 'image/svg' } })
        },
        '/image.png': () => {
          return new Response(image, {
            headers: {
              'Content-Type': 'image/png',
              'Content-Length': imageStat.size.toString()
            }
          })
        }
      }
    })
    app = await mockApp()
  })

  afterEach(async function () {
    await new RedisMock().flushdb()
    td.reset()
  })

  it('serves files defined in manifest.json file', async function () {
    const response = await request(app.server).get('/example.js')
    expect(response.statusCode).to.equal(200)
    expect(response.headers['content-type']).to.equal('application/javascript')