# mock-req

> Mocks node.js `http.IncomingMessage` (a request). See also `mock-res`.

Latest version **0.2.0** (published 2015-03-04) · MIT license · 0 weekly downloads

## Install

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

## 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.2.0 |
| Published | 2015-03-04 |
| First published | 2014-03-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 32 |
| Author | James Diacono |
| Maintainers | diachedelic |
| Keywords | mock, IncomingMessage, req, request, test, stub |

## Links

- npm: https://www.npmjs.com/package/mock-req
- Repository: https://github.com/diachedelic/mock-req
- Issues: https://github.com/diachedelic/mock-req/issues
- npm.io page: https://npm.io/package/mock-req

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2015-03-04
- 0.1.0 — 2014-03-02

## README

mock-req
========

Mocks node.js `http.IncomingMessage` (a request).  See also `mock-res`.

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

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

	var MockReq = require('mock-req');

	// Basic usage
	var req = new MockReq();

	// With options
	var req = new MockReq({
		method: 'PUT',
		url: '/stuff?q=thing',
		headers: {
			'Accept': 'text/plain'
		},

		// arbitrary properties:
		search: 'thing'
	});

	// Write body
	req.write('hello');
	req.write('world');

	// Or stringify to JSON
	req.write({
		val: 5
	});

	// Or even buffers
	req.write(new Buffer('buf'));

	// End body
	req.end();

	// NOTE req.end() is automatically called if 
	// method is set to GET/HEAD/DELETE.

## Example test case

	var assert = require('assert');
	var ping = require('./ping-handler');
	var MockReq = require('mock-req');

	function test(done) {
		/* Arrange */
		var req = new MockReq({
			method: 'GET',
			url: '/stuff',
			headers: {
				'Accept': 'text/plain'
			}
		});

		// NOTE: `req.end()` is automatically called for GET/HEAD/DELETE methods

		// Use `mock-res` for a better mock
		var res = {
			end: end
		};

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

		/* Assert */
		function end(data) {
			assert.equal(data, 'okay');

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

## Options
The options parameter is optional.

* `method`: The request's method, defaults to 'GET'
* `url`: The request's URL, defaults to ''
* `headers`: A case insensitive name/value object

All other values will be copied to the request.

## Methods

* All readable/writable stream methods.
* `req._fail(error)` Causes the request to emit an `error` when written to.

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