Skip to content

Commit

Permalink
send custom user agent when using got (#6256)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s committed Mar 10, 2021
1 parent 2359eb2 commit fd7eddc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/base-service/got.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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 }
Expand Down
15 changes: 15 additions & 0 deletions core/base-service/got.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit fd7eddc

Please sign in to comment.