Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Commit

Permalink
♻️ disable user-agent header when client options not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
yatki authored and willclarktech committed Jul 16, 2018
1 parent 6714a5d commit b558322
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
23 changes: 14 additions & 9 deletions packages/lisk-api-client/src/api_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ const commonHeaders = {
'Content-Type': 'application/json',
};

const getUserAgent = (
{ name = '????', version = '????', engine = '????' } = {},
) => {
const getUserAgent = clientOptions => {
if (!clientOptions) {
return {};
}

const { name = '????', version = '????', engine = '????' } = clientOptions;

const liskElementsInformation =
'LiskElements/1.0 (+https://github.com/LiskHQ/lisk-elements)';
const locale =
Expand All @@ -51,9 +55,12 @@ const getUserAgent = (
const systemInformation = `${os.platform()} ${os.release()}; ${os.arch()}${
locale ? `; ${locale}` : ''
}`;
return `${name}/${version} (${engine}) ${liskElementsInformation} ${
systemInformation
}`;

return {
'User-Agent': `${name}/${version} (${engine}) ${liskElementsInformation} ${
systemInformation
}`,
};
};

export default class APIClient {
Expand Down Expand Up @@ -107,9 +114,7 @@ export default class APIClient {
{},
commonHeaders,
options.nethash ? { nethash: options.nethash } : {},
{
'User-Agent': getUserAgent(options.client),
},
getUserAgent(options.client),
);

this.nodes = nodes;
Expand Down
18 changes: 14 additions & 4 deletions packages/lisk-api-client/test/api_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ describe('APIClient module', () => {
const platformInfo = `${os.platform()} ${os.release()}; ${os.arch()}${
locale ? `; ${locale}` : ''
}`;
const baseUserAgent = `LiskElements/1.0 (+https://github.com/LiskHQ/lisk-elements) ${platformInfo}`;
const defaultUserAgent = `????/???? (????) ${baseUserAgent}`;
const customUserAgent = `LiskHub/5.0 (+https://github.com/LiskHQ/lisk-hub) ${baseUserAgent}`;
const baseUserAgent = `LiskElements/1.0 (+https://github.com/LiskHQ/lisk-elements) ${
platformInfo
}`;
const customUserAgent = `LiskHub/5.0 (+https://github.com/LiskHQ/lisk-hub) ${
baseUserAgent
}`;
const defaultHeaders = {
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': defaultUserAgent,
};

const customHeaders = {
Expand Down Expand Up @@ -210,6 +212,14 @@ describe('APIClient module', () => {
.to.have.property('headers')
.and.eql(customHeaders);
});

it('should not set User-Agent header when client options were not given', () => {
apiClient = new APIClient(defaultNodes, {
version: customHeaders.version,
nethash: testnetHash,
});
return expect(apiClient.headers).to.not.have.property('User-Agent');
});
});

describe('nodes', () => {
Expand Down

0 comments on commit b558322

Please sign in to comment.