mock-res
Mocks node.js http.ServerResponse. See also `mock-req`.
Last updated 4 years ago by diachedelic .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mock-res 
SYNC missed versions from official npm registry.

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.
  • 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.

Current Tags

  • 0.6.0                                ...           latest (4 years ago)

10 Versions

  • 0.6.0                                ...           4 years ago
  • 0.5.0                                ...           9 years ago
  • 0.4.1                                ...           9 years ago
  • 0.4.0                                ...           9 years ago
  • 0.3.3                                ...           10 years ago
  • 0.3.1                                ...           10 years ago
  • 0.3.0                                ...           11 years ago
  • 0.2.1                                ...           12 years ago
  • 0.2.0                                ...           12 years ago
  • 0.1.0                                ...           12 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
None
Dev Dependencies (0)
None
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |