import { describe, expect, test } from 'vitest'
import headerSanitize from '../src/server/header-blacklist.js'

describe('Header black-list testing', () => {
  test('All headers invalid by name', () => {
    const headers = headerSanitize({
      origin: 'http://transloadit.com',
      'Accept-Charset': '...',
      'content-Length': 1234,
    })

    expect(headers).toEqual({})
  })

  test('All headers invalid by regex', () => {
    const headers = headerSanitize({
      'Proxy-header-fake': 'proxy-header-fake',
      'proxy-header-fake-lower': 'proxy-header-fake-lower',
      'proxy-': 'proxy-header-fake-empty',
      'Sec-': 'sec-header-empty',
      'sec-': 'sec-lower-header-empty',
      'Sec-header-fake': 'sec-header-fake',
      'sec-header-fake': 'sec-header-fake',
    })
    expect(headers).toEqual({})
  })

  test('All headers invalid by name and regex', () => {
    const headers = headerSanitize({
      'Proxy-header-fake': 'proxy-header-fake',
      'Sec-header-fake': 'sec-header-fake',
    })
    expect(headers).toEqual({})
  })

  test('Returning only allowed headers', () => {
    const headers = headerSanitize({
      Authorization: 'Basic Xxxxxx',
      'Content-Type': 'application/json',
      'Content-Length': 1234,
      Expires: 'Wed, 21 Oct 2015 07:28:00 GMT',
      Origin: 'http://transloadit.com',
    })
    expect(Object.keys(headers)).toHaveLength(3)
    expect(headers).toHaveProperty('Authorization')
    expect(headers).toHaveProperty('Content-Type')
    expect(headers).toHaveProperty('Expires')
  })

  test('Return empty object when headers is not an object', () => {
    expect(headerSanitize({})).toEqual({})
    // @ts-expect-error testing invalid input
    expect(headerSanitize(null)).toEqual({})
    expect(headerSanitize(undefined)).toEqual({})
    // @ts-expect-error testing invalid input
    expect(headerSanitize('Authorization: Basic 1234')).toEqual({})
    // @ts-expect-error testing invalid input
    expect(headerSanitize(['Authorization', 'Basic 1234'])).toEqual({})
  })
})
