Skip to content

Commit

Permalink
add http client options (SeleniumHQ#9638)
Browse files Browse the repository at this point in the history
Co-authored-by: David Burns <david.burns@theautomatedtester.co.uk>
  • Loading branch information
Dharin-shah and AutomatedTester committed Jul 15, 2021
1 parent 2e53853 commit b0a4a29
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
28 changes: 25 additions & 3 deletions javascript/node/selenium-webdriver/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ class HttpClient {
* Defaults to `http.globalAgent`.
* @param {?string=} opt_proxy The proxy to use for the connection to the
* server. Default is to use no proxy.
* @param {?Object.<string,Object>} client_options
*/
constructor(serverUrl, opt_agent, opt_proxy) {
constructor(serverUrl, opt_agent, opt_proxy, client_options = {}) {
/** @private {http.Agent} */
this.agent_ = opt_agent || null

Expand All @@ -88,12 +89,33 @@ class HttpClient {
*/
this.options_ = getRequestOptions(serverUrl)

/**
* client options, header overrides
*/
this.client_options = client_options

/**
* sets keep-alive for the agent
* see https://stackoverflow.com/a/58332910
*/
this.keepAlive = this.client_options['keep-alive']

/**
* @private {?RequestOptions}
*/
this.proxyOptions_ = opt_proxy ? getRequestOptions(opt_proxy) : null
}

get keepAlive() {
return this.agent_.keepAlive
}

set keepAlive(value) {
if (value === 'true' || value === true) {
this.agent_.keepAlive = true
}
}

/** @override */
send(httpRequest) {
let data
Expand All @@ -106,7 +128,7 @@ class HttpClient {
})
}

headers['User-Agent'] = USER_AGENT
headers['User-Agent'] = this.client_options['user-agent'] || USER_AGENT
headers['Content-Length'] = 0
if (httpRequest.method == 'POST' || httpRequest.method == 'PUT') {
data = JSON.stringify(httpRequest.data)
Expand Down Expand Up @@ -232,7 +254,7 @@ function sendRequest(options, onOk, onError, opt_data, opt_proxy, opt_retries) {
hash: location.hash,
headers: {
Accept: 'application/json; charset=utf-8',
'User-Agent': USER_AGENT,
'User-Agent': options.headers['User-Agent'] || USER_AGENT,
},
},
onOk,
Expand Down
64 changes: 64 additions & 0 deletions javascript/node/selenium-webdriver/test/http/http_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,70 @@ describe('HttpClient', function () {
request.headers.get('Accept'),
'application/json; charset=utf-8'
)
assert.strictEqual(agent.keepAlive, false)
})
})

it('can send a basic HTTP request with custom user-agent via client_options', function () {
const request = new HttpRequest('GET', '/echo')
request.headers.set('Foo', 'Bar')

const agent = new http.Agent()
agent.maxSockets = 1 // Only making 1 request.

const client = new HttpClient(server.url(), agent, null, {
'user-agent': 'test'
})

return client.send(request).then(function (response) {
assert.strictEqual(200, response.status)

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

assert.strictEqual(request.headers.get('Foo'), 'Bar')
assert.strictEqual(agent.keepAlive, false)
assert.strictEqual(
request.headers.get('Accept'),
'application/json; charset=utf-8'
)
})
})

it('can send a basic HTTP request with keep-alive being set to true via client_options', function () {
const request = new HttpRequest('GET', '/echo')
request.headers.set('Foo', 'Bar')

const agent = new http.Agent()
agent.maxSockets = 1 // Only making 1 request.

const client = new HttpClient(server.url(), agent, null, {
'keep-alive': 'true',
})

return client.send(request).then(function (response) {
assert.strictEqual(200, response.status)

const headers = JSON.parse(response.body)
assert.strictEqual(headers['content-length'], '0')
assert.strictEqual(headers['connection'], 'keep-alive')
assert.strictEqual(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.strictEqual(request.headers.get('Foo'), 'Bar')
assert.strictEqual(agent.keepAlive, true)
assert.strictEqual(
request.headers.get('Accept'),
'application/json; charset=utf-8'
)
})
})

Expand Down

0 comments on commit b0a4a29

Please sign in to comment.