mock-http
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 internal object Response._internal
and can be queried from your unit-tests
_internal: headers: {} // {Object} Response headers trailers: {} // {Object} Trailing Response headers buffer: 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
;
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 testsnpm run lint
- runs jshint for lintingnpm run doc
- generates the docs in ./doc - requiresnpm i -g jsdoc
License
Copyright (c) 2014-present Commenthol. (MIT License)
See LICENSE for more info.