diff --git a/src/internal/haveibeenpwned/axiosInstance.js b/src/internal/haveibeenpwned/axiosInstance.js index 90ca37b2..9fb0fbac 100644 --- a/src/internal/haveibeenpwned/axiosInstance.js +++ b/src/internal/haveibeenpwned/axiosInstance.js @@ -1,10 +1,6 @@ import Axios from 'axios'; import { name, version } from '../../../package.json'; -// Add a custom User-Agent header when running outside the browser -const customUserAgent = - typeof navigator === 'undefined' ? `${name} ${version}` : undefined; - /** * An Axios instance used for API queries. Not meant for general use. * @@ -14,6 +10,9 @@ export default Axios.create({ baseURL: 'https://haveibeenpwned.com/api', headers: { Accept: 'application/vnd.haveibeenpwned.v2+json', - 'User-Agent': customUserAgent, + // Add a custom User-Agent header when running outside the browser + ...(typeof navigator === 'undefined' && { + 'User-Agent': `${name} ${version}`, + }), }, }); diff --git a/src/internal/haveibeenpwned/axiosInstance.test.js b/src/internal/haveibeenpwned/axiosInstance.test.js index eb847717..eff9f924 100644 --- a/src/internal/haveibeenpwned/axiosInstance.test.js +++ b/src/internal/haveibeenpwned/axiosInstance.test.js @@ -7,6 +7,9 @@ describe('internal (haveibeenpwned): axiosInstance', () => { // Node global.navigator = undefined; const axiosInstanceNode = require.requireActual('./axiosInstance').default; + expect(Object.keys(axiosInstanceNode.defaults.headers)).toContain( + 'User-Agent', + ); expect(axiosInstanceNode.defaults.headers['User-Agent']).toBeTruthy(); jest.resetModules(); @@ -15,7 +18,9 @@ describe('internal (haveibeenpwned): axiosInstance', () => { global.navigator = {}; const axiosInstanceBrowser = require.requireActual('./axiosInstance') .default; - expect(axiosInstanceBrowser.defaults.headers['User-Agent']).toBeUndefined(); + expect(Object.keys(axiosInstanceBrowser.defaults.headers)).not.toContain( + 'User-Agent', + ); global.navigator = originalNavigator; });