# mock-res

> Mocks node.js http.ServerResponse. See also `mock-req`.

Latest version **0.6.0** (published 2021-10-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install mock-res
pnpm add mock-res
yarn add mock-res
bun add mock-res
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2021-10-25 |
| First published | 2014-03-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 26 |
| Author | James Diacono |
| Maintainers | diachedelic |
| Keywords | mock, http, ServerResponse, response, res |

## Links

- npm: https://www.npmjs.com/package/mock-res
- Repository: https://github.com/diachedelic/mock-res
- Homepage: https://github.com/diachedelic/mock-res#readme
- Issues: https://github.com/diachedelic/mock-res/issues
- npm.io page: https://npm.io/package/mock-res

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.6.0 (latest) — 2021-10-25
- 0.5.0 — 2017-07-07
- 0.4.1 — 2017-05-08
- 0.4.0 — 2017-05-03
- 0.3.3 — 2016-10-07
- 0.3.1 — 2016-09-30
- 0.3.0 — 2015-07-17
- 0.2.1 — 2014-08-01
- 0.2.0 — 2014-07-22
- 0.1.0 — 2014-03-02

## README

mock-res
========

Mocks node.js http.ServerResponse (a response). See also `mock-req`.

Being a readable/writable stream, you can pipe the response body to and from it.

# Usage
See `test.js` for further usage.

	var MockRes = require('mock-res');

	// Basic usage
	var res = new MockRes();

	// Supply a callback to be called after res.end() is called
	var res = new MockRes(function onEnd() {
		console.log('Response sent');
	});

	// Listen for stream events
	res.on('error', function (err) {
		// If not listened for, the 'error' event will throw,
		// as is true for any stream.
	});
	res.on('finish', function () {
		console.log('Finished writing response');
	});

	// Read status code
	res.statusCode; // 200 by default

	// Read body as string
	res._getString(); // 'I am a chicken';

	// Read body as parsed JSON
	res._getJSON(); // { chicken: true }

	// Pipe body somewhere
	res.pipe(fs.createWriteStream('/tmp/yo'));

## Example test case

	var assert = require('assert');
	var list = require('./list-handler');
	var MockRes = require('mock-res');

	function test(done) {
		/* Arrange */

		// Use `mock-req` for a better mock
		var req = {
			method: 'GET',
			url: '/foos'
		}

		var res = new MockRes(onEnd);

		/* Act */
		list(req, res);

		/* Assert */
		function onEnd() {
			// NOTE `this` === `res`

			assert.equal(this.statusCode, 200);
			assert.equal(this._getString(), '[{"id":0},{"id":1}]');
			assert.deepEqual(this._getJSON(), [{id: 0 }, {id: 1 }]);
			assert.deepEqual(this.getHeader('set-cookie'), ['a=1', 'b=2']);

			res.pipe(process.stdout); // `res` is just a readable stream here

			done(); // this is an async test
		}
	}

## Methods

* All readable/writable stream methods.
* `writeHead(statusCode, [reasonPhrase], [headers])` Sets the response status code, status message, and headers.  See also the [`http.ServerResponse` documentation](http://nodejs.org/api/http.html#http_response_writehead_statuscode_reasonphrase_headers).
* `setHeader()`, `getHeader()`, `getHeaders()`, `removeHeader()`
* `_getString()` Reads the body as a string, from the internal stream buffer.
* `_getJSON()` Reads the body as a parsed JSON object, from the internal stream buffer.

---
_Source: https://npm.io/package/mock-res · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
