diff --git a/core/base-service/got.js b/core/base-service/got.js index a5b0edd9cb8a9..f0c9db2e4e6da 100644 --- a/core/base-service/got.js +++ b/core/base-service/got.js @@ -3,6 +3,8 @@ const got = require('got') const { Inaccessible, InvalidResponse } = require('./errors') +const userAgent = 'Shields.io/2003a' + function requestOptions2GotOptions(options) { const requestOptions = Object.assign({}, options) const gotOptions = {} @@ -49,6 +51,8 @@ async function sendRequest(gotWrapper, url, options) { const gotOptions = requestOptions2GotOptions(options) gotOptions.throwHttpErrors = false gotOptions.retry = 0 + gotOptions.headers = gotOptions.headers || {} + gotOptions.headers['User-Agent'] = userAgent try { const resp = await gotWrapper(url, gotOptions) return { res: resp, buffer: resp.body } diff --git a/core/base-service/got.spec.js b/core/base-service/got.spec.js index d2a71ff549bea..580d2d33acb68 100644 --- a/core/base-service/got.spec.js +++ b/core/base-service/got.spec.js @@ -82,6 +82,21 @@ describe('got wrapper', function () { ) }) + it('should pass a custom user agent header', async function () { + nock('https://www.google.com', { + reqheaders: { + 'user-agent': function (agent) { + return agent.startsWith('Shields.io') + }, + }, + }) + .get('/foo/bar') + .once() + .reply(200) + const sendRequest = fetchFactory(1024) + await sendRequest('https://www.google.com/foo/bar') + }) + afterEach(function () { nock.cleanAll() nock.enableNetConnect()