File size: 2,797 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { parseCustomHeaders } from './customHeaderUtils.js';
describe('parseCustomHeaders', () => {
beforeEach(() => {
vi.resetAllMocks();
});
it('should return an empty object if input is undefined', () => {
expect(parseCustomHeaders(undefined)).toEqual({});
});
it('should return an empty object if input is empty string', () => {
expect(parseCustomHeaders('')).toEqual({});
});
it('should parse a single header correctly', () => {
const input = 'Authorization: Bearer abc123';
expect(parseCustomHeaders(input)).toEqual({
Authorization: 'Bearer abc123',
});
});
it('should parse multiple headers separated by commas', () => {
const input =
'Authorization: Bearer abc123, Content-Type: application/json';
expect(parseCustomHeaders(input)).toEqual({
Authorization: 'Bearer abc123',
'Content-Type': 'application/json',
});
});
it('should ignore entries without colon', () => {
const input = 'Authorization Bearer abc123, Content-Type: application/json';
expect(parseCustomHeaders(input)).toEqual({
'Content-Type': 'application/json',
});
});
it('should trim whitespace around names and values', () => {
const input =
' Authorization : Bearer abc123 , Content-Type : application/json ';
expect(parseCustomHeaders(input)).toEqual({
Authorization: 'Bearer abc123',
'Content-Type': 'application/json',
});
});
it('should handle headers with colons in the value', () => {
const input = 'X-Custom: value:with:colons, Authorization: Bearer xyz';
expect(parseCustomHeaders(input)).toEqual({
'X-Custom': 'value:with:colons',
Authorization: 'Bearer xyz',
});
});
it('should skip headers with empty name', () => {
const input = ': no-name, Authorization: Bearer abc';
expect(parseCustomHeaders(input)).toEqual({
Authorization: 'Bearer abc',
});
});
it('should skip completely empty entries', () => {
const input = ', , Authorization: Bearer abc';
expect(parseCustomHeaders(input)).toEqual({
Authorization: 'Bearer abc',
});
});
it('should handle Authorization Bearer with different casing', () => {
const input = 'authorization: Bearer token123';
expect(parseCustomHeaders(input)).toEqual({
authorization: 'Bearer token123',
});
});
it('should handle values with commas correctly', () => {
const input = 'X-Header: value,with,commas, Authorization: Bearer abc';
expect(parseCustomHeaders(input)).toEqual({
'X-Header': 'value,with,commas',
Authorization: 'Bearer abc',
});
});
});
|