Skip to content

Commit

Permalink
Merge pull request #139 from fkalny-groupon/main
Browse files Browse the repository at this point in the history
feat: add userAgent forwarding and tests
  • Loading branch information
TheSkyIsBlottedOut committed Feb 9, 2024
2 parents b6e8ca8 + d02643a commit 8c063e2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10.24.1
13 changes: 11 additions & 2 deletions lib/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,19 @@ function unifyAuth(auth) {
return `${user}:${pass}`;
}

function buildUserAgent({ clientName, clientVersion, appName, appSha, fqdn }) {
function buildUserAgent({
clientName,
clientVersion,
appName,
appSha,
fqdn,
userAgent,
}) {
return `${clientName || 'noServiceName'}/${
clientVersion || 'noServiceVersion'
} (${appName || 'noAppName'}/${appSha || 'noAppSha'}; ${fqdn || 'noFQDN'})`;
} (${appName || 'noAppName'}/${appSha || 'noAppSha'}; ${fqdn || 'noFQDN'}; ${
userAgent || 'noUserAgent'
})`;
}

function defaultTimeout(value, defaultValue) {
Expand Down
1 change: 1 addition & 0 deletions lib/typedefs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ declare namespace Gofer {
rejectUnauthorized?: boolean;
secureContext?: SecureContext;
captureAsyncStack?: boolean;
userAgent?: string;
[opt: string]: any;
};

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions test/gofer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ describe('gofer', () => {
});
});

describe('user agent', () => {
let gofer;
const userAgent = 'some-user-agent (like Firefox/42.0)';
before('create Gofer instance', () => {
gofer = new Gofer({ globalDefaults: { userAgent } }).with(options);
});

it('passes forward the user agent', function () {
if (typeof document !== 'undefined') {
return this.skip();
}

return gofer
.get('/echo')
.json()
.then(echo => {
assert.include(userAgent, echo.headers['user-agent']);
});
});
});

describe('sub-class', () => {
function SubGofer(config) {
Gofer.call(this, config, 'sub', '1.2.3', 'my-sub-client');
Expand Down

0 comments on commit 8c063e2

Please sign in to comment.