Skip to content

Commit

Permalink
fix: allow specifying the user-agent header for outgoing requests (#1287
Browse files Browse the repository at this point in the history
)

Co-authored-by: Filip Skokan <[email protected]>
  • Loading branch information
pvieira-diligent and panva authored Jan 19, 2025
1 parent 503f313 commit c77513c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/helpers/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default async function request(options) {
signal = AbortSignal.timeout(2500),
agent = options.url.protocol === 'http:' ? http.globalAgent : https.globalAgent,
dnsLookup = dns.lookup,
'user-agent': userAgent = undefined,
} = instance(this).configuration('httpOptions')(new URL(options.url));
const helperOptions = pickBy({ signal, agent, dnsLookup }, Boolean);

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

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

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

return got({
...options,
Expand Down
18 changes: 18 additions & 0 deletions test/helpers/request/request.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import merge from 'lodash/merge.js';

import getConfig from '../../default.config.js';

const config = getConfig();
merge(config, {
httpOptions(url) {
if (url.pathname === '/with-custom-user-agent') {
return { 'user-agent': 'some user agent' };
}

return {};
},
});

export default {
config,
};
39 changes: 39 additions & 0 deletions test/helpers/request/request.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect } from 'chai';
import nock from 'nock';

import bootstrap from '../../test_helper.js';
import request from '../../../lib/helpers/request.js';

describe('request helper', () => {
before(bootstrap(import.meta.url));

afterEach(nock.cleanAll);

afterEach(() => {
expect(nock.isDone()).to.be.true;
});

describe('when using custom httpOptions', () => {
it('defaults to not sending the user-agent HTTP header', async function () {
nock('https://www.example.com/', {
badheaders: ['user-agent'],
})
.get('/')
.reply(200);

await request.call(this.provider, { url: 'https://www.example.com' });
});

it("uses a custom 'user-agent' HTTP header", async function () {
nock('https://www.example.com/', {
reqheaders: {
'user-agent': 'some user agent',
},
})
.get('/with-custom-user-agent')
.reply(200);

await request.call(this.provider, { url: 'https://www.example.com/with-custom-user-agent' });
});
});
});

0 comments on commit c77513c

Please sign in to comment.