Skip to content

Commit

Permalink
[js] Add a User-Agent header for SeleniumHQ#5657
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyba committed Mar 31, 2018
1 parent 97ca8ab commit 5f52b02
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions javascript/node/selenium-webdriver/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### API Changes

* Export `lib/input.Origin` from the top level `selenium-webdriver` module.
* HTTP requests from this library will now include a User-Agent of the form
`selenium/${VERSION} (js ${PLATFORM})`.


## v4.0.0-alpha.1
Expand Down
13 changes: 12 additions & 1 deletion javascript/node/selenium-webdriver/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ function getRequestOptions(aUrl) {
}


/** @const {string} */
const USER_AGENT = (function() {
const version = require('../package.json').version;
const platform =
({'darwin': 'mac', 'win32': 'windows'}[process.platform]) || 'linux';
return `selenium/${version} (js ${platform})`;
})();


/**
* A basic HTTP client used to send messages to a remote end.
*
Expand Down Expand Up @@ -97,6 +106,7 @@ class HttpClient {
headers[name] = value;
});

headers['User-Agent'] = USER_AGENT;
headers['Content-Length'] = 0;
if (httpRequest.method == 'POST' || httpRequest.method == 'PUT') {
data = JSON.stringify(httpRequest.data);
Expand Down Expand Up @@ -209,7 +219,8 @@ function sendRequest(options, onOk, onError, opt_data, opt_proxy, opt_retries) {
search: location.search,
hash: location.hash,
headers: {
'Accept': 'application/json; charset=utf-8'
'Accept': 'application/json; charset=utf-8',
'User-Agent': USER_AGENT,
}
}, onOk, onError, undefined, opt_proxy);
return;
Expand Down
17 changes: 12 additions & 5 deletions javascript/node/selenium-webdriver/test/http/http_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe('HttpClient', function() {
let parsedUrl = url.parse(req.url);

if (req.method == 'GET' && req.url == '/echo') {
res.writeHead(200, req.headers);
res.end();
res.writeHead(200);
res.end(JSON.stringify(req.headers));

} else if (req.method == 'GET' && req.url == '/redirect') {
res.writeHead(303, {'Location': server.url('/hello')});
Expand Down Expand Up @@ -118,9 +118,16 @@ describe('HttpClient', function() {
var client = new HttpClient(server.url(), agent);
return client.send(request).then(function(response) {
assert.equal(200, response.status);
assert.equal(response.headers.get('content-length'), '0');
assert.equal(response.headers.get('connection'), 'keep-alive');
assert.equal(response.headers.get('host'), server.host());

const headers = JSON.parse(response.body);
assert.equal(headers['content-length'], '0');
assert.equal(headers['connection'], 'keep-alive');
assert.equal(headers['host'], server.host());

const regex = /^selenium\/.* \(js (windows|mac|linux)\)$/;
assert.ok(
regex.test(headers['user-agent']),
`${headers['user-agent']} does not match ${regex}`);

assert.equal(request.headers.get('Foo'), 'Bar');
assert.equal(
Expand Down

0 comments on commit 5f52b02

Please sign in to comment.