Skip to content

Commit

Permalink
Allow using a custom Agent with http.HttpClient.
Browse files Browse the repository at this point in the history
Change tests to use an Agent with a single socket
instead http.globalAgent for consistent behavior
between node v0.10.x and v0.11.y (y >= 7):

node v0.11.4 changed
http.globalAgent.maxSockets = Inifinity (from 5)
and v0.11.7 changed the http module to only send
“Connection: keep-alive” when necessary.
  • Loading branch information
jleyba committed Jul 18, 2014
1 parent 4e40b26 commit 3b2779a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
15 changes: 12 additions & 3 deletions javascript/node/selenium-webdriver/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@ var base = require('../_base'),
* A {@link webdriver.http.Client} implementation using Node's built-in http
* module.
* @param {string} serverUrl URL for the WebDriver server to send commands to.
* @param {http.Agent=} opt_agent The agent to use for each request.
* Defaults to {@code http.globalAgent}.
* @constructor
* @implements {webdriver.http.Client}
*/
var HttpClient = function(serverUrl) {
var HttpClient = function(serverUrl, opt_agent) {
var parsedUrl = url.parse(serverUrl);
if (!parsedUrl.hostname) {
throw new Error('Invalid server URL: ' + serverUrl);
}

/** @private {http.Agent} */
this.agent_ = opt_agent;

/**
* Base options for each request.
* @private {!Object}
Expand Down Expand Up @@ -66,13 +71,17 @@ HttpClient.prototype.send = function(httpRequest, callback) {
path += httpRequest.path;
}

sendRequest({
var options = {
method: httpRequest.method,
host: this.options_.host,
port: this.options_.port,
path: path,
headers: httpRequest.headers
}, callback, data);
};
if (this.agent_) {
options.agent = this.agent_;
}
sendRequest(options, callback, data);
};


Expand Down
6 changes: 5 additions & 1 deletion javascript/node/selenium-webdriver/test/http/http_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.

var assert = require('assert');
var http = require('http');

var HttpClient = require('../../http').HttpClient;
var HttpRequest = require('../../_base').require('webdriver.http.Request');
Expand Down Expand Up @@ -52,7 +53,10 @@ describe('HttpClient', function() {
var request = new HttpRequest('GET', '/echo');
request.headers['Foo'] = 'Bar';

var client = new HttpClient(server.url());
var agent = new http.Agent();
agent.maxSockets = 1; // Only making 1 request.

var client = new HttpClient(server.url(), agent);
return promise.checkedNodeCall(client.send.bind(client, request))
.then(function(response) {
assert.equal(200, response.status);
Expand Down

0 comments on commit 3b2779a

Please sign in to comment.