-
-
Notifications
You must be signed in to change notification settings - Fork 916
/
headers.js
150 lines (130 loc) 路 3.47 KB
/
headers.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import fs from 'fs';
import path from 'path';
import test from 'ava';
import FormData from 'form-data';
import got from '..';
import pkg from '../package';
import {createServer} from './helpers/server';
let s;
test.before('setup', async () => {
s = await createServer();
s.on('/', (req, res) => {
req.resume();
res.end(JSON.stringify(req.headers));
});
await s.listen(s.port);
});
test('user-agent', async t => {
const headers = (await got(s.url, {json: true})).body;
t.is(headers['user-agent'], `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`);
});
test('accept-encoding', async t => {
const headers = (await got(s.url, {json: true})).body;
t.is(headers['accept-encoding'], 'gzip, deflate');
});
test('do not override accept-encoding', async t => {
const headers = (await got(s.url, {
json: true,
headers: {
'accept-encoding': 'gzip'
}
})).body;
t.is(headers['accept-encoding'], 'gzip');
});
test('do not set accept-encoding header when decompress options is false', async t => {
const {body: headers} = await got(s.url, {
json: true,
decompress: false
});
t.false(Reflect.has(headers, 'accept-encoding'));
});
test('accept header with json option', async t => {
let headers = (await got(s.url, {json: true})).body;
t.is(headers.accept, 'application/json');
headers = (await got(s.url, {
headers: {
accept: ''
},
json: true
})).body;
t.is(headers.accept, '');
});
test('host', async t => {
const headers = (await got(s.url, {json: true})).body;
t.is(headers.host, `localhost:${s.port}`);
});
test('transform names to lowercase', async t => {
const headers = (await got(s.url, {
headers: {
'USER-AGENT': 'test'
},
json: true
})).body;
t.is(headers['user-agent'], 'test');
});
test('zero content-length', async t => {
const {body} = await got(s.url, {
headers: {
'content-length': 0
},
body: 'sup'
});
const headers = JSON.parse(body);
t.is(headers['content-length'], '0');
});
test('form-data manual content-type', async t => {
const form = new FormData();
form.append('a', 'b');
const {body} = await got(s.url, {
headers: {
'content-type': 'custom'
},
body: form
});
const headers = JSON.parse(body);
t.is(headers['content-type'], 'custom');
});
test('form-data automatic content-type', async t => {
const form = new FormData();
form.append('a', 'b');
const {body} = await got(s.url, {
body: form
});
const headers = JSON.parse(body);
t.is(headers['content-type'], `multipart/form-data; boundary=${form.getBoundary()}`);
});
test('form-data sets content-length', async t => {
const form = new FormData();
form.append('a', 'b');
const {body} = await got(s.url, {body: form});
const headers = JSON.parse(body);
t.is(headers['content-length'], '157');
});
test('stream as options.body sets content-length', async t => {
const {body} = await got(s.url, {
body: fs.createReadStream(path.join(__dirname, 'fixtures/stream-content-length'))
});
const headers = JSON.parse(body);
t.is(headers['content-length'], '9');
});
test('remove null value headers', async t => {
const {body} = await got(s.url, {
headers: {
'user-agent': null
}
});
const headers = JSON.parse(body);
t.false(Reflect.has(headers, 'user-agent'));
});
test('remove undefined value headers', async t => {
const {body} = await got(s.url, {
headers: {
'user-agent': undefined
}
});
const headers = JSON.parse(body);
t.false(Reflect.has(headers, 'user-agent'));
});
test.after('cleanup', async () => {
await s.close();
});