light-my-request
Advanced tools
Comparing version 4.1.1 to 4.2.0
16
index.js
@@ -78,2 +78,14 @@ 'use strict' | ||
function makeRequest (dispatchFunc, server, req, res) { | ||
req.once('error', function (err) { | ||
if (this.destroyed) res.destroy(err) | ||
}) | ||
req.once('close', function () { | ||
if (this.destroyed && !this._error) res.destroy() | ||
}) | ||
return req.prepare(() => dispatchFunc.call(server, req, res)) | ||
} | ||
function doInject (dispatchFunc, options, callback) { | ||
@@ -96,3 +108,3 @@ options = (typeof options === 'string' ? { url: options } : options) | ||
return req.prepare(() => dispatchFunc.call(server, req, res)) | ||
return makeRequest(dispatchFunc, server, req, res) | ||
} else { | ||
@@ -103,3 +115,3 @@ return new Promise((resolve, reject) => { | ||
req.prepare(() => dispatchFunc.call(server, req, res)) | ||
makeRequest(dispatchFunc, server, req, res) | ||
}) | ||
@@ -106,0 +118,0 @@ } |
@@ -177,4 +177,14 @@ 'use strict' | ||
Request.prototype.destroy = function () {} | ||
Request.prototype.destroy = function (error) { | ||
if (this.destroyed) return | ||
this.destroyed = true | ||
if (error) { | ||
this._error = true | ||
process.nextTick(() => this.emit('error', error)) | ||
} | ||
process.nextTick(() => this.emit('close')) | ||
} | ||
module.exports = Request |
@@ -24,3 +24,6 @@ 'use strict' | ||
let called = false | ||
const onEndSuccess = (payload) => { | ||
// no need to early-return if already called because this handler is bound `once` | ||
called = true | ||
if (this._promiseCallback) { | ||
@@ -33,2 +36,4 @@ return process.nextTick(() => onEnd(payload)) | ||
const onEndFailure = (err) => { | ||
if (called) return | ||
called = true | ||
if (this._promiseCallback) { | ||
@@ -49,2 +54,4 @@ return process.nextTick(() => reject(err)) | ||
this.once('error', onEndFailure) | ||
this.once('close', onEndFailure) | ||
} | ||
@@ -97,4 +104,13 @@ | ||
Response.prototype.destroy = function () {} | ||
Response.prototype.destroy = function (error) { | ||
if (this.destroyed) return | ||
this.destroyed = true | ||
if (error) { | ||
process.nextTick(() => this.emit('error', error)) | ||
} | ||
process.nextTick(() => this.emit('close')) | ||
} | ||
Response.prototype.addTrailers = function (trailers) { | ||
@@ -101,0 +117,0 @@ for (const key in trailers) { |
{ | ||
"name": "light-my-request", | ||
"version": "4.1.1", | ||
"version": "4.2.0", | ||
"description": "Fake HTTP injection library", | ||
@@ -16,2 +16,3 @@ "main": "index.js", | ||
"@types/node": "^14.0.1", | ||
"end-of-stream": "^1.4.4", | ||
"form-auto-content": "^2.0.0", | ||
@@ -27,3 +28,3 @@ "form-data": "^3.0.0", | ||
"lint": "standard", | ||
"unit": "tap test/test.js --100", | ||
"unit": "tap test/test.js test/*.test.js --100", | ||
"coverage": "npm run unit -- --cov --coverage-report=html", | ||
@@ -30,0 +31,0 @@ "tsd": "tsd" |
116
test/test.js
@@ -10,2 +10,4 @@ 'use strict' | ||
const http = require('http') | ||
const { finished } = require('stream') | ||
const eos = require('end-of-stream') | ||
@@ -114,4 +116,17 @@ const inject = require('../index') | ||
test('includes deprecated connection on request', (t) => { | ||
t.plan(2) | ||
test('includes deprecated connection on request', { only: true }, (t) => { | ||
t.plan(3) | ||
const warnings = process.listeners('warning') | ||
process.removeAllListeners('warning') | ||
function onWarning (err) { | ||
t.equal(err.code, 'FST_LIGHTMYREQUEST_DEP01') | ||
return false | ||
} | ||
process.on('warning', onWarning) | ||
t.tearDown(() => { | ||
process.removeListener('warning', onWarning) | ||
for (const fn of warnings) { | ||
process.on('warning', fn) | ||
} | ||
}) | ||
const dispatch = function (req, res) { | ||
@@ -1573,2 +1588,4 @@ res.writeHead(200, { 'Content-Type': 'text/plain' }) | ||
test('no error for response destory', (t) => { | ||
t.plan(1) | ||
const dispatch = function (req, res) { | ||
@@ -1581,4 +1598,97 @@ res.destroy() | ||
}) | ||
}) | ||
t.end() | ||
test('request destory without error', (t) => { | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
req.destroy() | ||
} | ||
inject(dispatch, { method: 'GET', url: '/' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res, null) | ||
}) | ||
}) | ||
test('request destory with error', (t) => { | ||
t.plan(2) | ||
const fakeError = new Error('some-err') | ||
const dispatch = function (req, res) { | ||
req.destroy(fakeError) | ||
} | ||
inject(dispatch, { method: 'GET', url: '/' }, (err, res) => { | ||
t.equal(err, fakeError) | ||
t.equal(res, null) | ||
}) | ||
}) | ||
test('compatible with stream.finished', (t) => { | ||
t.plan(3) | ||
const dispatch = function (req, res) { | ||
finished(res, (err) => { | ||
t.ok(err instanceof Error) | ||
}) | ||
req.destroy() | ||
} | ||
inject(dispatch, { method: 'GET', url: '/' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res, null) | ||
}) | ||
}) | ||
test('compatible with eos', (t) => { | ||
t.plan(3) | ||
const dispatch = function (req, res) { | ||
eos(res, (err) => { | ||
t.ok(err instanceof Error) | ||
}) | ||
req.destroy() | ||
} | ||
inject(dispatch, { method: 'GET', url: '/' }, (err, res) => { | ||
t.error(err) | ||
t.equal(res, null) | ||
}) | ||
}) | ||
test('compatible with eos, passes error correctly', (t) => { | ||
t.plan(3) | ||
const fakeError = new Error('some-error') | ||
const dispatch = function (req, res) { | ||
eos(res, (err) => { | ||
t.equal(err, fakeError) | ||
}) | ||
req.destroy(fakeError) | ||
} | ||
inject(dispatch, { method: 'GET', url: '/' }, (err, res) => { | ||
t.equal(err, fakeError) | ||
t.equal(res, null) | ||
}) | ||
}) | ||
test('multiple calls to req.destroy should not be called', (t) => { | ||
t.plan(2) | ||
const dispatch = function (req, res) { | ||
req.destroy() | ||
req.destroy() // twice | ||
} | ||
inject(dispatch, { method: 'GET', url: '/' }, (err, res) => { | ||
t.equal(err) | ||
t.equal(res, null) | ||
}) | ||
}) |
76735
15
2131
8