Skip to content

Commit

Permalink
chore(HttpClient)!: keep User-Agent on headers
Browse files Browse the repository at this point in the history
If User-Agent is given via headers, but is not given via options, keep
the headers User-Agent instead of replacing it with restify-clients UA.
This change makes User-Agent handling more aligned with how
restify-clients deals with other headers which can be overriten by
options.

The reasoning behind this is in cases where restify-clients is acting
like a forward-proxy of sorts. If User-Agent is present in headers
there's probably a request forwarding happening.
  • Loading branch information
mmarchini committed Jun 29, 2020
1 parent 82071ab commit 6ca322c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/HttpClient.js
Expand Up @@ -721,9 +721,12 @@ function HttpClient(options) {
this.headers['content-type'] = options.contentType;
}

if (options.userAgent !== false) {
this.headers['user-agent'] = options.userAgent ||
defaultUserAgent();
if (options.userAgent) {
this.headers['user-agent'] = options.userAgent;
}

if (!this.headers['user-agent']) {
this.headers['user-agent'] = defaultUserAgent();
}

if (options.version) {
Expand Down
23 changes: 23 additions & 0 deletions test/HttpClient.test.js
Expand Up @@ -45,4 +45,27 @@ describe('HttpClient', function () {
assert.strictEqual(CLIENT.url.hostname, 'www.restify.com');
assert.strictEqual(CLIENT.url.port, '3000');
});

it('should fill default User Agent when none is given', function () {
CLIENT = clients.createHttpClient();
assert.strictEqual(CLIENT.headers['user-agent'].slice(0, 8),
'restify/');
});

it('should keep User Agent from headers if none is given', function () {
const userAgent = 'The Acme Browser 0.42';
CLIENT = clients.createHttpClient({
headers: {'user-agent': userAgent}
});
assert.strictEqual(CLIENT.headers['user-agent'], userAgent);
});

it('should use given User Agent', function () {
const userAgent = 'The Acme Browser 0.42';
CLIENT = clients.createHttpClient({
headers: {'user-agent': 'Not The Acme Browser 0.00'},
userAgent
});
assert.strictEqual(CLIENT.headers['user-agent'], userAgent);
});
});

0 comments on commit 6ca322c

Please sign in to comment.