Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mock-res

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-res

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

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
42K
decreased by-11.36%
Maintainers
1
Weekly downloads
 
Created
Source

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();

// With callback for 'finish' event
var res = new MockRes(function() {
	console.log('Response finished');
});

// Or listen for stream events
res.on('error', function(err) {
	console.error('Error: %s', err.stack);

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

// 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(finish);

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

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

		assert.equal(this.statusCode, 200);
		assert.equal(this._getString(), '[{"id":0},{"id":1}]');
		assert.deepEqual(this._getJSON(), [{id: 0 }, {id: 1 }]);

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

Keywords

FAQs

Package last updated on 17 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc