Skip to content

Commit c77513c

Browse files
fix: allow specifying the user-agent header for outgoing requests (#1287)
Co-authored-by: Filip Skokan <panva.ip@gmail.com>
1 parent 503f313 commit c77513c

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

lib/helpers/request.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default async function request(options) {
1919
signal = AbortSignal.timeout(2500),
2020
agent = options.url.protocol === 'http:' ? http.globalAgent : https.globalAgent,
2121
dnsLookup = dns.lookup,
22+
'user-agent': userAgent = undefined,
2223
} = instance(this).configuration('httpOptions')(new URL(options.url));
2324
const helperOptions = pickBy({ signal, agent, dnsLookup }, Boolean);
2425

@@ -34,12 +35,12 @@ export default async function request(options) {
3435
throw new TypeError('"dnsLookup" http request option must be a function');
3536
}
3637

37-
if (helperOptions['user-agent'] !== undefined && typeof helperOptions['user-agent'] !== 'string') {
38+
if (userAgent !== undefined && typeof userAgent !== 'string') {
3839
throw new TypeError('"user-agent" http request option must be a string');
3940
}
4041

4142
// eslint-disable-next-line no-param-reassign
42-
options.headers['user-agent'] = helperOptions['user-agent'];
43+
options.headers['user-agent'] = userAgent;
4344

4445
return got({
4546
...options,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import merge from 'lodash/merge.js';
2+
3+
import getConfig from '../../default.config.js';
4+
5+
const config = getConfig();
6+
merge(config, {
7+
httpOptions(url) {
8+
if (url.pathname === '/with-custom-user-agent') {
9+
return { 'user-agent': 'some user agent' };
10+
}
11+
12+
return {};
13+
},
14+
});
15+
16+
export default {
17+
config,
18+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { expect } from 'chai';
2+
import nock from 'nock';
3+
4+
import bootstrap from '../../test_helper.js';
5+
import request from '../../../lib/helpers/request.js';
6+
7+
describe('request helper', () => {
8+
before(bootstrap(import.meta.url));
9+
10+
afterEach(nock.cleanAll);
11+
12+
afterEach(() => {
13+
expect(nock.isDone()).to.be.true;
14+
});
15+
16+
describe('when using custom httpOptions', () => {
17+
it('defaults to not sending the user-agent HTTP header', async function () {
18+
nock('https://www.example.com/', {
19+
badheaders: ['user-agent'],
20+
})
21+
.get('/')
22+
.reply(200);
23+
24+
await request.call(this.provider, { url: 'https://www.example.com' });
25+
});
26+
27+
it("uses a custom 'user-agent' HTTP header", async function () {
28+
nock('https://www.example.com/', {
29+
reqheaders: {
30+
'user-agent': 'some user agent',
31+
},
32+
})
33+
.get('/with-custom-user-agent')
34+
.reply(200);
35+
36+
await request.call(this.provider, { url: 'https://www.example.com/with-custom-user-agent' });
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)