light-my-request
Advanced tools
Comparing version 3.6.2 to 3.6.3
@@ -49,2 +49,10 @@ 'use strict' | ||
Response.prototype.setTimeout = function (msecs, callback) { | ||
this.timeoutHandle = setTimeout(() => { | ||
this.emit('timeout') | ||
}, msecs) | ||
this.on('timeout', callback) | ||
return this | ||
} | ||
Response.prototype.writeHead = function () { | ||
@@ -69,2 +77,5 @@ const result = http.ServerResponse.prototype.writeHead.apply(this, arguments) | ||
Response.prototype.write = function (data, encoding, callback) { | ||
if (this.timeoutHandle) { | ||
clearTimeout(this.timeoutHandle) | ||
} | ||
http.ServerResponse.prototype.write.call(this, data, encoding, callback) | ||
@@ -71,0 +82,0 @@ this._lightMyRequest.payloadChunks.push(Buffer.from(data, encoding)) |
{ | ||
"name": "light-my-request", | ||
"version": "3.6.2", | ||
"version": "3.6.3", | ||
"description": "Fake HTTP injection library", | ||
@@ -12,4 +12,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@types/node": "^12.11.6", | ||
"form-data": "^2.5.1", | ||
"@types/node": "^13.1.0", | ||
"form-data": "^3.0.0", | ||
"pre-commit": "^1.2.2", | ||
@@ -16,0 +16,0 @@ "standard": "^14.0.2", |
@@ -850,2 +850,24 @@ 'use strict' | ||
test('should handle response timeout handler', (t) => { | ||
t.plan(3) | ||
const dispatch = function (req, res) { | ||
const handle = setTimeout(() => { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
res.end('incorrect') | ||
}, 200) | ||
res.setTimeout(100, () => { | ||
clearTimeout(handle) | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
res.end('correct') | ||
}) | ||
res.on('timeout', () => { | ||
t.ok(true, 'Response timeout event not emitted') | ||
}) | ||
} | ||
inject(dispatch, { method: 'GET', url: '/test' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res.payload, 'correct') | ||
}) | ||
}) | ||
test('should throw on unknown HTTP method', (t) => { | ||
@@ -852,0 +874,0 @@ t.plan(1) |
63120
1714