Skip to content

Commit

Permalink
test: add request timeout bigger than agent timeout cases
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Oct 31, 2018
1 parent 0de1968 commit d177d40
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
31 changes: 31 additions & 0 deletions test/agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,37 @@ describe('test/agent.test.js', () => {
assert(Object.keys(agentkeepalive.sockets).length === 1);
});

describe('request timeout > agent timeout', () => {
it('should use request timeout', done => {
const agent = new Agent({
keepAlive: true,
timeout: 1000,
});
const req = http.get({
agent,
port,
path: '/?timeout=20000',
timeout: 1500,
}, res => {
console.error(res.statusCode, res.headers);
assert.fail('should not get res here');
});

let isTimeout = false;
req.on('timeout', () => {
isTimeout = true;
req.abort();
});
req.on('error', err => {
assert(isTimeout);
assert(err);
assert(err.message === 'socket hang up');
assert(err.code === 'ECONNRESET');
done();
});
});
});

describe('keepAlive = false', () => {
it('should close socket after request', done => {
const name = 'localhost:' + port + ':';
Expand Down
33 changes: 32 additions & 1 deletion test/https_agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const urlparse = require('url').parse;
const fs = require('fs');
const assert = require('assert');
const HttpsAgent = require('..').HttpsAgent;
// const HttpsAgent = https.Agent;

describe('test/https_agent.test.js', () => {
let app = null;
Expand Down Expand Up @@ -219,4 +218,36 @@ describe('test/https_agent.test.js', () => {
});
});
});

describe('request timeout > agent timeout', () => {
it('should use request timeout', done => {
const agent = new HttpsAgent({
keepAlive: true,
timeout: 2000,
});
const req = https.get({
agent,
port,
path: '/?timeout=20000',
timeout: 2500,
ca: fs.readFileSync(__dirname + '/fixtures/ca.pem'),
}, res => {
console.error(res.statusCode, res.headers);
assert.fail('should not get res here');
});

let isTimeout = false;
req.on('timeout', () => {
isTimeout = true;
req.abort();
});
req.on('error', err => {
assert(isTimeout);
assert(err);
assert(err.message === 'socket hang up');
assert(err.code === 'ECONNRESET');
done();
});
});
});
});

0 comments on commit d177d40

Please sign in to comment.