Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow specifying the user-agent header for outgoing requests #1287

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' });
});
});
});
Loading