nock 9.2.1 → 9.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -43
- package/lib/interceptor.js +1 -1
- package/lib/match_body.js +38 -7
- package/lib/recorder.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://travis-ci.org/node-nock/nock)
|
|
4
4
|
[](https://coveralls.io/github/node-nock/nock?branch=master)
|
|
5
|
-
[](https://gitter.im/node-nock/nock)
|
|
5
|
+
[](https://greenkeeper.io/)
|
|
7
6
|
|
|
8
7
|
|
|
9
8
|
Nock is an HTTP mocking and expectations library for Node.js
|
|
@@ -188,64 +187,51 @@ var scope = nock('http://www.example.com')
|
|
|
188
187
|
|
|
189
188
|
## Specifying request body
|
|
190
189
|
|
|
191
|
-
You can specify the request body to be matched as the second argument to the `get`, `post`, `put` or `delete` specifications
|
|
190
|
+
You can specify the request body to be matched as the second argument to the `get`, `post`, `put` or `delete` specifications. There are four types of second argument allowed:
|
|
191
|
+
|
|
192
|
+
**String**: nock will exact match the stringified request body with the provided string
|
|
192
193
|
|
|
193
194
|
```js
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
email: 'pedro.teixeira@gmail.com'
|
|
198
|
-
})
|
|
199
|
-
.reply(201, {
|
|
200
|
-
ok: true,
|
|
201
|
-
id: '123ABC',
|
|
202
|
-
rev: '946B7D1C'
|
|
203
|
-
});
|
|
195
|
+
nock('http://www.example.com')
|
|
196
|
+
.post('/login', 'username=pgte&password=123456')
|
|
197
|
+
.reply(200, { id: '123ABC' });
|
|
204
198
|
```
|
|
205
199
|
|
|
206
|
-
|
|
200
|
+
**RegExp**: nock will test the stringified request body against the provided RegExp
|
|
207
201
|
|
|
208
202
|
```js
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
ok: true,
|
|
213
|
-
id: '123ABC',
|
|
214
|
-
rev: '946B7D1C'
|
|
215
|
-
});
|
|
203
|
+
nock('http://www.example.com')
|
|
204
|
+
.post('/login', /username=\w+/gi)
|
|
205
|
+
.reply(200, { id: '123ABC' });
|
|
216
206
|
```
|
|
217
207
|
|
|
218
|
-
|
|
208
|
+
**JSON object**: nock will exact match the request body with the provided object. In order to increase flexibility, nock also supports RegExp as an attribute value for the keys:
|
|
219
209
|
|
|
220
210
|
```js
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
password: /a.+/,
|
|
225
|
-
email: 'pedro.teixeira@gmail.com'
|
|
226
|
-
})
|
|
227
|
-
.reply(201, {
|
|
228
|
-
ok: true,
|
|
229
|
-
id: '123ABC',
|
|
230
|
-
rev: '946B7D1C'
|
|
231
|
-
});
|
|
211
|
+
nock('http://www.example.com')
|
|
212
|
+
.post('/login', { username: 'pgte', password: /.+/i })
|
|
213
|
+
.reply(200, { id: '123ABC' });
|
|
232
214
|
```
|
|
233
215
|
|
|
234
|
-
|
|
216
|
+
**Function**: nock will evaluate the function providing the request body object as first argument. Return true if it should be considered a match:
|
|
235
217
|
|
|
236
218
|
```js
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
});
|
|
219
|
+
nock('http://www.example.com')
|
|
220
|
+
.post('/login', function(body) {
|
|
221
|
+
return body.username && body.password;
|
|
222
|
+
})
|
|
223
|
+
.reply(200, { id: '123ABC' });
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
In case you need to perform a partial matching on a complex, nested request body you should have a look at libraries like [lodash.matches](https://lodash.com/docs/#matches). Indeed, partial matching can be achieved as:
|
|
246
227
|
|
|
228
|
+
```js
|
|
229
|
+
nock('http://www.example.com')
|
|
230
|
+
.post('/user', _.matches({ address: { country: 'US' } }))
|
|
231
|
+
.reply(200, { id: '123ABC' });
|
|
247
232
|
```
|
|
248
233
|
|
|
234
|
+
|
|
249
235
|
## Specifying request query string
|
|
250
236
|
|
|
251
237
|
Nock understands query strings. Instead of placing the entire URL, you can specify the query part as an object:
|
package/lib/interceptor.js
CHANGED
|
@@ -334,7 +334,7 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
|
|
|
334
334
|
};
|
|
335
335
|
|
|
336
336
|
Interceptor.prototype.matchIndependentOfBody = function matchIndependentOfBody(options) {
|
|
337
|
-
var isRegex = _.isRegExp(this.
|
|
337
|
+
var isRegex = _.isRegExp(this.path);
|
|
338
338
|
|
|
339
339
|
var method = (options.method || 'GET').toUpperCase()
|
|
340
340
|
, path = options.path
|
package/lib/match_body.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var deepEqual = require('deep-equal');
|
|
4
4
|
var qs = require('qs');
|
|
5
|
+
var _ = require('lodash')
|
|
5
6
|
|
|
6
7
|
module.exports =
|
|
7
8
|
function matchBody(spec, body) {
|
|
@@ -14,10 +15,14 @@ function matchBody(spec, body) {
|
|
|
14
15
|
body = body.toString();
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
var contentType =
|
|
18
|
-
|
|
18
|
+
var contentType = (
|
|
19
|
+
options.headers &&
|
|
20
|
+
(options.headers['Content-Type'] || options.headers['content-type']) ||
|
|
21
|
+
''
|
|
22
|
+
).toString();
|
|
19
23
|
|
|
20
|
-
var isMultipart = contentType
|
|
24
|
+
var isMultipart = contentType.indexOf('multipart') >= 0;
|
|
25
|
+
var isUrlencoded = contentType.indexOf('application/x-www-form-urlencoded') >= 0;
|
|
21
26
|
|
|
22
27
|
// try to transform body to json
|
|
23
28
|
var json;
|
|
@@ -25,10 +30,8 @@ function matchBody(spec, body) {
|
|
|
25
30
|
try { json = JSON.parse(body);} catch(err) {}
|
|
26
31
|
if (json !== undefined) {
|
|
27
32
|
body = json;
|
|
28
|
-
} else {
|
|
29
|
-
|
|
30
|
-
body = qs.parse(body, { allowDots: true });
|
|
31
|
-
}
|
|
33
|
+
} else if (isUrlencoded) {
|
|
34
|
+
body = qs.parse(body, { allowDots: true });
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
|
|
@@ -54,9 +57,37 @@ function matchBody(spec, body) {
|
|
|
54
57
|
spec = spec.replace(/\r?\n|\r/g, '');
|
|
55
58
|
}
|
|
56
59
|
|
|
60
|
+
if (isUrlencoded) {
|
|
61
|
+
spec = mapValuesDeep(spec, function(val) {
|
|
62
|
+
if (_.isRegExp(val)) {
|
|
63
|
+
return val
|
|
64
|
+
}
|
|
65
|
+
return val + ''
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
57
69
|
return deepEqualExtended(spec, body);
|
|
58
70
|
};
|
|
59
71
|
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Based on lodash issue discussion
|
|
75
|
+
* https://github.com/lodash/lodash/issues/1244
|
|
76
|
+
*/
|
|
77
|
+
function mapValuesDeep(obj, cb) {
|
|
78
|
+
if (_.isArray(obj)) {
|
|
79
|
+
return obj.map(function(v) {
|
|
80
|
+
return mapValuesDeep(v, cb)
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
if (_.isPlainObject(obj)) {
|
|
84
|
+
return _.mapValues(obj, function(v) {
|
|
85
|
+
return mapValuesDeep(v, cb)
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
return cb(obj)
|
|
89
|
+
}
|
|
90
|
+
|
|
60
91
|
function deepEqualExtended(spec, body) {
|
|
61
92
|
if (spec && spec.constructor === RegExp) {
|
|
62
93
|
return spec.test(body);
|
package/lib/recorder.js
CHANGED
|
@@ -74,11 +74,11 @@ var getBodyFromChunks = function(chunks, headers) {
|
|
|
74
74
|
// 2. A string buffer which represents a JSON object.
|
|
75
75
|
// 3. A string buffer which doesn't represent a JSON object.
|
|
76
76
|
|
|
77
|
-
var isBinary = common.isBinaryBuffer(mergedBuffer)
|
|
77
|
+
var isBinary = common.isBinaryBuffer(mergedBuffer);
|
|
78
78
|
if(isBinary) {
|
|
79
79
|
return {
|
|
80
80
|
body: mergedBuffer.toString('hex'),
|
|
81
|
-
isBinary
|
|
81
|
+
isBinary: true
|
|
82
82
|
}
|
|
83
83
|
} else {
|
|
84
84
|
var maybeStringifiedJson = mergedBuffer.toString('utf8');
|