toWebHeaders copied every response's headers into a Headers object one by
one, so set-cookie arrays survive. Deno and Bun build native headers from a
record directly, and the JS copy cost 5-8% on every Deno request (bisected to
50010da). A record holding only strings is now returned as it is; anything
else still goes through Headers.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
64 lines
2.6 KiB
TypeScript
64 lines
2.6 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { mergeInjectedHeaders, joinListHeaders, toWebHeaders } from '../src/http/headers';
|
|
|
|
describe('mergeInjectedHeaders', () => {
|
|
it('makes injected keys authoritative regardless of handler casing', () => {
|
|
const out = mergeInjectedHeaders(
|
|
{ 'Content-Type': 'text/html', 'X-Content-Type-Options': 'sniffme' },
|
|
{ 'x-content-type-options': 'nosniff' },
|
|
);
|
|
expect(out['x-content-type-options']).toBe('nosniff');
|
|
expect(Object.keys(out).filter((k) => k.toLowerCase() === 'x-content-type-options')).toHaveLength(1);
|
|
expect(out['Content-Type']).toBe('text/html'); // untouched handler header kept
|
|
});
|
|
|
|
it('merges Vary from handler and injected', () => {
|
|
const out = mergeInjectedHeaders({ Vary: 'Accept' }, { vary: 'Origin' });
|
|
expect(out['vary'].toLowerCase()).toContain('accept');
|
|
expect(out['vary'].toLowerCase()).toContain('origin');
|
|
});
|
|
|
|
it('preserves handler Vary when no injected Vary', () => {
|
|
const out = mergeInjectedHeaders({ Vary: 'Accept' }, {});
|
|
expect(out['vary']).toBe('Accept');
|
|
});
|
|
});
|
|
|
|
describe('array header values (D11)', () => {
|
|
it('keeps a handler array intact and still merges vary', () => {
|
|
const cookies = ['a=1', 'b=2'];
|
|
const out = mergeInjectedHeaders({ 'set-cookie': cookies, Vary: ['Accept', 'Accept-Language'] }, { vary: 'Origin' });
|
|
expect(out['set-cookie']).toEqual(['a=1', 'b=2']);
|
|
expect(out['vary']).toBe('Accept, Accept-Language, Origin');
|
|
});
|
|
|
|
it('joinListHeaders joins every array but set-cookie, and returns the input when there is none', () => {
|
|
const plain = { a: '1' };
|
|
expect(joinListHeaders(plain)).toBe(plain);
|
|
expect(joinListHeaders({ 'Set-Cookie': ['a=1', 'b=2'], 'x-list': ['a', 'b'] })).toEqual({
|
|
'Set-Cookie': ['a=1', 'b=2'],
|
|
'x-list': 'a, b',
|
|
});
|
|
});
|
|
|
|
it('toWebHeaders returns a record of plain strings untouched', () => {
|
|
const record = { 'content-type': 'application/json', vary: 'origin' };
|
|
expect(toWebHeaders(record)).toBe(record);
|
|
});
|
|
|
|
it('toWebHeaders drops undefined values', () => {
|
|
const h = toWebHeaders({ 'content-type': 'text/plain', 'x-gone': undefined }) as Headers;
|
|
expect(h.has('x-gone')).toBe(false);
|
|
expect(h.get('content-type')).toBe('text/plain');
|
|
});
|
|
|
|
it('toWebHeaders appends one set-cookie per element', () => {
|
|
const h = toWebHeaders({
|
|
'set-cookie': ['a=1; Expires=Tue, 01 Jan 2030 00:00:00 GMT', 'b=2'],
|
|
'x-list': ['a', 'b'],
|
|
}) as Headers;
|
|
expect(h.getSetCookie()).toEqual(['a=1; Expires=Tue, 01 Jan 2030 00:00:00 GMT', 'b=2']);
|
|
expect(h.get('x-list')).toBe('a, b');
|
|
});
|
|
});
|