Git Product home page Git Product logo

mock-http's Introduction

mock-http

NPM version Build Status

Mock http request response

This module provides a mock to the server side request and response classes without the need of creating a socket. The full API as documented on http://nodejs.org/api/http.html is supported.

All methods can be used to mock a client requests on the server as well as server responses such allowing to unit-test e.g. connect middleware.

Table of Contents

Request

Mock implementation of Class http.IncomingMessage

It behaves like the class, apart from really handling a socket. I.e. it implements the Readable Stream Class as well. All methods can be used to mock a client request on the server such allowing to unit-test e.g. connect middleware

Response

Mock implementation of Class http.ServerResponse

It behaves like the class, apart from really handling a socket. I.e. it implements the Writable Stream Class as well. All methods can be used to mock a server response such allowing to unit-test e.g. connect middleware

States are stored in the interal object Response._internal and can be queried from your unit-tests

_internal: {
  headers: {},            // {Object}  Response headers
  trailers: {},           // {Object}  Trailing Response headers
  buffer: new Buffer(''), // {Buffer}  Internal buffer represents response body
  timedout: false,        // {Boolean} If true than `Response.setTimeout` was called.
  ended: false,           // {Boolean} If true than `Response.end` was called.
}

Usage

This is a unit-test using mocha which illustrates the usage. The example can be found in ./test/index.mocha.js

describe('example', function(){
    // a middleware function under test
    var middleware = function(req, res, next) {
        var regex = /^(?:\/test)(\/.*|$)/;
        req.params = '';

        req.on('data', function(data){
            req.params += data; // a simple body parser
        });
        req.on('end', function(){
            if (regex.test(req.url)) {
                req.url = req.url.replace(regex, '$1') || '/';
                res.writeHead(200, { 'Cache-Control': 'max-age=300'});
                res.write('this is a test');
                res.end();
            }
            else {
                next && next();
            }
        });
    };
    it('shall respond with a 200', function(done){
        var req = new mock.Request({
                    url: '/test',
                    method: 'POST',
                    buffer: new Buffer('name=mock&version=first')
                });
        var res = new mock.Response({
                onEnd: function() {
                    // the test ends here
                    assert.equal(req.url, '/');
                    assert.equal(req.params, 'name=mock&version=first');
                    assert.equal(res.statusCode, 200);
                    assert.equal(res.headersSent, true);
                    assert.equal(res.getHeader('Cache-Control'), 'max-age=300');
                    assert.equal(res.hasEnded(), true);
                    done();
                }
            });
        middleware(req, res, function(){
            assert.equal('test never', 'reaches here');
        });
    });
    it('shall call next middleware', function(done){
        var req = new mock.Request({
                    url: '/other',
                    method: 'POST',
                    buffer: new Buffer('name=mock&version=first')
                });
        var res = new mock.Response({
                onEnd: function() {
                    assert.equal('test never', 'reaches here');
                }
            });
        middleware(req, res, function(){
            // the test ends here
            assert.equal(req.url, '/other');
            assert.equal(res.headersSent, false);
            assert.equal(res.hasEnded(), false);
            done();
        });
    });
});

Documentation

Documentation can be found in ./doc.

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work.

  • npm test - runs the tests
  • npm run lint - runs jshint for linting
  • npm run doc - generates the docs in ./doc - requires npm i -g jsdoc

License

Copyright (c) 2014-2015, Commenthol. (MIT License)

See LICENSE for more info.

mock-http's People

Contributors

commenthol avatar

Watchers

 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.