Skip to content

Commit

Permalink
fix: HTTP request agent not changes on protocol change. (#4286)
Browse files Browse the repository at this point in the history
  • Loading branch information
garg3133 authored Oct 25, 2024
1 parent 2e37dbe commit 6a41f47
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ class HttpRequest extends EventEmitter {
}

static getAgent({secure, keepAliveMsecs, maxSockets}) {
if (__httpKeepAliveAgent__) {
const expectedProtocol = secure ? 'https:' : 'http:';
if (__httpKeepAliveAgent__ && __httpKeepAliveAgent__.protocol === expectedProtocol) {
return __httpKeepAliveAgent__;
}

const protocol = secure ? https : http;

// might also consider storing http and https user agents separately
// and use the same agent for the entirety of test instead of creating
// a new user-agent every time the protocol changes.
// 1st commit: https://github.com/nightwatchjs/nightwatch/pull/3748
return __httpKeepAliveAgent__ = new protocol.Agent({
keepAlive: true,
keepAliveMsecs,
Expand Down Expand Up @@ -160,8 +165,9 @@ class HttpRequest extends EventEmitter {
}

if (enabled) {
const port = reqOptions.port || this.httpOpts.port;
reqOptions.agent = HttpRequest.getAgent({
secure: this.httpOpts.port === 443,
secure: port === 443,
keepAliveMsecs,
maxSockets: 1
});
Expand Down
63 changes: 63 additions & 0 deletions test/src/index/testRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,69 @@ describe('test HttpRequest', function() {
assert.strictEqual(secondRequest.reqOptions.agent, opts.agent);
});

it.only('keep alive user agent changes when request protocol changes', function() {
const optionsHttp = {
path: '/session',
method: 'POST',
port: 4444,
data: {
desiredCapabilities: {
browserName: 'firefox'
}
}
};

const optionsHttps = {
path: '/session',
method: 'POST',
port: 443,
data: {
desiredCapabilities: {
browserName: 'chrome'
}
}
};

HttpRequest.globalSettings = {
default_path: '/wd/hub',
port: 4444,
keep_alive: {
keepAliveMsecs: 1000,
enabled: true
}
};

const httpRequest = new HttpRequest(optionsHttp);
const secondHttpRequest = new HttpRequest(optionsHttp);
const httpsRequest = new HttpRequest(optionsHttps);
const secondHttpsRequest = new HttpRequest(optionsHttps);
const thirdHttpRequest = new HttpRequest(optionsHttp);

const optsHttp = httpRequest.reqOptions;
assert.ok('agent' in optsHttp);
assert.ok(optsHttp.agent.protocol === 'http:');
assert.strictEqual(optsHttp.agent.keepAliveMsecs, 1000);

// first and second HTTP requests should share the same agent
assert.strictEqual(secondHttpRequest.reqOptions.agent, optsHttp.agent);

const optsHttps = httpsRequest.reqOptions;
assert.ok('agent' in optsHttps);
assert.ok(optsHttps.agent.protocol === 'https:');
// HTTPS agent should be different from HTTP agent
assert.notStrictEqual(optsHttps.agent, optsHttp.agent);

// first and second HTTPS requests should share the same agent
assert.strictEqual(secondHttpsRequest.reqOptions.agent, optsHttps.agent);
assert.notStrictEqual(secondHttpsRequest.reqOptions.agent, optsHttp.agent);

// while making HTTP request again after HTTPS request, it does
// not re-use the old HTTP agent (probably it should?)
assert.notStrictEqual(thirdHttpRequest.reqOptions.agent, optsHttp.agent);
assert.ok('agent' in thirdHttpRequest.reqOptions);
assert.ok(thirdHttpRequest.reqOptions.agent.protocol === 'http:');
});

it('test send post request with keep alive extended - disabled', function (done) {
const options = {
path: '/session',
Expand Down

0 comments on commit 6a41f47

Please sign in to comment.