node-mocks-http
Advanced tools
Comparing version 1.12.2 to 1.13.0
@@ -63,3 +63,3 @@ 'use strict'; | ||
mockRequest.path = options.path || | ||
((options.url ? url.parse(options.url).pathname : '')); | ||
(options.url ? url.parse(options.url).pathname : ''); | ||
mockRequest.params = options.params ? options.params : {}; | ||
@@ -544,2 +544,105 @@ if (options.session) { | ||
/** | ||
* Function: asyncIterator | ||
* | ||
* Buffers data, error, end, and close events and yields them in order. | ||
* Unlike stream.Readable, this async iterator implementation will not exit | ||
* early on error or close. | ||
*/ | ||
mockRequest[Symbol.asyncIterator] = async function* asyncIterator() { | ||
let ended = false; | ||
let closed = false; | ||
let error = null; | ||
let chunks = []; | ||
let resolvePromise = null; | ||
const promiseExecutor = resolve => { | ||
resolvePromise = resolve; | ||
}; | ||
const promiseResolver = () => { | ||
if (resolvePromise) { | ||
resolvePromise(); | ||
resolvePromise = null; | ||
} | ||
}; | ||
const dataEventHandler = chunk => { | ||
if (ended || closed || error) { | ||
return; | ||
} | ||
chunks.push(chunk); | ||
promiseResolver(); | ||
}; | ||
const endEventHandler = () => { | ||
if (ended || closed || error) { | ||
return; | ||
} | ||
ended = true; | ||
promiseResolver(); | ||
}; | ||
const closeEventHandler = () => { | ||
if (closed || error) { | ||
return; | ||
} | ||
closed = true; | ||
promiseResolver(); | ||
}; | ||
const errorEventHandler = err => { | ||
if (closed || error) { | ||
return; | ||
} | ||
error = err; | ||
promiseResolver(); | ||
}; | ||
mockRequest.on('data', dataEventHandler); | ||
mockRequest.on('end', endEventHandler); | ||
mockRequest.on('close', closeEventHandler); | ||
mockRequest.on('error', errorEventHandler); | ||
// Emit custom event after entering the loop. | ||
setTimeout(() => { | ||
this.emit('async_iterator'); | ||
}); | ||
try { | ||
for (;;) { | ||
await new Promise(promiseExecutor); | ||
let i = 0; | ||
for (;;) { | ||
if (error) { | ||
throw error; | ||
} | ||
if (closed) { | ||
return; | ||
} | ||
const hasChunks = i < chunks.length; | ||
if (!hasChunks) { | ||
if (ended) { | ||
// End signaled. Bail. | ||
return; | ||
} | ||
// Wait for next push. | ||
break; | ||
} | ||
const chunk = chunks[i]; | ||
chunks[i] = undefined; | ||
i += 1; | ||
yield chunk; | ||
} | ||
chunks.length = 0; | ||
} | ||
} finally { | ||
chunks.length = 0; | ||
error = null; | ||
mockRequest.off('data', dataEventHandler); | ||
mockRequest.off('end', endEventHandler); | ||
mockRequest.off('close', closeEventHandler); | ||
mockRequest.off('error', errorEventHandler); | ||
} | ||
}; | ||
return mockRequest; | ||
@@ -546,0 +649,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "Mock 'http' objects for testing Express routing functions", | ||
"version": "1.12.2", | ||
"version": "1.13.0", | ||
"homepage": "https://github.com/howardabrams/node-mocks-http", | ||
@@ -42,3 +42,3 @@ "bugs": { | ||
"engines": { | ||
"node": ">=0.6" | ||
"node": ">=14" | ||
}, | ||
@@ -64,3 +64,3 @@ "dependencies": { | ||
"gulp-istanbul": "^1.1.3", | ||
"gulp-mocha": "^7.0.2", | ||
"gulp-mocha": "^8.0.0", | ||
"istanbul": "^0.4.5", | ||
@@ -67,0 +67,0 @@ "mocha": "^8.1.1", |
@@ -1,6 +0,5 @@ | ||
[![node-mocks-http logo][nmh-logo]][nmh-url] | ||
[![Logo](https://user-images.githubusercontent.com/895071/227720269-37e34019-eba0-4768-80ab-1a4dad043043.png)](https://github.com/eugef/node-mocks-http) | ||
--- | ||
[![NPM version][npm-badge]][npm-url] | ||
[![Build Status][travis-badge]][travis-url] | ||
[![Gitter chat][gitter-badge]][gitter-url] | ||
@@ -216,4 +215,3 @@ | ||
[release-notes]: https://github.com/howardabrams/node-mocks-http/releases | ||
[release-v1.4.4]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.4.4 | ||
[release-notes]: https://github.com/eugef/node-mocks-http/releases | ||
@@ -223,16 +221,7 @@ License | ||
Licensed under [MIT](https://github.com/howardabrams/node-mocks-http/blob/master/LICENSE). | ||
Licensed under [MIT](LICENSE). | ||
[nmh-logo]: https://raw.githubusercontent.com/wiki/howardabrams/node-mocks-http/images/nmh-logo-200x132.png | ||
[nmh-url]: https://github.com/howardabrams/node-mocks-http | ||
[npm-badge]: https://badge.fury.io/js/node-mocks-http.png | ||
[npm-url]: https://www.npmjs.com/package/node-mocks-http | ||
[travis-badge]: https://travis-ci.org/howardabrams/node-mocks-http.svg?branch=master | ||
[travis-url]: https://travis-ci.org/howardabrams/node-mocks-http | ||
[gitter-badge]: https://badges.gitter.im/howardabrams/node-mocks-http.png | ||
[gitter-url]: https://gitter.im/howardabrams/node-mocks-http | ||
[express-url]: http://expressjs.com/ | ||
@@ -239,0 +228,0 @@ [koa-url]: https://koajs.com/ |
74750
2045
229