nock 2.9.1 → 2.13.0
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/.travis.yml +4 -1
- package/README.md +2 -2
- package/coverage/coverage.json +1 -1
- package/coverage/lcov-report/index.html +14 -14
- package/coverage/lcov-report/nock/index.html +1 -1
- package/coverage/lcov-report/nock/index.js.html +1 -1
- package/coverage/lcov-report/nock/lib/back.js.html +1 -1
- package/coverage/lcov-report/nock/lib/common.js.html +65 -65
- package/coverage/lcov-report/nock/lib/delayed_body.js.html +1 -1
- package/coverage/lcov-report/nock/lib/index.html +21 -21
- package/coverage/lcov-report/nock/lib/intercept.js.html +71 -71
- package/coverage/lcov-report/nock/lib/match_body.js.html +3 -3
- package/coverage/lcov-report/nock/lib/mixin.js.html +1 -1
- package/coverage/lcov-report/nock/lib/recorder.js.html +178 -112
- package/coverage/lcov-report/nock/lib/request_overrider.js.html +127 -127
- package/coverage/lcov-report/nock/lib/scope.js.html +328 -76
- package/coverage/lcov-report/nock/lib/socket.js.html +12 -12
- package/coverage/lcov.info +1225 -1142
- package/lib/recorder.js +10 -2
- package/lib/request_overrider.js +11 -1
- package/lib/scope.js +25 -10
- package/lib/socket.js +7 -1
- package/package.json +9 -4
- package/tests/test_back.js +3 -3
- package/tests/test_basic_auth.js +50 -13
- package/tests/test_data.js +1 -2
- package/tests/test_dynamic_mock.js +0 -2
- package/tests/test_intercept.js +19 -1
package/lib/recorder.js
CHANGED
|
@@ -360,7 +360,8 @@ function record(rec_options) {
|
|
|
360
360
|
|
|
361
361
|
// Since we gave client the chance to setup its listeners
|
|
362
362
|
// before us, we need to remove them and setup our own.
|
|
363
|
-
|
|
363
|
+
var dataListeners = res.listeners('data');
|
|
364
|
+
_.each(dataListeners, function(listener) {
|
|
364
365
|
res.removeListener('data', listener);
|
|
365
366
|
});
|
|
366
367
|
|
|
@@ -370,6 +371,10 @@ function record(rec_options) {
|
|
|
370
371
|
data = new Buffer(data, encoding);
|
|
371
372
|
}
|
|
372
373
|
dataChunks.push(data);
|
|
374
|
+
// Manually invoke the user listeners emulating 'data' event.
|
|
375
|
+
_.each(dataListeners, function(listener) {
|
|
376
|
+
listener(data);
|
|
377
|
+
});
|
|
373
378
|
};
|
|
374
379
|
res.on('data', onData);
|
|
375
380
|
}
|
|
@@ -383,10 +388,13 @@ function record(rec_options) {
|
|
|
383
388
|
});
|
|
384
389
|
|
|
385
390
|
var oldWrite = req.write;
|
|
386
|
-
req.write = function(data) {
|
|
391
|
+
req.write = function(data, encoding) {
|
|
387
392
|
if ('undefined' !== typeof(data)) {
|
|
388
393
|
if (data) {
|
|
389
394
|
debug(thisRecordingId, 'new', proto, 'body chunk');
|
|
395
|
+
if (! Buffer.isBuffer(data)) {
|
|
396
|
+
data = new Buffer(data, encoding);
|
|
397
|
+
}
|
|
390
398
|
bodyChunks.push(data);
|
|
391
399
|
}
|
|
392
400
|
oldWrite.call(req, data);
|
package/lib/request_overrider.js
CHANGED
|
@@ -133,7 +133,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
133
133
|
return getHeader(req, name);
|
|
134
134
|
};
|
|
135
135
|
|
|
136
|
-
req.socket = response.socket = Socket();
|
|
136
|
+
req.socket = response.socket = Socket({ proto: options.proto });
|
|
137
137
|
|
|
138
138
|
req.write = function(buffer, encoding) {
|
|
139
139
|
debug('write', arguments);
|
|
@@ -273,6 +273,8 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
273
273
|
}
|
|
274
274
|
response.statusCode = Number(interceptor.statusCode) || 200;
|
|
275
275
|
response.headers = interceptor.headers || {};
|
|
276
|
+
response.rawHeaders = interceptor.rawHeaders || [];
|
|
277
|
+
debug('response.rawHeaders:', response.rawHeaders);
|
|
276
278
|
|
|
277
279
|
// We again set request headers, now for our matched interceptor.
|
|
278
280
|
setRequestHeaders(req, options, interceptor);
|
|
@@ -406,6 +408,14 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
406
408
|
}
|
|
407
409
|
});
|
|
408
410
|
|
|
411
|
+
for(var rawHeaderIndex = 0 ; rawHeaderIndex < response.rawHeaders.length ; rawHeaderIndex += 2) {
|
|
412
|
+
var value = response.rawHeaders[rawHeaderIndex + 1];
|
|
413
|
+
if (typeof value === "function") {
|
|
414
|
+
response.rawHeaders[rawHeaderIndex + 1] = value(req, response, responseBody);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
|
|
409
419
|
process.nextTick(function() {
|
|
410
420
|
var respond = function() {
|
|
411
421
|
debug('emitting response');
|
package/lib/scope.js
CHANGED
|
@@ -103,13 +103,17 @@ function startScope(basePath, options) {
|
|
|
103
103
|
|
|
104
104
|
if (headers !== undefined) {
|
|
105
105
|
this.headers = {};
|
|
106
|
+
this.rawHeaders = [];
|
|
106
107
|
|
|
107
108
|
// makes sure all keys in headers are in lower case
|
|
108
109
|
for (var key2 in headers) {
|
|
109
110
|
if (headers.hasOwnProperty(key2)) {
|
|
110
111
|
this.headers[key2.toLowerCase()] = headers[key2];
|
|
112
|
+
this.rawHeaders.push(key2);
|
|
113
|
+
this.rawHeaders.push(headers[key2]);
|
|
111
114
|
}
|
|
112
115
|
}
|
|
116
|
+
debug('reply.rawHeaders:', this.rawHeaders);
|
|
113
117
|
}
|
|
114
118
|
|
|
115
119
|
// If the content is not encoded we may need to transform the response body.
|
|
@@ -265,16 +269,27 @@ function startScope(basePath, options) {
|
|
|
265
269
|
|
|
266
270
|
// Only check for query string matches if this.queries is an object
|
|
267
271
|
if (_.isObject(this.queries)) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
272
|
+
// Make sure that you have an equal number of keys. We are
|
|
273
|
+
// looping through the passed query params and not the expected values
|
|
274
|
+
// if the user passes fewer query params than expected but all values
|
|
275
|
+
// match this will throw a false positive. Testing that the length of the
|
|
276
|
+
// passed query params is equal to the length of expected keys will prevent
|
|
277
|
+
// us from doing any value checking BEFORE we know if they have all the proper
|
|
278
|
+
// params
|
|
279
|
+
if (Object.keys(this.queries).length !== queries.length) {
|
|
280
|
+
matchQueries = false;
|
|
281
|
+
} else {
|
|
282
|
+
for (var i = 0; i < queries.length; i++) {
|
|
283
|
+
var query = queries[i].split('=');
|
|
284
|
+
|
|
285
|
+
if (query[1] === undefined || this.queries[ query[0] ] === undefined) {
|
|
286
|
+
matchQueries = false;
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
var isMatch = matchStringOrRegexp(query[1], this.queries[ query[0] ]);
|
|
291
|
+
matchQueries = matchQueries && !!isMatch;
|
|
274
292
|
}
|
|
275
|
-
|
|
276
|
-
var isMatch = matchStringOrRegexp(query[1], this.queries[ query[0] ]);
|
|
277
|
-
matchQueries = matchQueries && !!isMatch;
|
|
278
293
|
}
|
|
279
294
|
}
|
|
280
295
|
|
|
@@ -352,7 +367,7 @@ function startScope(basePath, options) {
|
|
|
352
367
|
|
|
353
368
|
function basicAuth(options) {
|
|
354
369
|
var username = options['user'];
|
|
355
|
-
var password = options['pass'];
|
|
370
|
+
var password = options['pass'] || '';
|
|
356
371
|
var name = 'authorization';
|
|
357
372
|
var value = 'Basic ' + new Buffer(username + ':' + password).toString('base64');
|
|
358
373
|
interceptorMatchHeaders.push({ name: name, value: value });
|
package/lib/socket.js
CHANGED
|
@@ -5,9 +5,15 @@ var EventEmitter = require('events').EventEmitter,
|
|
|
5
5
|
|
|
6
6
|
module.exports = Socket;
|
|
7
7
|
|
|
8
|
-
function Socket() {
|
|
8
|
+
function Socket(options) {
|
|
9
9
|
var socket = new EventEmitter();
|
|
10
10
|
|
|
11
|
+
options = options || {};
|
|
12
|
+
|
|
13
|
+
if (options.proto === 'https') {
|
|
14
|
+
socket.authorized = true;
|
|
15
|
+
}
|
|
16
|
+
|
|
11
17
|
socket.writable = true;
|
|
12
18
|
socket.readable = true;
|
|
13
19
|
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"testing",
|
|
8
8
|
"isolation"
|
|
9
9
|
],
|
|
10
|
-
"version": "2.
|
|
10
|
+
"version": "2.13.0",
|
|
11
11
|
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
|
|
12
12
|
"contributors": [
|
|
13
13
|
{
|
|
@@ -131,6 +131,11 @@
|
|
|
131
131
|
"name": "Keith Laban",
|
|
132
132
|
"url": "https://github.com/kelaban",
|
|
133
133
|
"email": "kelaban17@gmail.com"
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
"name": "Rui Marinho",
|
|
137
|
+
"url": "https://github.com/ruimarinho",
|
|
138
|
+
"email": "ruipmarinho@gmail.com"
|
|
134
139
|
}
|
|
135
140
|
],
|
|
136
141
|
"repository": {
|
|
@@ -145,7 +150,7 @@
|
|
|
145
150
|
],
|
|
146
151
|
"main": "./index",
|
|
147
152
|
"dependencies": {
|
|
148
|
-
"chai": ">=1.9.2 <
|
|
153
|
+
"chai": ">=1.9.2 <4.0.0",
|
|
149
154
|
"debug": "^1.0.4",
|
|
150
155
|
"deep-equal": "^1.0.0",
|
|
151
156
|
"lodash": "2.4.1",
|
|
@@ -160,7 +165,7 @@
|
|
|
160
165
|
"jshint": "^2.5.6",
|
|
161
166
|
"needle": "^0.7.1",
|
|
162
167
|
"pre-commit": "0.0.9",
|
|
163
|
-
"request": "2.
|
|
168
|
+
"request": "2.61.0",
|
|
164
169
|
"restify": "^2.8.1",
|
|
165
170
|
"restler": "3.2.2",
|
|
166
171
|
"rimraf": "^2.3.2",
|
|
@@ -170,7 +175,7 @@
|
|
|
170
175
|
"scripts": {
|
|
171
176
|
"test": "node tests/test.js",
|
|
172
177
|
"coverage": "istanbul cover tests/test.js && istanbul check-coverage",
|
|
173
|
-
"coveralls": "
|
|
178
|
+
"coveralls": "cat ./coverage/lcov.info | coveralls && rm -rf ./coverage",
|
|
174
179
|
"jshint": "jshint lib/*.js"
|
|
175
180
|
},
|
|
176
181
|
"pre-commit": [
|
package/tests/test_back.js
CHANGED
|
@@ -105,7 +105,7 @@ tap.test('nockBack dryrun tests', function (nw) {
|
|
|
105
105
|
|
|
106
106
|
// Manually disable net connectivity to confirm that dryrun enables it.
|
|
107
107
|
nock.disableNetConnect();
|
|
108
|
-
|
|
108
|
+
|
|
109
109
|
nockBack.fixtures = __dirname + '/fixtures';
|
|
110
110
|
nockBack.setMode('dryrun');
|
|
111
111
|
|
|
@@ -116,7 +116,7 @@ tap.test('nockBack dryrun tests', function (nw) {
|
|
|
116
116
|
, port: 80
|
|
117
117
|
}, function(res) {
|
|
118
118
|
|
|
119
|
-
t.
|
|
119
|
+
t.ok([200, 302].indexOf(res.statusCode) >= 0);
|
|
120
120
|
t.end();
|
|
121
121
|
|
|
122
122
|
});
|
|
@@ -156,7 +156,7 @@ tap.test('nockBack dryrun tests', function (nw) {
|
|
|
156
156
|
, port: 80
|
|
157
157
|
}, function(res) {
|
|
158
158
|
|
|
159
|
-
t.
|
|
159
|
+
t.ok([200, 302].indexOf(res.statusCode) >= 0);
|
|
160
160
|
res.on('end', function() {
|
|
161
161
|
var doneFails = false;
|
|
162
162
|
|
package/tests/test_basic_auth.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var test = require('tap').test;
|
|
4
|
-
var mikealRequest = require('request');
|
|
5
3
|
var nock = require('../');
|
|
4
|
+
var request = require('request');
|
|
5
|
+
var test = require('tap').test;
|
|
6
|
+
|
|
7
|
+
test('basic auth with username and password', function(t) {
|
|
8
|
+
t.plan(2);
|
|
6
9
|
|
|
7
|
-
test("basic auth", function(t) {
|
|
8
10
|
nock('http://super-secure.com')
|
|
9
11
|
.get('/test')
|
|
10
12
|
.basicAuth({
|
|
@@ -13,8 +15,8 @@ test("basic auth", function(t) {
|
|
|
13
15
|
})
|
|
14
16
|
.reply(200, 'Here is the content');
|
|
15
17
|
|
|
16
|
-
t.test('
|
|
17
|
-
|
|
18
|
+
t.test('succeeds when it matches', function (tt) {
|
|
19
|
+
request({
|
|
18
20
|
url: 'http://super-secure.com/test',
|
|
19
21
|
auth: {
|
|
20
22
|
user: 'foo',
|
|
@@ -24,19 +26,54 @@ test("basic auth", function(t) {
|
|
|
24
26
|
if (err) {
|
|
25
27
|
throw err;
|
|
26
28
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
tt.equal(res.statusCode, 200);
|
|
30
|
+
tt.equal(body, 'Here is the content');
|
|
31
|
+
tt.end();
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
t.test('fails when it doesnt match', function (tt) {
|
|
36
|
+
request({
|
|
37
|
+
url: 'http://super-secure.com/test',
|
|
38
|
+
}, function(err, res, body) {
|
|
39
|
+
tt.type(err, 'Error');
|
|
40
|
+
tt.end();
|
|
30
41
|
});
|
|
31
42
|
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('basic auth with username only', function(t) {
|
|
46
|
+
t.plan(2);
|
|
47
|
+
|
|
48
|
+
nock('http://super-secure.com')
|
|
49
|
+
.get('/test')
|
|
50
|
+
.basicAuth({
|
|
51
|
+
user: 'foo'
|
|
52
|
+
})
|
|
53
|
+
.reply(200, 'Here is the content');
|
|
32
54
|
|
|
33
|
-
t.test('
|
|
34
|
-
|
|
55
|
+
t.test('succeeds when it matches', function (tt) {
|
|
56
|
+
request({
|
|
35
57
|
url: 'http://super-secure.com/test',
|
|
58
|
+
auth: {
|
|
59
|
+
user: 'foo'
|
|
60
|
+
}
|
|
36
61
|
}, function(err, res, body) {
|
|
37
|
-
|
|
38
|
-
|
|
62
|
+
if (err) {
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
tt.equal(res.statusCode, 200);
|
|
66
|
+
tt.equal(body, 'Here is the content');
|
|
67
|
+
tt.end();
|
|
39
68
|
});
|
|
40
69
|
});
|
|
41
70
|
|
|
42
|
-
|
|
71
|
+
t.test('fails when it doesnt match', function (tt) {
|
|
72
|
+
request({
|
|
73
|
+
url: 'http://super-secure.com/test',
|
|
74
|
+
}, function(err, res, body) {
|
|
75
|
+
tt.type(err, 'Error');
|
|
76
|
+
tt.end();
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
});
|
package/tests/test_data.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
var nock = require('../');
|
|
2
|
-
var assert = require('assert');
|
|
3
2
|
var http = require('http');
|
|
4
3
|
|
|
5
4
|
var tap = require('tap')
|
|
@@ -19,7 +18,7 @@ tap.test('data emits', function(t) {
|
|
|
19
18
|
});
|
|
20
19
|
|
|
21
20
|
res.on('end', function() {
|
|
22
|
-
|
|
21
|
+
t.same(JSON.parse(body), reqBody);
|
|
23
22
|
t.end();
|
|
24
23
|
});
|
|
25
24
|
|
|
@@ -4,8 +4,6 @@ var test = require('tap').test;
|
|
|
4
4
|
var nock = require('../.');
|
|
5
5
|
var request = require('request');
|
|
6
6
|
|
|
7
|
-
nock.disableNetConnect();
|
|
8
|
-
|
|
9
7
|
test('one function returning the body defines a full mock', function(t) {
|
|
10
8
|
var scope = nock('http://acompleteandfullmock.io')
|
|
11
9
|
.get('/abc')
|
package/tests/test_intercept.js
CHANGED
|
@@ -625,6 +625,7 @@ test("reply headers as function work", function(t) {
|
|
|
625
625
|
path: '/'
|
|
626
626
|
}, function (res) {
|
|
627
627
|
t.equivalent(res.headers, { 'x-my-headers': 'boo!' });
|
|
628
|
+
t.equivalent(res.rawHeaders, ['x-my-headers', 'boo!']); // 67
|
|
628
629
|
t.end();
|
|
629
630
|
});
|
|
630
631
|
});
|
|
@@ -3224,7 +3225,7 @@ test('fix #146 - resume() is automatically invoked when the response is drained'
|
|
|
3224
3225
|
t.notOk(err);
|
|
3225
3226
|
t.ok(res);
|
|
3226
3227
|
t.ok(buffer);
|
|
3227
|
-
t.
|
|
3228
|
+
t.same(buffer, replyBuffer);
|
|
3228
3229
|
t.end();
|
|
3229
3230
|
});
|
|
3230
3231
|
});
|
|
@@ -4098,6 +4099,23 @@ test('query() will not match when a query string is malformed', function (t) {
|
|
|
4098
4099
|
})
|
|
4099
4100
|
});
|
|
4100
4101
|
|
|
4102
|
+
test('query() will not match when a query string has fewer correct values than expected', function (t) {
|
|
4103
|
+
var scope = nock('http://google.com')
|
|
4104
|
+
.get('/')
|
|
4105
|
+
.query({
|
|
4106
|
+
num:1,
|
|
4107
|
+
bool:true,
|
|
4108
|
+
empty:null,
|
|
4109
|
+
str:'fou'
|
|
4110
|
+
})
|
|
4111
|
+
.reply(200);
|
|
4112
|
+
|
|
4113
|
+
mikealRequest('http://google.com/?num=1str=fou', function(err, res) {
|
|
4114
|
+
t.equal(err.message.trim(), 'Nock: No match for request GET http://google.com/?num=1str=fou');
|
|
4115
|
+
t.end();
|
|
4116
|
+
})
|
|
4117
|
+
});
|
|
4118
|
+
|
|
4101
4119
|
|
|
4102
4120
|
test("teardown", function(t) {
|
|
4103
4121
|
var leaks = Object.keys(global)
|