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 User-Agent request header #40

Merged
merged 1 commit into from
Jun 26, 2018
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
9 changes: 4 additions & 5 deletions src/internal/haveibeenpwned/axiosInstance.js
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -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' && {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You taught me something: optional spreading!

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! 😄

'User-Agent': `${name} ${version}`,
}),
},
});
7 changes: 6 additions & 1 deletion src/internal/haveibeenpwned/axiosInstance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
});
Expand Down