summaryrefslogtreecommitdiff
path: root/packages/integrations/node/test/createOutgoingHttpHeaders.test.js
blob: 2f7063b1c0a884a98fb3f931aaf74e17a32587f8 (plain) (blame)
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
import { expect } from 'chai';

import { createOutgoingHttpHeaders } from '../dist/createOutgoingHttpHeaders.js';

describe('createOutgoingHttpHeaders', () => {
	it('undefined input headers', async () => {
		const result = createOutgoingHttpHeaders(undefined);
		expect(result).to.equal(undefined);
	});

	it('null input headers', async () => {
		const result = createOutgoingHttpHeaders(undefined);
		expect(result).to.equal(undefined);
	});

	it('Empty Headers', async () => {
		const headers = new Headers();
		const result = createOutgoingHttpHeaders(headers);
		expect(result).to.equal(undefined);
	});

	it('Headers with single key', async () => {
		const headers = new Headers();
		headers.append('x-test', 'hello world');
		const result = createOutgoingHttpHeaders(headers);
		expect(result).to.deep.equal({ 'x-test': 'hello world' });
	});

	it('Headers with multiple keys', async () => {
		const headers = new Headers();
		headers.append('x-test1', 'hello');
		headers.append('x-test2', 'world');
		const result = createOutgoingHttpHeaders(headers);
		expect(result).to.deep.equal({ 'x-test1': 'hello', 'x-test2': 'world' });
	});

	it('Headers with multiple values (not set-cookie)', async () => {
		const headers = new Headers();
		headers.append('x-test', 'hello');
		headers.append('x-test', 'world');
		const result = createOutgoingHttpHeaders(headers);
		expect(result).to.deep.equal({ 'x-test': 'hello, world' });
	});

	it('Headers with multiple values (set-cookie special case)', async () => {
		const headers = new Headers();
		headers.append('set-cookie', 'hello');
		headers.append('set-cookie', 'world');
		const result = createOutgoingHttpHeaders(headers);
		expect(result).to.deep.equal({ 'set-cookie': ['hello', 'world'] });
	});

	it('Headers with multiple values (set-cookie case handling)', async () => {
		const headers = new Headers();
		headers.append('Set-cookie', 'hello');
		headers.append('Set-Cookie', 'world');
		const result = createOutgoingHttpHeaders(headers);
		expect(result).to.deep.equal({ 'set-cookie': ['hello', 'world'] });
	});

	it('Headers with all use cases', async () => {
		const headers = new Headers();
		headers.append('x-single', 'single');
		headers.append('x-triple', 'one');
		headers.append('x-triple', 'two');
		headers.append('x-triple', 'three');
		headers.append('Set-cookie', 'hello');
		headers.append('Set-Cookie', 'world');
		const result = createOutgoingHttpHeaders(headers);
		expect(result).to.deep.equal({
			'x-single': 'single',
			'x-triple': 'one, two, three',
			'set-cookie': ['hello', 'world'],
		});
	});
});