Skip to content

Commit 5f52b02

Browse files
committed
[js] Add a User-Agent header for #5657
1 parent 97ca8ab commit 5f52b02

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

javascript/node/selenium-webdriver/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
### API Changes
44

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

79

810
## v4.0.0-alpha.1

javascript/node/selenium-webdriver/http/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ function getRequestOptions(aUrl) {
5959
}
6060

6161

62+
/** @const {string} */
63+
const USER_AGENT = (function() {
64+
const version = require('../package.json').version;
65+
const platform =
66+
({'darwin': 'mac', 'win32': 'windows'}[process.platform]) || 'linux';
67+
return `selenium/${version} (js ${platform})`;
68+
})();
69+
70+
6271
/**
6372
* A basic HTTP client used to send messages to a remote end.
6473
*
@@ -97,6 +106,7 @@ class HttpClient {
97106
headers[name] = value;
98107
});
99108

109+
headers['User-Agent'] = USER_AGENT;
100110
headers['Content-Length'] = 0;
101111
if (httpRequest.method == 'POST' || httpRequest.method == 'PUT') {
102112
data = JSON.stringify(httpRequest.data);
@@ -209,7 +219,8 @@ function sendRequest(options, onOk, onError, opt_data, opt_proxy, opt_retries) {
209219
search: location.search,
210220
hash: location.hash,
211221
headers: {
212-
'Accept': 'application/json; charset=utf-8'
222+
'Accept': 'application/json; charset=utf-8',
223+
'User-Agent': USER_AGENT,
213224
}
214225
}, onOk, onError, undefined, opt_proxy);
215226
return;

javascript/node/selenium-webdriver/test/http/http_test.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ describe('HttpClient', function() {
3232
let parsedUrl = url.parse(req.url);
3333

3434
if (req.method == 'GET' && req.url == '/echo') {
35-
res.writeHead(200, req.headers);
36-
res.end();
35+
res.writeHead(200);
36+
res.end(JSON.stringify(req.headers));
3737

3838
} else if (req.method == 'GET' && req.url == '/redirect') {
3939
res.writeHead(303, {'Location': server.url('/hello')});
@@ -118,9 +118,16 @@ describe('HttpClient', function() {
118118
var client = new HttpClient(server.url(), agent);
119119
return client.send(request).then(function(response) {
120120
assert.equal(200, response.status);
121-
assert.equal(response.headers.get('content-length'), '0');
122-
assert.equal(response.headers.get('connection'), 'keep-alive');
123-
assert.equal(response.headers.get('host'), server.host());
121+
122+
const headers = JSON.parse(response.body);
123+
assert.equal(headers['content-length'], '0');
124+
assert.equal(headers['connection'], 'keep-alive');
125+
assert.equal(headers['host'], server.host());
126+
127+
const regex = /^selenium\/.* \(js (windows|mac|linux)\)$/;
128+
assert.ok(
129+
regex.test(headers['user-agent']),
130+
`${headers['user-agent']} does not match ${regex}`);
124131

125132
assert.equal(request.headers.get('Foo'), 'Bar');
126133
assert.equal(

0 commit comments

Comments
 (0)