diff --git a/lib/HttpClient.js b/lib/HttpClient.js index 23e7785..315ee22 100644 --- a/lib/HttpClient.js +++ b/lib/HttpClient.js @@ -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) { diff --git a/test/HttpClient.test.js b/test/HttpClient.test.js index 13eccc0..143eeaa 100644 --- a/test/HttpClient.test.js +++ b/test/HttpClient.test.js @@ -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); + }); });