Skip to content

Commit 191e00a

Browse files
mrmlncsindresorhus
authored andcommittedNov 3, 2018
Don't override headers defined in the url argument when it's an object (#633)
Fixes #632
·
v14.4.7v9.3.1
1 parent 311b184 commit 191e00a

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed
 

‎source/normalize-arguments.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ const preNormalize = (options, defaults) => {
8787
};
8888

8989
const normalize = (url, options, defaults) => {
90-
if (is.plainObject(url) && Reflect.has(url, 'url')) {
91-
options = url;
92-
url = url.url;
90+
if (is.plainObject(url)) {
91+
options = {...url, ...options};
92+
url = options.url || {};
9393
delete options.url;
9494
}
9595

@@ -122,12 +122,8 @@ const normalize = (url, options, defaults) => {
122122
url = urlToOptions(url);
123123
}
124124

125-
options = {
126-
path: '',
127-
...url,
128-
protocol: url.protocol || 'https:', // Override both null/undefined with default protocol
129-
...options
130-
};
125+
// Override both null/undefined with default protocol
126+
options = merge({path: ''}, url, {protocol: url.protocol || 'https:'}, options);
131127

132128
const {baseUrl} = options;
133129
Object.defineProperty(options, 'baseUrl', {

‎test/headers.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ test('do not override accept-encoding', async t => {
4444
t.is(headers['accept-encoding'], 'gzip');
4545
});
4646

47+
test('do not remove user headers from `url` object argument', async t => {
48+
const headers = (await got({
49+
hostname: s.host,
50+
port: s.port,
51+
json: true,
52+
protocol: 'http:',
53+
headers: {
54+
'X-Request-Id': 'value'
55+
}
56+
})).body;
57+
58+
t.is(headers.accept, 'application/json');
59+
t.is(headers['user-agent'], `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`);
60+
t.is(headers['accept-encoding'], 'gzip, deflate');
61+
t.is(headers['x-request-id'], 'value');
62+
});
63+
4764
test('do not set accept-encoding header when decompress options is false', async t => {
4865
const {body: headers} = await got(s.url, {
4966
json: true,

0 commit comments

Comments
 (0)
Please sign in to comment.