Skip to content

Commit

Permalink
feat: populate x-goog-api-client header for auth (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Aug 9, 2019
1 parent ed7384a commit 526dcf6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/transporters.ts
Expand Up @@ -68,6 +68,18 @@ export class DefaultTransporter {
'User-Agent'
] = `${uaValue} ${DefaultTransporter.USER_AGENT}`;
}
// track google-auth-library-nodejs version:
const authVersion = `auth/${pkg.version}`;
if (opts.headers['x-goog-api-client']) {
opts.headers[
'x-goog-api-client'
] = `${opts.headers['x-goog-api-client']} ${authVersion}`;
} else {
const nodeVersion = process.version.replace(/^v/, '');
opts.headers[
'x-goog-api-client'
] = `gl-node/${nodeVersion} ${authVersion}`;
}
}
return opts;
}
Expand Down
22 changes: 22 additions & 0 deletions test/test.transporters.ts
Expand Up @@ -55,6 +55,28 @@ it('should not append default client user agent to the existing user agent more
assert.strictEqual(opts.headers!['User-Agent'], appName);
});

it('should add x-goog-api-client header if none exists', () => {
const opts = transporter.configure({
url: '',
});
assert(
/^gl-node\/[.-\w$]+ auth\/[.-\w$]+$/.test(
opts.headers!['x-goog-api-client']
)
);
});

it('should append to x-goog-api-client header if it exists', () => {
const opts = transporter.configure({
headers: {'x-goog-api-client': 'gdcl/1.0.0'},
url: '',
});
console.info(opts.headers);
assert(
/^gdcl\/[.-\w$]+ auth\/[.-\w$]+$/.test(opts.headers!['x-goog-api-client'])
);
});

it('should create a single error from multiple response errors', done => {
const firstError = {message: 'Error 1'};
const secondError = {message: 'Error 2'};
Expand Down

0 comments on commit 526dcf6

Please sign in to comment.