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

describe('Responses contain custom headers', function () {
  let app

  before(async function () {
    mockConfig({ urls: ['http://ui-server/'] })
    mockRedis()
    mockFetch({
      'http://ui-server': {
        '/manifest.json': generateSimpleViteManifest({
          'example.js': {},
          'main.css': {},
          'index.html': {
            file: 'index.html.js',
            isEntry: true,
            imports: ['example.js'],
            css: ['main.css']
          }
        }),
        '/example.js': () => new Response('this is example', { headers: { 'content-type': 'application/javascript' } }),
        '/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' } })
      }
    })
    mockRedis()
    app = await mockApp()
  })

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

  after(function () {
    td.reset()
  })

  it('index.html has version', async function () {
    const response = await request(app.server).get('/index.html')
    expect(response.statusCode).to.equal(200)
    expect(response.headers.version).to.equal('3215668592')
  })

  it('serves requested version', async function () {
    const response = await request(app.server).get('/index.html.js').set('version', '123456')
    expect(response.statusCode).to.equal(200)
    expect(response.headers.version).to.equal('123456')
    expect(response.headers['latest-version']).to.equal('3215668592')
  })

  it('javascript file contains dependencies', async function () {
    const response = await request(app.server).get('/index.html.js')
    expect(response.statusCode).to.equal(200)
    expect(response.headers.dependencies).to.equal('main.css')
  })

  describe('with different files', function () {
    beforeEach(async function () {
      td.reset()
      await new RedisMock().flushdb()

      mockConfig({ urls: ['http://ui-server/'] })
      mockRedis()
      mockFetch({
        'http://ui-server': {
          '/manifest.json': generateSimpleViteManifest({
            'index.html': {}
          }),
          '/index.html': () => new Response('<html><head></head><body>it\'s me</body></html>', { headers: { 'content-type': 'text/html' } })
        }
      })
      app = await mockApp()
    })

    it('index.html has version', async function () {
      const response = await request(app.server).get('/index.html')
      expect(response.statusCode).to.equal(200)
      // important here is, that it is different than in the test without meta.json
      expect(response.headers.version).to.equal('3961519424')
    })
  })

  describe('with meta.json', function () {
    beforeEach(async function () {
      td.reset()
      await new RedisMock().flushdb()

      mockConfig({ urls: ['http://ui-server/'] })
      mockRedis()
      mockFetch({
        'http://ui-server': {
          '/manifest.json': generateSimpleViteManifest({
            'index.html': {}
          }),
          '/index.html': () => new Response('<html><head></head><body>it\'s me</body></html>', { headers: { 'content-type': 'text/html' } }),
          '/meta.json': { commitSha: '1234567890' }
        }
      })
      app = await mockApp()
    })

    it('index.html has version', async function () {
      const response = await request(app.server).get('/index.html')
      expect(response.statusCode).to.equal(200)
      // important here is, that it is different than in the test without meta.json
      expect(response.headers.version).to.equal('1487554813')
    })
  })

  describe('with different meta.json', function () {
    beforeEach(async function () {
      td.reset()
      await new RedisMock().flushdb()

      mockConfig({ urls: ['http://ui-server/'] })
      mockRedis()
      mockFetch({
        'http://ui-server': {
          '/manifest.json': generateSimpleViteManifest({
            'index.html': {}
          }),
          '/index.html': () => new Response('<html><head></head><body>it\'s me</body></html>', { headers: { 'content-type': 'text/html' } }),
          '/meta.json': { commitSha: '0987654321' }
        }
      })
      app = await mockApp()
    })

    it('index.html has version', async function () {
      const response = await request(app.server).get('/index.html')
      expect(response.statusCode).to.equal(200)
      // important here is, that it is different than in the test without meta.json
      expect(response.headers.version).to.equal('319344871')
    })
  })
})