File: README.md

package info (click to toggle)
node-mocks-http 1.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 292 kB
  • ctags: 240
  • sloc: makefile: 4; sh: 2
file content (207 lines) | stat: -rw-r--r-- 6,386 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
[![node-mocks-http logo][nmh-logo]][nmh-url]
---
[![NPM version][npm-badge]][npm-url]
[![Build Status][travis-badge]][travis-url]
[![Gitter chat][gitter-badge]][gitter-url]


Mock 'http' objects for testing [Express][express-url]
routing functions, but could be used for testing any
[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][npm-url].

```bash
$ npm install --save-dev node-mocks-http
```

> Our example includes `--save-dev` based on the assumption that **node-mocks-http** will be used as a development dependency..

After installing the package include the following in your test files:

```js
var httpMocks = require('node-mocks-http');
```

## Usage

Suppose you have the following Express route:

```js
app.get('/user/:id', routeHandler);
```

And you have created a function to handle that route's call:

```js
var routeHandler = function( request, response ) { ... };
```

You can easily test the `routeHandler` function with some code like
this using the testing framework of your choice:

```js
exports['routeHandler - Simple testing'] = function(test) {

    var request  = httpMocks.createRequest({
        method: 'GET',
        url: '/user/42',
        params: {
          id: 42
        }
    });

    var response = httpMocks.createResponse();

    routeHandler(request, response);

    var data = JSON.parse( response._getData() );
    test.equal("Bob Dog", data.name);
    test.equal(42, data.age);
    test.equal("bob@dog.com", data.email);

    test.equal(200, response.statusCode );
    test.ok( response._isEndCalled());
    test.ok( response._isJSON());
    test.ok( response._isUTF8());

    test.done();

};
```

## 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 `response` object | `mockEventEmitter`
`writableStream`  | writable stream used by `response` object | `mockWritableStream`

> NOTE: The out-of-the-box mock event emitter included with `node-mocks-http` is
not a functional event emitter and as such does not actually emit events. If you
wish to test your event handlers you will need to bring your own event emitter.

> Here's an example:

```js
var httpMocks = require('node-mocks-http');
var res = httpMocks.createResponse({
  eventEmitter: require('events').EventEmitter
});

// ...
  it('should do something', function(done) {
    res.on('end', function() {
      assert.equal(...);
      done();
    });
  });
// ...
```

## Design Decisions

We wanted some simple mocks without a large framework.

We also wanted the mocks to act like the original framework being
mocked, but allow for setting of values before calling and inspecting
of values after calling.

## For Developers

We are looking for more volunteers to bring value to this project,
including the creation of more objects from the
[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,
and a member of the team will be timely in merging them.

If you wish to contribute please read our [Contributing Guidelines](CONTRIBUTING.md).


## Release Notes

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][release-notes]

* [v1.4.4][release-v1.4.4] - June 3, 2015
* [v1.4.3][release-v1.4.3] - June 3, 2015
* [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
* [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).

[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.4]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.4.4
[release-v1.4.3]: https://github.com/howardabrams/node-mocks-http/releases/tag/v1.4.3
[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