nock 11.3.6 → 11.7.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/README.md +9 -0
- package/index.js +2 -0
- package/lib/common.js +33 -9
- package/lib/delayed_body.js +1 -1
- package/lib/intercept.js +41 -40
- package/lib/intercepted_request_router.js +10 -14
- package/lib/interceptor.js +10 -4
- package/lib/match_body.js +2 -5
- package/lib/playback_interceptor.js +9 -10
- package/lib/recorder.js +27 -30
- package/lib/scope.js +3 -2
- package/lib/socket.js +16 -0
- package/package.json +14 -6
- package/types/index.d.ts +2 -0
- package/.all-contributorsrc +0 -123
- package/.eslintignore +0 -3
- package/.eslintrc.yml +0 -34
- package/.github/FUNDING.yml +0 -1
- package/.github/ISSUE_TEMPLATE/01_bug_report.md +0 -26
- package/.github/ISSUE_TEMPLATE/02_feature_request.md +0 -20
- package/.github/ISSUE_TEMPLATE/03_support.md +0 -10
- package/.github/ISSUE_TEMPLATE/04_thanks.md +0 -20
- package/.github/lock.yml +0 -19
- package/.github/stale.yml +0 -25
- package/.github/toc.yml +0 -1
- package/.istanbul.yml +0 -5
- package/.prettierignore +0 -5
- package/.prettierrc.yml +0 -4
- package/.travis.yml +0 -47
- package/CODE_OF_CONDUCT.md +0 -46
- package/CONTRIBUTING.md +0 -164
- package/assets/reply_file_1.txt +0 -1
- package/assets/reply_file_2.txt.gz +0 -0
- package/examples/.eslintrc.yml +0 -3
- package/examples/_log.js +0 -12
- package/examples/delay-connection.js +0 -15
- package/examples/delay-response.js +0 -15
- package/examples/net-connect-default-no-mock.js +0 -18
- package/examples/net-connect-default-other-mock.js +0 -23
- package/examples/net-connect-disabled-different-host.js +0 -26
- package/examples/net-connect-mock-same-host-different-path.js +0 -24
- package/examples/socket-delay-abort.js +0 -19
- package/examples/socket-delay-no-abort.js +0 -15
- package/rfcs/rfc-001.md +0 -43
- package/types/tests.ts +0 -810
- package/types/tsconfig.json +0 -11
- package/types/tslint.json +0 -6
package/examples/.eslintrc.yml
DELETED
package/examples/_log.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
module.exports = log
|
|
2
|
-
|
|
3
|
-
const start = Date.now()
|
|
4
|
-
|
|
5
|
-
function log(emitter, name) {
|
|
6
|
-
return function(event) {
|
|
7
|
-
emitter.on(event, function() {
|
|
8
|
-
const lag = `${Math.round((Date.now() - start) / 100) / 10} sec`
|
|
9
|
-
console.log('[%s] %s => %s', lag, name, event)
|
|
10
|
-
})
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const http = require('http')
|
|
2
|
-
const nock = require('../')
|
|
3
|
-
const log = require('./_log')
|
|
4
|
-
const events = ['socket', 'response', 'end', 'data']
|
|
5
|
-
|
|
6
|
-
nock('http://delayconnection.com')
|
|
7
|
-
.get('/')
|
|
8
|
-
.delayConnection(1000)
|
|
9
|
-
.reply(200, 'hey')
|
|
10
|
-
|
|
11
|
-
const req = http.get('http://delayconnection.com', function(res) {
|
|
12
|
-
events.forEach(log(res, 'res'))
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
events.forEach(log(req, 'req'))
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const http = require('http')
|
|
2
|
-
const nock = require('../')
|
|
3
|
-
const log = require('./_log')
|
|
4
|
-
const events = ['socket', 'response', 'end', 'data']
|
|
5
|
-
|
|
6
|
-
nock('http://delayconnection.com')
|
|
7
|
-
.get('/')
|
|
8
|
-
.delay(1000)
|
|
9
|
-
.reply(200, 'hey')
|
|
10
|
-
|
|
11
|
-
const req = http.get('http://delayconnection.com', function(res) {
|
|
12
|
-
events.forEach(log(res, 'res'))
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
events.forEach(log(req, 'req'))
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Default net connect.
|
|
3
|
-
No mock.
|
|
4
|
-
Result: Nock allows request to proceed.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const log = require('./_log')
|
|
8
|
-
|
|
9
|
-
const events = ['socket', 'response', 'end', 'data', 'error']
|
|
10
|
-
|
|
11
|
-
const http = require('http')
|
|
12
|
-
console.log('making request...')
|
|
13
|
-
const req = http.get('http://www.google.com/', function(res) {
|
|
14
|
-
console.log('request result: res.statusCode = %j', res.statusCode)
|
|
15
|
-
events.forEach(log(res, 'res'))
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
events.forEach(log(req, 'req'))
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Default net connect.
|
|
3
|
-
Mock the different hostname:port.
|
|
4
|
-
Result: Nock allows request to proceed.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const log = require('./_log')
|
|
8
|
-
|
|
9
|
-
const events = ['socket', 'response', 'end', 'data', 'error']
|
|
10
|
-
|
|
11
|
-
const nock = require('../')
|
|
12
|
-
|
|
13
|
-
nock('http://someotherservice.com')
|
|
14
|
-
.get('/')
|
|
15
|
-
.reply(200, 'whaaa')
|
|
16
|
-
|
|
17
|
-
const http = require('http')
|
|
18
|
-
const req = http.get('http://www.google.com/', function(res) {
|
|
19
|
-
console.log('request result: res.statusCode = %j', res.statusCode)
|
|
20
|
-
events.forEach(log(res, 'res'))
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
events.forEach(log(req, 'req'))
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Disabled net connect.
|
|
3
|
-
Mock the different hostname:port.
|
|
4
|
-
Result: Nock does not allow request to proceed.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const log = require('./_log')
|
|
8
|
-
|
|
9
|
-
const events = ['socket', 'response', 'end', 'data', 'error']
|
|
10
|
-
|
|
11
|
-
const nock = require('../')
|
|
12
|
-
|
|
13
|
-
nock.disableNetConnect()
|
|
14
|
-
|
|
15
|
-
nock('http://someotherservice.com')
|
|
16
|
-
.get('/')
|
|
17
|
-
.reply(200, 'whaaa')
|
|
18
|
-
|
|
19
|
-
const http = require('http')
|
|
20
|
-
const req = http.get('http://www.google.com/')
|
|
21
|
-
|
|
22
|
-
req.once('error', function(err) {
|
|
23
|
-
console.log(err.stack)
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
events.forEach(log(req, 'req'))
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Default net connect.
|
|
3
|
-
Mock the same hostname:port, different path.
|
|
4
|
-
Result: Nock does not allow request to proceed.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const log = require('./_log')
|
|
8
|
-
|
|
9
|
-
const events = ['socket', 'response', 'end', 'data', 'error']
|
|
10
|
-
|
|
11
|
-
const nock = require('../')
|
|
12
|
-
|
|
13
|
-
nock('http://example.com')
|
|
14
|
-
.get('/path')
|
|
15
|
-
.reply(200, 'whaaa')
|
|
16
|
-
|
|
17
|
-
const http = require('http')
|
|
18
|
-
const req = http.get('http://example.com/other-path')
|
|
19
|
-
|
|
20
|
-
req.once('error', function(err) {
|
|
21
|
-
console.log(err.stack)
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
events.forEach(log(req, 'req'))
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
const http = require('http')
|
|
2
|
-
const nock = require('../')
|
|
3
|
-
const log = require('./_log')
|
|
4
|
-
const events = ['socket', 'response', 'end', 'data', 'timeout', 'error']
|
|
5
|
-
|
|
6
|
-
nock('http://delayconnection.com')
|
|
7
|
-
.get('/')
|
|
8
|
-
.socketDelay(2000)
|
|
9
|
-
.reply(200, 'hey')
|
|
10
|
-
|
|
11
|
-
const req = http.get('http://delayconnection.com', function(res) {
|
|
12
|
-
events.forEach(log(res, 'res'))
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
req.setTimeout(1000, function() {
|
|
16
|
-
req.abort()
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
events.forEach(log(req, 'req'))
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const http = require('http')
|
|
2
|
-
const nock = require('../')
|
|
3
|
-
const log = require('./_log')
|
|
4
|
-
const events = ['socket', 'response', 'end', 'data', 'timeout', 'error']
|
|
5
|
-
|
|
6
|
-
nock('http://delayconnection.com')
|
|
7
|
-
.get('/')
|
|
8
|
-
.socketDelay(2000)
|
|
9
|
-
.reply(200, 'hey')
|
|
10
|
-
|
|
11
|
-
const req = http.get('http://delayconnection.com', function(res) {
|
|
12
|
-
events.forEach(log(res, 'res'))
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
events.forEach(log(req, 'req'))
|
package/rfcs/rfc-001.md
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
# Analysis of lifecycle methods
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-

|
|
5
|
-
|
|
6
|
-
Nock's lifecycle methods are confusingly named, and at times are inconvenient.
|
|
7
|
-
It can take a lot of studying to understand how to use them. It's also easy to
|
|
8
|
-
misunderstand what these methods are doing, and leave unwanted state in
|
|
9
|
-
`nock`. This RFC analyzes the most common use cases and the function calls
|
|
10
|
-
needed for each one.
|
|
11
|
-
|
|
12
|
-
Nock doesn't automatically have a way to assert that mocks have been
|
|
13
|
-
satisfied; it's the caller's responsibility to do this for each one.
|
|
14
|
-
|
|
15
|
-
See
|
|
16
|
-
https://github.com/paulmelnikow/icedfrisby-nock/blob/master/icedfrisby-nock.js
|
|
17
|
-
for an attempt at getting the lifecycle right.
|
|
18
|
-
|
|
19
|
-
Subsequent RFCs will propose changes to the lifecycle APIs which better
|
|
20
|
-
accommodate these use cases.
|
|
21
|
-
|
|
22
|
-
## Typical use cases
|
|
23
|
-
|
|
24
|
-
1. Assert that all mocks have been satisfied.
|
|
25
|
-
2. Completely reset `nock` after a test.
|
|
26
|
-
3. Allowing unmocked requests only to certain hosts.
|
|
27
|
-
4. Preventing unmocked requests entirely.
|
|
28
|
-
5. Simulating network connection failures.
|
|
29
|
-
6. Temporarily disabling http call interception while preserving registered mocks.
|
|
30
|
-
7. Turn `nock` all the way off and clean up its state (\*\* I've actually never
|
|
31
|
-
wanted to do this, but wanted to include it in the analysis)
|
|
32
|
-
|
|
33
|
-
## Analysis
|
|
34
|
-
|
|
35
|
-
| Use case | Code | Assessment |
|
|
36
|
-
| ----------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
37
|
-
| Assert that all mocks have been satisfied | `scopes.forEach(scope => scope.done())`. When using `nockBack`, `assert.deepEqual(scope.pendingMocks(), [])` | `done()` could have a more explicit name, though otherwise this is fairly clear. However it requires the caller to keep track of all the scopes, which is not ideal. |
|
|
38
|
-
| Reset `nock` after a test to its initial post-`require()` state | `nock.restore(); nock.cleanAll(); nock.enableNetConnect(); nock.activate()` | This is too much typing. |
|
|
39
|
-
| Forbid unmocked requests | `nock.disableNetConnect()` | This _looks_ okay, but it doesn't have the desired effect. Errors are received by the client code and often swallowed up by the application (#884). |
|
|
40
|
-
| Allow unmocked requests, but only to certain hosts | `nock.disableNetConnect(); nock.enableNetConnect('example.com')` | This is a common use case, and should be possible to do more succintly, with a single call. |
|
|
41
|
-
| Simulate network connection failure | N/A | This is what `disableNetConnect()` does today. However from the function name, it's not really clear this is the intention. |
|
|
42
|
-
| Temporarily disable http interception while preserving registered mocks | `nock.restore()` | This is a confusing name, as it only cleans _part_ of nock's state. |
|
|
43
|
-
| Turn `nock` all the way off and clean up its state | `nock.restore(); nock.cleanAll()` | `restore()` is a confusing name. This isn't the most common use case, so it is probably okay that it requires two function calls. |
|