Skip to content

Commit b0a4a29

Browse files
add http client options (#9638)
Co-authored-by: David Burns <david.burns@theautomatedtester.co.uk>
1 parent 2e53853 commit b0a4a29

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ class HttpClient {
7777
* Defaults to `http.globalAgent`.
7878
* @param {?string=} opt_proxy The proxy to use for the connection to the
7979
* server. Default is to use no proxy.
80+
* @param {?Object.<string,Object>} client_options
8081
*/
81-
constructor(serverUrl, opt_agent, opt_proxy) {
82+
constructor(serverUrl, opt_agent, opt_proxy, client_options = {}) {
8283
/** @private {http.Agent} */
8384
this.agent_ = opt_agent || null
8485

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

92+
/**
93+
* client options, header overrides
94+
*/
95+
this.client_options = client_options
96+
97+
/**
98+
* sets keep-alive for the agent
99+
* see https://stackoverflow.com/a/58332910
100+
*/
101+
this.keepAlive = this.client_options['keep-alive']
102+
91103
/**
92104
* @private {?RequestOptions}
93105
*/
94106
this.proxyOptions_ = opt_proxy ? getRequestOptions(opt_proxy) : null
95107
}
96108

109+
get keepAlive() {
110+
return this.agent_.keepAlive
111+
}
112+
113+
set keepAlive(value) {
114+
if (value === 'true' || value === true) {
115+
this.agent_.keepAlive = true
116+
}
117+
}
118+
97119
/** @override */
98120
send(httpRequest) {
99121
let data
@@ -106,7 +128,7 @@ class HttpClient {
106128
})
107129
}
108130

109-
headers['User-Agent'] = USER_AGENT
131+
headers['User-Agent'] = this.client_options['user-agent'] || USER_AGENT
110132
headers['Content-Length'] = 0
111133
if (httpRequest.method == 'POST' || httpRequest.method == 'PUT') {
112134
data = JSON.stringify(httpRequest.data)
@@ -232,7 +254,7 @@ function sendRequest(options, onOk, onError, opt_data, opt_proxy, opt_retries) {
232254
hash: location.hash,
233255
headers: {
234256
Accept: 'application/json; charset=utf-8',
235-
'User-Agent': USER_AGENT,
257+
'User-Agent': options.headers['User-Agent'] || USER_AGENT,
236258
},
237259
},
238260
onOk,

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,70 @@ describe('HttpClient', function () {
131131
request.headers.get('Accept'),
132132
'application/json; charset=utf-8'
133133
)
134+
assert.strictEqual(agent.keepAlive, false)
135+
})
136+
})
137+
138+
it('can send a basic HTTP request with custom user-agent via client_options', function () {
139+
const request = new HttpRequest('GET', '/echo')
140+
request.headers.set('Foo', 'Bar')
141+
142+
const agent = new http.Agent()
143+
agent.maxSockets = 1 // Only making 1 request.
144+
145+
const client = new HttpClient(server.url(), agent, null, {
146+
'user-agent': 'test'
147+
})
148+
149+
return client.send(request).then(function (response) {
150+
assert.strictEqual(200, response.status)
151+
152+
const headers = JSON.parse(response.body)
153+
assert.strictEqual(headers['content-length'], '0')
154+
assert.strictEqual(headers['connection'], 'keep-alive')
155+
assert.strictEqual(headers['host'], server.host())
156+
assert.strictEqual(headers['user-agent'], 'test')
157+
158+
assert.strictEqual(request.headers.get('Foo'), 'Bar')
159+
assert.strictEqual(agent.keepAlive, false)
160+
assert.strictEqual(
161+
request.headers.get('Accept'),
162+
'application/json; charset=utf-8'
163+
)
164+
})
165+
})
166+
167+
it('can send a basic HTTP request with keep-alive being set to true via client_options', function () {
168+
const request = new HttpRequest('GET', '/echo')
169+
request.headers.set('Foo', 'Bar')
170+
171+
const agent = new http.Agent()
172+
agent.maxSockets = 1 // Only making 1 request.
173+
174+
const client = new HttpClient(server.url(), agent, null, {
175+
'keep-alive': 'true',
176+
})
177+
178+
return client.send(request).then(function (response) {
179+
assert.strictEqual(200, response.status)
180+
181+
const headers = JSON.parse(response.body)
182+
assert.strictEqual(headers['content-length'], '0')
183+
assert.strictEqual(headers['connection'], 'keep-alive')
184+
assert.strictEqual(headers['host'], server.host())
185+
186+
const regex = /^selenium\/.* \(js (windows|mac|linux)\)$/
187+
assert.ok(
188+
regex.test(headers['user-agent']),
189+
`${headers['user-agent']} does not match ${regex}`
190+
)
191+
192+
assert.strictEqual(request.headers.get('Foo'), 'Bar')
193+
assert.strictEqual(agent.keepAlive, true)
194+
assert.strictEqual(
195+
request.headers.get('Accept'),
196+
'application/json; charset=utf-8'
197+
)
134198
})
135199
})
136200

0 commit comments

Comments
 (0)