|
| 1 | +import { buildOutgoingHttpHeaders } from '../src/utils' |
| 2 | + |
| 3 | +describe('buildOutgoingHttpHeaders', () => { |
| 4 | + it('original content-type is preserved', () => { |
| 5 | + const headers = new Headers({ |
| 6 | + a: 'b', |
| 7 | + 'content-type': 'text/html; charset=UTF-8', |
| 8 | + }) |
| 9 | + const result = buildOutgoingHttpHeaders(headers) |
| 10 | + expect(result).toEqual({ |
| 11 | + a: 'b', |
| 12 | + 'content-type': 'text/html; charset=UTF-8', |
| 13 | + }) |
| 14 | + }) |
| 15 | + |
| 16 | + it('multiple set-cookie', () => { |
| 17 | + const headers = new Headers() |
| 18 | + headers.append('set-cookie', 'a') |
| 19 | + headers.append('set-cookie', 'b') |
| 20 | + const result = buildOutgoingHttpHeaders(headers) |
| 21 | + expect(result).toEqual({ |
| 22 | + 'set-cookie': ['a', 'b'], |
| 23 | + 'content-type': 'text/plain; charset=UTF-8', |
| 24 | + }) |
| 25 | + }) |
| 26 | + |
| 27 | + it('Headers', () => { |
| 28 | + const headers = new Headers({ |
| 29 | + a: 'b', |
| 30 | + }) |
| 31 | + const result = buildOutgoingHttpHeaders(headers) |
| 32 | + expect(result).toEqual({ |
| 33 | + a: 'b', |
| 34 | + 'content-type': 'text/plain; charset=UTF-8', |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + it('Record<string, string>', () => { |
| 39 | + const headers = { |
| 40 | + a: 'b', |
| 41 | + 'Set-Cookie': 'c', // case-insensitive |
| 42 | + } |
| 43 | + const result = buildOutgoingHttpHeaders(headers) |
| 44 | + expect(result).toEqual({ |
| 45 | + a: 'b', |
| 46 | + 'set-cookie': ['c'], |
| 47 | + 'content-type': 'text/plain; charset=UTF-8', |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + it('Record<string, string>[]', () => { |
| 52 | + const headers: HeadersInit = [['a', 'b']] |
| 53 | + const result = buildOutgoingHttpHeaders(headers) |
| 54 | + expect(result).toEqual({ |
| 55 | + a: 'b', |
| 56 | + 'content-type': 'text/plain; charset=UTF-8', |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('null', () => { |
| 61 | + const result = buildOutgoingHttpHeaders(null) |
| 62 | + expect(result).toEqual({ |
| 63 | + 'content-type': 'text/plain; charset=UTF-8', |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + it('undefined', () => { |
| 68 | + const result = buildOutgoingHttpHeaders(undefined) |
| 69 | + expect(result).toEqual({ |
| 70 | + 'content-type': 'text/plain; charset=UTF-8', |
| 71 | + }) |
| 72 | + }) |
| 73 | +}) |
0 commit comments