|
1 | | -import { guard, isEdgeRuntime, polyfilledOrNative } from './test-if' |
| 1 | +describe('headers', () => { |
| 2 | + it.each([ |
| 3 | + ['host', 'vercel.com'], |
| 4 | + ['content-length', '1234'], |
| 5 | + ['content-type', 'application/json'], |
| 6 | + ['transfer-encoding', 'chunked'], |
| 7 | + ['connection', 'keep-alive'], |
| 8 | + ['keep-alive', 'timeout=5'], |
| 9 | + ['upgrade', 'websocket'], |
| 10 | + ['expect', '100-continue'], |
| 11 | + ])("sets '%s' header in the constructor", async (name, value) => { |
| 12 | + const headers = new Headers({ [name]: value }) |
| 13 | + expect(headers.get(name)).toBe(value) |
| 14 | + }) |
2 | 15 |
|
3 | | -guard(it, polyfilledOrNative)( |
4 | | - 'sets header calling Headers constructor', |
5 | | - async () => { |
| 16 | + it('sets header calling Headers constructor', async () => { |
6 | 17 | const headers = new Headers() |
7 | 18 | headers.set('cookie', 'hello=world') |
8 | 19 | expect(headers.get('cookie')).toBe('hello=world') |
9 | | - }, |
10 | | -) |
11 | | - |
12 | | -guard(it, polyfilledOrNative)('multiple headers', async () => { |
13 | | - const headers = new Headers() |
14 | | - headers.append('set-cookie', 'foo=chocochip') |
15 | | - headers.append('set-cookie', 'bar=chocochip') |
16 | | - expect(headers.get('set-cookie')).toBe('foo=chocochip, bar=chocochip') |
17 | | - expect([...headers]).toEqual([ |
18 | | - ['set-cookie', 'foo=chocochip'], |
19 | | - ['set-cookie', 'bar=chocochip'], |
20 | | - ]) |
21 | | -}) |
22 | | - |
23 | | -guard(describe, isEdgeRuntime())('getAll', () => { |
24 | | - test('on set-cookie', () => { |
25 | | - const headers = new Headers() |
26 | | - headers.append('set-cookie', 'a=1') |
27 | | - headers.append('set-cookie', 'b=2') |
28 | | - expect(headers.getSetCookie()).toEqual(['a=1', 'b=2']) |
29 | | - expect(headers.getAll?.('set-cookie')).toEqual(['a=1', 'b=2']) |
30 | 20 | }) |
31 | 21 |
|
32 | | - test('on any other name', () => { |
| 22 | + it('multiple headers', async () => { |
33 | 23 | const headers = new Headers() |
34 | | - expect(() => headers.getAll?.('other')).toThrow(/getAll can only be used/) |
| 24 | + headers.append('set-cookie', 'foo=chocochip') |
| 25 | + headers.append('set-cookie', 'bar=chocochip') |
| 26 | + expect(headers.get('set-cookie')).toBe('foo=chocochip, bar=chocochip') |
| 27 | + expect([...headers]).toEqual([ |
| 28 | + ['set-cookie', 'foo=chocochip'], |
| 29 | + ['set-cookie', 'bar=chocochip'], |
| 30 | + ]) |
35 | 31 | }) |
36 | | -}) |
37 | 32 |
|
38 | | -guard(describe, polyfilledOrNative)('iterators', () => { |
39 | | - const generate = () => { |
40 | | - const headers = new Headers() |
41 | | - headers.append('a', '1') |
42 | | - headers.append('b', '2') |
43 | | - headers.append('set-cookie', 'c=3') |
44 | | - headers.append('set-cookie', 'd=4') |
45 | | - return headers |
46 | | - } |
| 33 | + describe('iterators', () => { |
| 34 | + const generate = () => { |
| 35 | + const headers = new Headers() |
| 36 | + headers.append('a', '1') |
| 37 | + headers.append('b', '2') |
| 38 | + headers.append('set-cookie', 'c=3') |
| 39 | + headers.append('set-cookie', 'd=4') |
| 40 | + return headers |
| 41 | + } |
47 | 42 |
|
48 | | - test('#Symbol.iterator', () => { |
49 | | - const entries = [...generate()] |
50 | | - expect(entries).toEqual([ |
51 | | - ['a', '1'], |
52 | | - ['b', '2'], |
53 | | - ['set-cookie', 'c=3'], |
54 | | - ['set-cookie', 'd=4'], |
55 | | - ]) |
56 | | - }) |
| 43 | + test('#Symbol.iterator', () => { |
| 44 | + const entries = [...generate()] |
| 45 | + expect(entries).toEqual([ |
| 46 | + ['a', '1'], |
| 47 | + ['b', '2'], |
| 48 | + ['set-cookie', 'c=3'], |
| 49 | + ['set-cookie', 'd=4'], |
| 50 | + ]) |
| 51 | + }) |
57 | 52 |
|
58 | | - test('#entries', () => { |
59 | | - const entries = [...generate().entries()] |
60 | | - expect(entries).toEqual([ |
61 | | - ['a', '1'], |
62 | | - ['b', '2'], |
63 | | - ['set-cookie', 'c=3'], |
64 | | - ['set-cookie', 'd=4'], |
65 | | - ]) |
66 | | - }) |
| 53 | + test('#entries', () => { |
| 54 | + const entries = [...generate().entries()] |
| 55 | + expect(entries).toEqual([ |
| 56 | + ['a', '1'], |
| 57 | + ['b', '2'], |
| 58 | + ['set-cookie', 'c=3'], |
| 59 | + ['set-cookie', 'd=4'], |
| 60 | + ]) |
| 61 | + }) |
67 | 62 |
|
68 | | - test('#values', () => { |
69 | | - const values = [...generate().values()] |
70 | | - expect(values).toEqual(['1', '2', 'c=3', 'd=4']) |
| 63 | + test('#values', () => { |
| 64 | + const values = [...generate().values()] |
| 65 | + expect(values).toEqual(['1', '2', 'c=3', 'd=4']) |
| 66 | + }) |
71 | 67 | }) |
72 | 68 | }) |
0 commit comments