From d2d67322b22fe51516c181e3884811ddf40afee3 Mon Sep 17 00:00:00 2001 From: Jason Loveman Date: Thu, 9 Apr 2015 13:33:42 -0700 Subject: [PATCH 1/6] somewhere in the guts of the sendstream pipe calls (in "fresh"), it directly checks response._headers. Signed-off-by: Jason Loveman --- lib/mockResponse.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/mockResponse.js b/lib/mockResponse.js index d288cc4..eacc555 100644 --- a/lib/mockResponse.js +++ b/lib/mockResponse.js @@ -38,7 +38,6 @@ function createResponse(options) { var _endCalled = false; var _data = ''; - var _headers = {}; var _encoding = options.encoding; var _redirectUrl = ''; @@ -58,6 +57,8 @@ function createResponse(options) { var mockResponse = {}; + mockResponse._headers = {}; + mockResponse.statusCode = 200; mockResponse.cookies = {}; @@ -101,9 +102,9 @@ function createResponse(options) { // Note: Not sure if the headers given in this function // overwrite any headers specified earlier. if (headers) { - _headers = headers; + mockResponse._headers = headers; } else { - _headers = phrase; + mockResponse._headers = phrase; } }; @@ -169,7 +170,7 @@ function createResponse(options) { case 3: _formatData(a); - _headers = b; + mockResponse._headers = b; mockResponse.statusCode = c; console.warn('WARNING: Called send() with deprecated three parameters'); @@ -353,7 +354,7 @@ function createResponse(options) { * Returns a particular header by name. */ mockResponse.get = mockResponse.getHeader = function(name) { - return _headers[name]; + return mockResponse._headers[name]; }; /** @@ -363,7 +364,7 @@ function createResponse(options) { * Set a particular header by name. */ mockResponse.setHeader = function(name, value) { - _headers[name] = value; + mockResponse._headers[name] = value; return value; }; @@ -373,7 +374,7 @@ function createResponse(options) { * Removes an HTTP header by name. */ mockResponse.removeHeader = function(name) { - delete _headers[name]; + delete mockResponse._headers[name]; }; /** @@ -515,7 +516,7 @@ function createResponse(options) { * empty object, but probably will have "Content-Type" set. */ mockResponse._getHeaders = function() { - return _headers; + return mockResponse._headers; }; /** @@ -543,7 +544,7 @@ function createResponse(options) { * It doesn't validate the data that was sent. */ mockResponse._isJSON = function() { - return (_headers['Content-Type'] === 'application/json'); + return (mockResponse._headers['Content-Type'] === 'application/json'); }; /** @@ -580,8 +581,8 @@ function createResponse(options) { */ mockResponse._isDataLengthValid = function() { - if (_headers['Content-Length']) { - return (_headers['Content-Length'].toString() === _data.length.toString()); + if (mockResponse._headers['Content-Length']) { + return (mockResponse._headers['Content-Length'].toString() === _data.length.toString()); } return true; From 3587a574bd65746e5e745dab623711799911ecd2 Mon Sep 17 00:00:00 2001 From: Johnny Estilles Date: Thu, 30 Apr 2015 10:31:00 +0800 Subject: [PATCH 2/6] docs(README): update release note references --- README.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7359826..f2866f1 100644 --- a/README.md +++ b/README.md @@ -103,15 +103,23 @@ by Express. [Most Recent Release Notes](https://github.com/howardabrams/node-mocks-http/releases) -* [v1.4.1](https://github.com/howardabrams/node-mocks-http/releases/tag/v1.4.1) - April 14, 2015 -* [v1.4.0](https://github.com/howardabrams/node-mocks-http/releases/tag/v1.4.0) - April 12, 2015 -* [v1.3.0](https://github.com/howardabrams/node-mocks-http/releases/tag/v1.3.0) - April 5, 2015 -* [v1.2.7](https://github.com/howardabrams/node-mocks-http/releases/tag/v1.2.7) - March 24, 2015 -* [v1.2.6](https://github.com/howardabrams/node-mocks-http/releases/tag/v1.2.6) - March 19, 2015 -* [v1.2.5](https://github.com/howardabrams/node-mocks-http/releases/tag/v1.2.5) - March 5, 2015 +* [v1.4.1][release-v1.4.1] - April 14, 2015 +* [v1.4.0][release-v1.4.0] - April 12, 2015 +* [v1.3.0][release-v1.3.0] - April 5, 2015 +* [v1.2.7][release-v1.2.7] - March 24, 2015 +* [v1.2.6][release-v1.2.6] - March 19, 2015 +* [v1.2.5][release-v1.2.5] - March 5, 2015 License --- Licensed under [MIT](https://github.com/howardabrams/node-mocks-http/blob/master/LICENSE). + +[release-v1.4.2]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.4.2 +[release-v1.4.1]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.4.1 +[release-v1.4.0]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.4.0 +[release-v1.3.0]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.3.0 +[release-v1.2.7]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.2.7 +[release-v1.2.6]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.2.6 +[release-v1.2.5]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.2.5 From 49adc409eca0399433bb21df6d8747b92c345051 Mon Sep 17 00:00:00 2001 From: Johnny Estilles Date: Thu, 30 Apr 2015 10:33:54 +0800 Subject: [PATCH 3/6] chore(version): bump version to 1.4.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1ff126e..0a71f19 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Howard Abrams (http://www.github.com/howardabrams)", "name": "node-mocks-http", "description": "Mock 'http' objects for testing Express routing functions", - "version": "1.4.1", + "version": "1.4.2", "homepage": "https://github.com/howardabrams/node-mocks-http", "bugs": { "url": "https://github.com/howardabrams/node-mocks-http/issues" From 5b259d8ecdb741cfab374a153bc8474c718e9769 Mon Sep 17 00:00:00 2001 From: Johnny Estilles Date: Thu, 30 Apr 2015 10:34:13 +0800 Subject: [PATCH 4/6] docs(README): add release notes reference to 1.4.2 --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f2866f1..d154eb0 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ by Express. [Most Recent Release Notes](https://github.com/howardabrams/node-mocks-http/releases) +* [v1.4.2][release-v1.4.2] - April 30, 2015 * [v1.4.1][release-v1.4.1] - April 14, 2015 * [v1.4.0][release-v1.4.0] - April 12, 2015 * [v1.3.0][release-v1.3.0] - April 5, 2015 From 16dbe0988263787f5eeea53569342a58e9214a56 Mon Sep 17 00:00:00 2001 From: Johnny Estilles Date: Thu, 30 Apr 2015 10:41:54 +0800 Subject: [PATCH 5/6] docs(README): update other url references --- README.md | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d154eb0..a94b091 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,19 @@ -[![node-mocks-http logo](https://raw.githubusercontent.com/wiki/howardabrams/node-mocks-http/images/nmh-logo-200x132.png)](https://github.com/howardabrams/node-mocks-http) +[![node-mocks-http logo][nmh-logo]][nmh-url] --- -[![NPM version](https://badge.fury.io/js/node-mocks-http.png)](https://www.npmjs.com/package/node-mocks-http) -[![Build Status](https://travis-ci.org/howardabrams/node-mocks-http.svg?branch=master)](https://travis-ci.org/howardabrams/node-mocks-http) -[![Gitter chat](https://badges.gitter.im/howardabrams/node-mocks-http.png)](https://gitter.im/howardabrams/node-mocks-http) +[![NPM version][npm-badge]][npm-url] +[![Build Status][travis-badge]][travis-url] +[![Gitter chat][gitter-badge]][gitter-url] -Mock 'http' objects for testing [Express](http://expressjs.com/) +Mock 'http' objects for testing [Express][express-url] routing functions, but could be used for testing any -[Node.js](http://www.nodejs.org) web server applications that have +[Node.js][node-url] web server applications that have code that requires mockups of the `request` and `response` objects. ## Installation This project is available as a -[NPM package](https://www.npmjs.org/package/node-mocks-http). +[NPM package][npm-url]. ```bash $ npm install --save-dev node-mocks-http @@ -86,7 +86,7 @@ of values after calling. We are looking for more volunteers to bring value to this project, including the creation of more objects from the -[HTTP module](http://nodejs.org/docs/latest/api/http.html). +[HTTP module][node-http-module-url]. This project doesn't address all features that must be mocked, but it is a good start. Feel free to send pull requests, @@ -101,7 +101,7 @@ Most releases fix bugs with our mocks or add features similar to the actual `Request` and `Response` objects offered by Node.js and extended by Express. -[Most Recent Release Notes](https://github.com/howardabrams/node-mocks-http/releases) +[Most Recent Release Notes][release-notes] * [v1.4.2][release-v1.4.2] - April 30, 2015 * [v1.4.1][release-v1.4.1] - April 14, 2015 @@ -117,6 +117,24 @@ License Licensed under [MIT](https://github.com/howardabrams/node-mocks-http/blob/master/LICENSE). +[nmh-logo]: https://raw.githubusercontent.com/wiki/howardabrams/node-mocks-http/images/nmh-logo-200x132.png +[nmh-url]: https://github.com/howardabrams/node-mocks-http + +[npm-badge]: https://badge.fury.io/js/node-mocks-http.png +[npm-url]: https://www.npmjs.com/package/node-mocks-http + +[travis-badge]: https://travis-ci.org/howardabrams/node-mocks-http.svg?branch=master +[travis-url]: https://travis-ci.org/howardabrams/node-mocks-http + +[gitter-badge]: https://badges.gitter.im/howardabrams/node-mocks-http.png +[gitter-url]: https://gitter.im/howardabrams/node-mocks-http + +[express-url]: http://expressjs.com/ +[node-url]: http://www.nodejs.org +[node-http-module-url]: http://nodejs.org/docs/latest/api/http.html + +[release-notes]: https://github.com/howardabrams/node-mocks-http/releases + [release-v1.4.2]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.4.2 [release-v1.4.1]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.4.1 [release-v1.4.0]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.4.0 From 41740ab8f2ff55df94accef498a23139cd0303c7 Mon Sep 17 00:00:00 2001 From: Johnny Estilles Date: Thu, 30 Apr 2015 10:56:46 +0800 Subject: [PATCH 6/6] docs(README): add api docs --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index a94b091..6702db0 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,43 @@ exports['routeHandler - Simple testing'] = function(test) { }; ``` +## API +### .createRequest() + +``` +httpMocks.createRequest(options) +``` + +Where options is an object hash with any of the following values: + +option | description | default value +------ | ----------- | ------------- +`method`| request HTTP method | 'GET' +`url` | request URL | '' +`originalUrl` | request original URL | `url` +`path` | request path | '' +`params` | object hash with params | {} +`session` | object hash with session values | `undefined` +`cookies` | object hash with request cookies | {} +`signedCookies` | object hash with signed cookies | `undefined` +`headers` | object hash with request headers | {} +`body` | object hash with body | {} +`query` | object hash with query values | {} +`files` | object hash with values | {} + +### .createResponse() + +```js +httpMocks.createResponse(options) +``` + +Where options is an object hash with any of the following values: + +option | description | default value +------ | ----------- | ------------- +`eventEmitter` | event emitter used by nmh | `mockEventEmitter` +`writableStream` | writable stream used by nmh | `mockWritableStream` + ## Design Decisions We wanted some simple mocks without a large framework.