|
| 1 | +import {filterCustomHeaders} from './utilities.js' |
| 2 | +import {describe, expect, test} from 'vitest' |
| 3 | + |
| 4 | +describe('filterCustomHeaders', () => { |
| 5 | + test('allows custom headers that are not blocked', () => { |
| 6 | + const headers = { |
| 7 | + 'x-custom-header': 'custom-value', |
| 8 | + 'x-another-header': 'another-value', |
| 9 | + } |
| 10 | + |
| 11 | + const result = filterCustomHeaders(headers) |
| 12 | + |
| 13 | + expect(result).toEqual({ |
| 14 | + 'x-custom-header': 'custom-value', |
| 15 | + 'x-another-header': 'another-value', |
| 16 | + }) |
| 17 | + }) |
| 18 | + |
| 19 | + test('blocks hop-by-hop headers', () => { |
| 20 | + const headers = { |
| 21 | + connection: 'keep-alive', |
| 22 | + 'keep-alive': 'timeout=5', |
| 23 | + 'transfer-encoding': 'chunked', |
| 24 | + 'x-custom-header': 'custom-value', |
| 25 | + } |
| 26 | + |
| 27 | + const result = filterCustomHeaders(headers) |
| 28 | + |
| 29 | + expect(result).toEqual({ |
| 30 | + 'x-custom-header': 'custom-value', |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + test('blocks proxy-controlled headers', () => { |
| 35 | + const headers = { |
| 36 | + host: 'localhost:3000', |
| 37 | + 'content-type': 'application/json', |
| 38 | + accept: 'application/json', |
| 39 | + 'user-agent': 'Mozilla/5.0', |
| 40 | + authorization: 'Bearer token', |
| 41 | + cookie: 'session=abc', |
| 42 | + 'x-shopify-access-token': 'secret-token', |
| 43 | + 'x-custom-header': 'custom-value', |
| 44 | + } |
| 45 | + |
| 46 | + const result = filterCustomHeaders(headers) |
| 47 | + |
| 48 | + expect(result).toEqual({ |
| 49 | + 'x-custom-header': 'custom-value', |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + test('blocks headers case-insensitively', () => { |
| 54 | + const headers = { |
| 55 | + Connection: 'keep-alive', |
| 56 | + HOST: 'localhost', |
| 57 | + 'Content-Type': 'application/json', |
| 58 | + 'X-Custom-Header': 'custom-value', |
| 59 | + } |
| 60 | + |
| 61 | + const result = filterCustomHeaders(headers) |
| 62 | + |
| 63 | + expect(result).toEqual({ |
| 64 | + 'X-Custom-Header': 'custom-value', |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + test('filters out non-string header values', () => { |
| 69 | + const headers: {[key: string]: string | string[] | undefined} = { |
| 70 | + 'x-custom-header': 'custom-value', |
| 71 | + 'x-array-header': ['value1', 'value2'], |
| 72 | + 'x-undefined-header': undefined, |
| 73 | + } |
| 74 | + |
| 75 | + const result = filterCustomHeaders(headers) |
| 76 | + |
| 77 | + expect(result).toEqual({ |
| 78 | + 'x-custom-header': 'custom-value', |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + test('returns empty object when all headers are blocked', () => { |
| 83 | + const headers = { |
| 84 | + host: 'localhost', |
| 85 | + connection: 'keep-alive', |
| 86 | + 'content-type': 'application/json', |
| 87 | + } |
| 88 | + |
| 89 | + const result = filterCustomHeaders(headers) |
| 90 | + |
| 91 | + expect(result).toEqual({}) |
| 92 | + }) |
| 93 | + |
| 94 | + test('returns empty object for empty input', () => { |
| 95 | + const result = filterCustomHeaders({}) |
| 96 | + |
| 97 | + expect(result).toEqual({}) |
| 98 | + }) |
| 99 | +}) |
0 commit comments