1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
const assert = require('assert');
const getSetup = require('./support/setup');
const request = require('./support/client');
describe('.timeout(ms)', function () {
let setup;
let base;
before(async () => {
setup = await getSetup();
base = setup.uri;
});
this.timeout(15_000);
describe('when timeout is exceeded', () => {
it('should error', (done) => {
request
.get(`${base}/delay/500`)
.timeout(150)
.end((error, res) => {
assert(error, 'expected an error');
assert.equal(
'number',
typeof error.timeout,
'expected an error with .timeout'
);
assert.equal('ECONNABORTED', error.code, 'expected abort error code');
done();
});
});
it('should error in promise interface ', (done) => {
request
.get(`${base}/delay/500`)
.timeout(150)
.catch((err) => {
assert(err, 'expected an error');
assert.equal(
'number',
typeof err.timeout,
'expected an error with .timeout'
);
assert.equal('ECONNABORTED', err.code, 'expected abort error code');
done();
});
});
it('should handle gzip timeout', (done) => {
request
.get(`${base}/delay/zip`)
.timeout(150)
.end((error, res) => {
assert(error, 'expected an error');
assert.equal(
'number',
typeof error.timeout,
'expected an error with .timeout'
);
assert.equal('ECONNABORTED', error.code, 'expected abort error code');
done();
});
});
it('should handle buffer timeout', (done) => {
request
.get(`${base}/delay/json`)
.buffer(true)
.timeout(150)
.end((error, res) => {
assert(error, 'expected an error');
assert.equal(
'number',
typeof error.timeout,
'expected an error with .timeout'
);
assert.equal('ECONNABORTED', error.code, 'expected abort error code');
done();
});
});
it('should error on deadline', (done) => {
request
.get(`${base}/delay/500`)
.timeout({ deadline: 150 })
.end((error, res) => {
assert(error, 'expected an error');
assert.equal(
'number',
typeof error.timeout,
'expected an error with .timeout'
);
assert.equal('ECONNABORTED', error.code, 'expected abort error code');
done();
});
});
it('should support setting individual options', (done) => {
request
.get(`${base}/delay/500`)
.timeout({ deadline: 10 })
.timeout({ response: 99_999 })
.end((error, res) => {
assert(error, 'expected an error');
assert.equal('ECONNABORTED', error.code, 'expected abort error code');
assert.equal('ETIME', error.errno);
done();
});
});
it('should error on response', (done) => {
request
.get(`${base}/delay/500`)
.timeout({ response: 150 })
.end((error, res) => {
assert(error, 'expected an error');
assert.equal(
'number',
typeof error.timeout,
'expected an error with .timeout'
);
assert.equal('ECONNABORTED', error.code, 'expected abort error code');
assert.equal('ETIMEDOUT', error.errno);
done();
});
});
it('should accept slow body with fast response', (done) => {
request
.get(`${base}/delay/slowbody`)
.timeout({ response: 1000 })
.on('progress', () => {
// This only makes the test faster without relying on arbitrary timeouts
request.get(`${base}/delay/slowbody/finish`).end();
})
.end(done);
});
});
});
|