Giter Club home page Giter Club logo

mock-res's Introduction

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

mock-res's People

Contributors

jamesdiacono avatar tschaub avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.