nock 9.4.4 → 10.0.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/.github/ISSUE_TEMPLATE/01_bug_report.md +26 -0
- package/.github/ISSUE_TEMPLATE/02_feature_request.md +21 -0
- package/.github/ISSUE_TEMPLATE/03_support.md +24 -0
- package/.github/ISSUE_TEMPLATE/04_thanks.md +21 -0
- package/.github/lock.yml +19 -0
- package/.github/stale.yml +25 -0
- package/.travis.yml +1 -3
- package/CONTRIBUTING.md +30 -5
- package/README.md +125 -20
- package/lib/common.js +2 -2
- package/lib/intercept.js +1 -1
- package/lib/interceptor.js +7 -3
- package/lib/match_body.js +9 -0
- package/lib/recorder.js +4 -4
- package/lib/request_overrider.js +28 -25
- package/lib/socket.js +2 -1
- package/package.json +3 -3
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: 🐛 Bug report
|
|
3
|
+
about: If something isn't working 🤕
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
**What is the expected behavior?**
|
|
8
|
+
|
|
9
|
+
**What is the actual behavior?**
|
|
10
|
+
|
|
11
|
+
**Possible solution**
|
|
12
|
+
|
|
13
|
+
**How to reproduce the issue**
|
|
14
|
+
|
|
15
|
+
Runkit: *[Example link](https://runkit.com/gr2m/node-nock-nock-768)*
|
|
16
|
+
|
|
17
|
+
_Having problem producing a test case? Try and ask the community for help. If the test case cannot be reproduced, the Nock community might not be able to help you._
|
|
18
|
+
|
|
19
|
+
**Does the bug have a test case?**
|
|
20
|
+
|
|
21
|
+
**Versions**
|
|
22
|
+
|
|
23
|
+
| Software | Version(s) |
|
|
24
|
+
|----------|------------|
|
|
25
|
+
| Nock | |
|
|
26
|
+
| Node | |
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: 🚀 Feature request
|
|
3
|
+
about: Let us know if you have a feature request 💡
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
**Context**
|
|
8
|
+
|
|
9
|
+
What are you trying to do and how would you want to do it differently? Is it something you currently you cannot do? Is this related to an issue/problem?
|
|
10
|
+
|
|
11
|
+
**Alternatives**
|
|
12
|
+
|
|
13
|
+
Can you achieve the same result doing it in an alternative way? Is the alternative considerable?
|
|
14
|
+
|
|
15
|
+
**Has the feature been requested before?**
|
|
16
|
+
|
|
17
|
+
Please provide a link to the issue.
|
|
18
|
+
|
|
19
|
+
**If the feature request is accepted, would you be willing to submit a PR?**
|
|
20
|
+
|
|
21
|
+
Yes / No _(Help can be provided if you need assistance submitting a PR)_
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ❓ Support
|
|
3
|
+
about: If you need support, check out Nock on StackOverflow
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
<!-- 👆 Click "Preview" for a nicer view! -->
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
We primarily use GitHub as an issue tracker; for usage and support questions we primairly use StackOverflow. Please tag the questions with `nock` so the community can easily find it. [Go to StackOverflow](https://stackoverflow.com/questions/tagged/nock).
|
|
23
|
+
|
|
24
|
+
If you want to submit a feature request you can do that [here](https://github.com/nock/nock/issues/new?template=feature_request.md) or bug reports [here](https://github.com/nock/nock/issues/new?template=bug_report.md).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ❤️ Say thank you
|
|
3
|
+
about: Tell us how you use nock & support our efforts in maintaining Nock
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# ❤️ I'm using Nock
|
|
8
|
+
|
|
9
|
+
If you (or your company) are using Nock - please let us know. We'd love to hear from you!
|
|
10
|
+
|
|
11
|
+
Nock is a community driven project, which means it's not maintained by a company. If you would like to help Nock - any of the following is greatly appreciated.
|
|
12
|
+
|
|
13
|
+
- [ ] Give the repository a star ⭐️
|
|
14
|
+
- [ ] Help out with issues
|
|
15
|
+
- [ ] Review pull requests
|
|
16
|
+
- [ ] Blog about Nock
|
|
17
|
+
- [ ] Make tutorials
|
|
18
|
+
- [ ] Give talks
|
|
19
|
+
- [ ] Support us on [https://opencollective.com/nock](https://opencollective.com/nock)
|
|
20
|
+
|
|
21
|
+
Thank you! 💐
|
package/.github/lock.yml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Configuration for lock-threads - https://github.com/dessant/lock-threads
|
|
2
|
+
|
|
3
|
+
# Number of days of inactivity before a closed issue or pull request is locked
|
|
4
|
+
daysUntilLock: 14
|
|
5
|
+
|
|
6
|
+
# Issues and pull requests with these labels will not be locked. Set to `[]` to disable
|
|
7
|
+
exemptLabels: []
|
|
8
|
+
|
|
9
|
+
# Label to add before locking, such as `outdated`. Set to `false` to disable
|
|
10
|
+
lockLabel: false
|
|
11
|
+
|
|
12
|
+
# Comment to post before locking. Set to `false` to disable
|
|
13
|
+
lockComment: >
|
|
14
|
+
This thread has been automatically locked since there has not been
|
|
15
|
+
any recent activity after it was closed. Please open a new issue and
|
|
16
|
+
add a reference to this one if it’s related. Thank you!
|
|
17
|
+
|
|
18
|
+
# Assign `resolved` as the reason for locking. Set to `false` to disable
|
|
19
|
+
setLockReason: true
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Configuration for probot-stale - https://github.com/probot/stale
|
|
2
|
+
|
|
3
|
+
# Number of days of inactivity before an Issue or Pull Request becomes stale
|
|
4
|
+
daysUntilStale: 90
|
|
5
|
+
|
|
6
|
+
# Number of days of inactivity before an Issue or Pull Request with the stale label is closed.
|
|
7
|
+
# Set to false to disable. If disabled, issues still need to be closed manually, but will remain marked as stale.
|
|
8
|
+
daysUntilClose: 7
|
|
9
|
+
|
|
10
|
+
# Issues or Pull Requests with these labels will never be considered stale. Set to `[]` to disable
|
|
11
|
+
exemptLabels:
|
|
12
|
+
- pinned
|
|
13
|
+
|
|
14
|
+
# Label to use when marking as stale
|
|
15
|
+
staleLabel: stale
|
|
16
|
+
|
|
17
|
+
# Comment to post when marking as stale. Set to `false` to disable
|
|
18
|
+
markComment: >
|
|
19
|
+
This issue has been automatically marked as stale because it has not had
|
|
20
|
+
recent activity. It will be closed if no further activity occurs. We try to
|
|
21
|
+
do our best, but nock is maintained by volunteers and there is only so much
|
|
22
|
+
we can do at a time. Thank you for your contributions.
|
|
23
|
+
|
|
24
|
+
# Limit the number of actions per hour, from 1-30. Default is 30
|
|
25
|
+
limitPerRun: 1
|
package/.travis.yml
CHANGED
|
@@ -23,14 +23,12 @@ jobs:
|
|
|
23
23
|
env: linting
|
|
24
24
|
script: npm run lint
|
|
25
25
|
- stage: test
|
|
26
|
-
node_js:
|
|
26
|
+
node_js: 10
|
|
27
27
|
script: npm run unit
|
|
28
28
|
- node_js: 8
|
|
29
29
|
script: npm run unit
|
|
30
30
|
- node_js: 6
|
|
31
31
|
script: npm run unit
|
|
32
|
-
- node_js: 4
|
|
33
|
-
script: npm run unit
|
|
34
32
|
- stage: release
|
|
35
33
|
node_js: lts/*
|
|
36
34
|
env: semantic-release
|
package/CONTRIBUTING.md
CHANGED
|
@@ -12,10 +12,12 @@ Please note that this project is released with a [Contributor Code of Conduct](.
|
|
|
12
12
|
- [Generate README TOC](#generate-readme-toc)
|
|
13
13
|
- [Running tests](#running-tests)
|
|
14
14
|
* [Airplane mode](#airplane-mode)
|
|
15
|
+
- [Release Process](#release-process)
|
|
16
|
+
- [Becoming a maintainer](#becoming-a-maintainer)
|
|
15
17
|
|
|
16
18
|
<!-- tocstop -->
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
## Commit Message conventions
|
|
19
21
|
|
|
20
22
|
`nock` releases are automated using [semantic-release](https://github.com/semantic-release/semantic-release).
|
|
21
23
|
To automatically calculate the correct version number as well as changelogs,
|
|
@@ -36,7 +38,7 @@ Other helpful conventions are
|
|
|
36
38
|
|
|
37
39
|
The commit message(s) of a pull request can be fixed using the `squash & merge` button.
|
|
38
40
|
|
|
39
|
-
|
|
41
|
+
## Generate README TOC
|
|
40
42
|
|
|
41
43
|
Make sure to update the README's table of contents whenever you update the README using the following npm script.
|
|
42
44
|
|
|
@@ -44,17 +46,40 @@ Make sure to update the README's table of contents whenever you update the READM
|
|
|
44
46
|
$ npm run toc
|
|
45
47
|
```
|
|
46
48
|
|
|
47
|
-
|
|
49
|
+
## Running tests
|
|
48
50
|
|
|
49
51
|
```
|
|
50
52
|
$ npm test
|
|
51
53
|
```
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
### Airplane mode
|
|
54
56
|
|
|
55
57
|
Some of the tests depend on online connectivity. To skip them, set the `AIRPLANE` environment variable to some value.
|
|
56
58
|
|
|
57
59
|
```
|
|
58
60
|
$ export AIRPLANE=true
|
|
59
61
|
$ npm test
|
|
60
|
-
```
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Release Process
|
|
65
|
+
|
|
66
|
+
All of our releases are automated using [semantic-release](https://github.com/semantic-release/semantic-release). The commit messages pushed to the master branch trigger new releases. Semantic-release requires that commits follow certain convetions, [described above](#commit-message-conventions). semantic-release creates a GitHub release, adds release notes and publishes the new version to npm. This is why we do not store release notes in the [`CHANGELOG`](CHANGELOG.md) file - they're already on GitHub.
|
|
67
|
+
|
|
68
|
+
We use @nockbot as a separate account for releases, because npm tokens cannot be scoped to a single package. This improves our security model in case of a data breach involving npm tokens. @nockbot's credentials were set up by @gr2m; contact him if for any reason you need to change this in the future.
|
|
69
|
+
|
|
70
|
+
## Becoming a maintainer
|
|
71
|
+
|
|
72
|
+
So you want to do more than file a bug or submit a PR? Awesome!
|
|
73
|
+
|
|
74
|
+
Nock is actively interested in having more maintainers. That means that we would love to have you (yes, you) get more involved if you want to! We don't have strict tests that you need to pass to become a maintainer. Instead, all we want is to find people who are frequent contributors, understand what Nock does, and are eager to help make this tool better.
|
|
75
|
+
|
|
76
|
+
Here are some things you can do today to actively show the Nock team that you're interested in helping out in the long term:
|
|
77
|
+
|
|
78
|
+
* **Triage issues!** We have more issues than we have bandwidth to deal with. For some of these issues, there are no labels, no comments suggesting how the issue could be resolved, and no follow-up after months or years of silence. It would be great if the issues actively reflected the state of the project. Go through old issues and suggesting labels in comments, ask for more information, and generally help out with the resolution process. This would be a great help!
|
|
79
|
+
* **Review PRs.** We have a lot of open PRs! Some of these are probably ready to merge, and some of them probably need more work. Any extra eyes on PRs are encouraged. Comment on code patterns you think need work, suggest extra tests, and let us know if a PR 'LGTM' ("looks good to me"). The more reviewers we have, the faster we can merge issues, the better this project becomes.
|
|
80
|
+
* **Help out!** If someone files a bug and no one has responded yet, see if you can resolve it! Suggest PRs, or file them yourself. While most contributors are going to only be interested in their own bugs, great maintainers help out with other people's bugs. This is one of the best ways to become an expert at Nock (and Node.js, JavaScript, or pretty much any project) - helping others.
|
|
81
|
+
* **Write docs.** Are our docs missing something important? Are they not clear? Could they be better? Open a PR!
|
|
82
|
+
* **Suggest examples.** Right now, we have a few examples, but we could always have more. Submit small example files and tutorials. At some point, we'll have to work on a better way to display these - for now, it's great to show others how to solve difficult mocking problems easily with Nock.
|
|
83
|
+
* **Refactor.** This is one of the hardest things to do, but one of the most useful. Go through the code, and find examples where it could be written better - with better variable names, more useful abstractions, and more elegant patterns. Taking away ten lines of code that are unnecessary is more valuable than submitting a hundred new lines, sometimes. Open a PR or a comment and defend your position; ask for feedback.
|
|
84
|
+
|
|
85
|
+
Once you've been around for a bit, ask a current Maintainer - one of [the team members](https://github.com/orgs/nock/people) - whether you can be elevated to Maintainer status and given permissions to close issues and merge PRs. We're interested in how well you know what Nock is about, and how involved you are in the community - not where you're from, how good your English is, or whether or not you can pass a whiteboard test blindfolded. If you think that you've been helpful, let us know. We're friendly, promise. :)
|
package/README.md
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
[](https://travis-ci.org/nock/nock)
|
|
4
4
|
[](https://coveralls.io/github/nock/nock?branch=master)
|
|
5
5
|
[](https://greenkeeper.io/)
|
|
6
|
+
[](#backers)
|
|
7
|
+
[](#sponsors)
|
|
6
8
|
|
|
7
9
|
> HTTP server mocking and expectations library for Node.js
|
|
8
10
|
|
|
@@ -59,7 +61,10 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
|
|
|
59
61
|
- [Restoring](#restoring)
|
|
60
62
|
- [Activating](#activating)
|
|
61
63
|
- [Turning Nock Off (experimental!)](#turning-nock-off-experimental)
|
|
62
|
-
- [Enable/Disable real HTTP
|
|
64
|
+
- [Enable/Disable real HTTP requests](#enabledisable-real-http-requests)
|
|
65
|
+
* [Disabling requests](#disabling-requests)
|
|
66
|
+
* [Enabling requests](#enabling-requests)
|
|
67
|
+
* [Resetting NetConnect](#resetting-netconnect)
|
|
63
68
|
- [Recording](#recording)
|
|
64
69
|
* [`dont_print` option](#dont_print-option)
|
|
65
70
|
* [`output_objects` option](#output_objects-option)
|
|
@@ -78,6 +83,12 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
|
|
|
78
83
|
- [Debugging](#debugging)
|
|
79
84
|
- [PROTIP](#protip)
|
|
80
85
|
- [Contributing](#contributing)
|
|
86
|
+
- [Commit Message conventions](#commit-message-conventions)
|
|
87
|
+
- [Generate README TOC](#generate-readme-toc)
|
|
88
|
+
- [Running tests](#running-tests)
|
|
89
|
+
* [Airplane mode](#airplane-mode)
|
|
90
|
+
- [Backers](#backers)
|
|
91
|
+
- [Sponsors](#sponsors)
|
|
81
92
|
- [License](#license)
|
|
82
93
|
|
|
83
94
|
<!-- tocstop -->
|
|
@@ -183,7 +194,7 @@ var scope = nock('http://www.example.com')
|
|
|
183
194
|
|
|
184
195
|
### Specifying request body
|
|
185
196
|
|
|
186
|
-
You can specify the request body to be matched as the second argument to the `get`, `post`, `put` or `delete` specifications. There are
|
|
197
|
+
You can specify the request body to be matched as the second argument to the `get`, `post`, `put` or `delete` specifications. There are five types of second argument allowed:
|
|
187
198
|
|
|
188
199
|
**String**: nock will exact match the stringified request body with the provided string
|
|
189
200
|
|
|
@@ -193,6 +204,14 @@ nock('http://www.example.com')
|
|
|
193
204
|
.reply(200, { id: '123ABC' });
|
|
194
205
|
```
|
|
195
206
|
|
|
207
|
+
**Buffer**: nock will exact match the stringified request body with the provided buffer
|
|
208
|
+
|
|
209
|
+
```js
|
|
210
|
+
nock('http://www.example.com')
|
|
211
|
+
.post('/login', Buffer.from([0xff, 0x11]))
|
|
212
|
+
.reply(200, { id: '123ABC' });
|
|
213
|
+
```
|
|
214
|
+
|
|
196
215
|
**RegExp**: nock will test the stringified request body against the provided RegExp
|
|
197
216
|
|
|
198
217
|
```js
|
|
@@ -621,7 +640,7 @@ nock('http://my.server.com')
|
|
|
621
640
|
.reply(200, '<html></html>')
|
|
622
641
|
```
|
|
623
642
|
|
|
624
|
-
NOTE: the [`'response'`](http://nodejs.org/api/http.html#http_event_response) event will occur immediately, but the [IncomingMessage](http://nodejs.org/api/http.html#http_http_incomingmessage) will not emit
|
|
643
|
+
NOTE: the [`'response'`](http://nodejs.org/api/http.html#http_event_response) event will occur immediately, but the [IncomingMessage](http://nodejs.org/api/http.html#http_http_incomingmessage) will not emit its `'end'` event until after the delay.
|
|
625
644
|
|
|
626
645
|
### Delay the response
|
|
627
646
|
|
|
@@ -832,6 +851,19 @@ example.pendingMocks() // []
|
|
|
832
851
|
|
|
833
852
|
example.get("/pathB").optionally().reply(200);
|
|
834
853
|
example.pendingMocks() // []
|
|
854
|
+
|
|
855
|
+
// You can also pass a boolean argument to `optionally()`. This
|
|
856
|
+
// is useful if you want to conditionally make a mocked request
|
|
857
|
+
// optional.
|
|
858
|
+
var getMock = function(optional) {
|
|
859
|
+
return example.get("/pathC").optionally(optional).reply(200);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
getMock(true);
|
|
863
|
+
example.pendingMocks() // []
|
|
864
|
+
getMock(false);
|
|
865
|
+
example.pendingMocks() // ["GET http://example.com:80/pathC"]
|
|
866
|
+
|
|
835
867
|
```
|
|
836
868
|
|
|
837
869
|
### Allow __unmocked__ requests on a mocked hostname
|
|
@@ -1000,17 +1032,19 @@ This way you can have your tests hit the real servers just by switching on this
|
|
|
1000
1032
|
$ NOCK_OFF=true node my_test.js
|
|
1001
1033
|
```
|
|
1002
1034
|
|
|
1003
|
-
## Enable/Disable real HTTP
|
|
1035
|
+
## Enable/Disable real HTTP requests
|
|
1004
1036
|
|
|
1005
1037
|
By default, any requests made to a host that is not mocked will be executed normally. If you want to block these requests, nock allows you to do so.
|
|
1006
1038
|
|
|
1039
|
+
### Disabling requests
|
|
1040
|
+
|
|
1007
1041
|
For disabling real http requests.
|
|
1008
1042
|
|
|
1009
1043
|
```js
|
|
1010
1044
|
nock.disableNetConnect();
|
|
1011
1045
|
```
|
|
1012
1046
|
|
|
1013
|
-
So, if you try to request any host not 'nocked', it will
|
|
1047
|
+
So, if you try to request any host not 'nocked', it will throw a `NetConnectNotAllowedError`.
|
|
1014
1048
|
|
|
1015
1049
|
```js
|
|
1016
1050
|
nock.disableNetConnect();
|
|
@@ -1020,40 +1054,45 @@ req.on('error', function(err){
|
|
|
1020
1054
|
});
|
|
1021
1055
|
// The returned `http.ClientRequest` will emit an error event (or throw if you're not listening for it)
|
|
1022
1056
|
// This code will log a NetConnectNotAllowedError with message:
|
|
1023
|
-
// Nock:
|
|
1057
|
+
// Nock: Disallowed net connect for "google.com:80"
|
|
1024
1058
|
```
|
|
1025
1059
|
|
|
1026
|
-
|
|
1060
|
+
### Enabling requests
|
|
1061
|
+
|
|
1062
|
+
For enabling any real HTTP requests (the default behavior):
|
|
1027
1063
|
|
|
1028
1064
|
```js
|
|
1029
1065
|
nock.enableNetConnect();
|
|
1030
1066
|
```
|
|
1031
1067
|
|
|
1032
|
-
You could allow real HTTP
|
|
1068
|
+
You could allow real HTTP requests for certain host names by providing a string or a regular expression for the hostname:
|
|
1033
1069
|
|
|
1034
1070
|
```js
|
|
1035
|
-
//
|
|
1071
|
+
// Using a string
|
|
1036
1072
|
nock.enableNetConnect('amazon.com');
|
|
1037
1073
|
|
|
1038
|
-
//
|
|
1039
|
-
nock.enableNetConnect(/(amazon|github)
|
|
1074
|
+
// Or a RegExp
|
|
1075
|
+
nock.enableNetConnect(/(amazon|github)\.com/);
|
|
1040
1076
|
|
|
1041
1077
|
http.get('http://www.amazon.com/');
|
|
1042
|
-
http.get('http://github.com/');
|
|
1078
|
+
http.get('http://github.com/');
|
|
1043
1079
|
|
|
1044
|
-
// This request will be done!
|
|
1045
1080
|
http.get('http://google.com/');
|
|
1046
|
-
//
|
|
1047
|
-
// Nock:
|
|
1081
|
+
// This will throw NetConnectNotAllowedError with message:
|
|
1082
|
+
// Nock: Disallowed net connect for "google.com:80"
|
|
1048
1083
|
```
|
|
1049
1084
|
|
|
1050
|
-
A common use case when testing local endpoints would be to disable all but
|
|
1085
|
+
A common use case when testing local endpoints would be to disable all but localhost, then add in additional nocks for external requests:
|
|
1051
1086
|
|
|
1052
1087
|
```js
|
|
1053
1088
|
nock.disableNetConnect();
|
|
1054
|
-
|
|
1089
|
+
// Allow localhost connections so we can test local routes and mock servers.
|
|
1090
|
+
nock.enableNetConnect('127.0.0.1');
|
|
1055
1091
|
```
|
|
1056
|
-
|
|
1092
|
+
|
|
1093
|
+
### Resetting NetConnect
|
|
1094
|
+
|
|
1095
|
+
When you're done with the test, you probably want to set everything back to normal:
|
|
1057
1096
|
|
|
1058
1097
|
```js
|
|
1059
1098
|
nock.cleanAll();
|
|
@@ -1163,7 +1202,7 @@ var nocks = nock.define(nockDefs);
|
|
|
1163
1202
|
|
|
1164
1203
|
### `enable_reqheaders_recording` option
|
|
1165
1204
|
|
|
1166
|
-
Recording request headers by default is deemed more trouble than
|
|
1205
|
+
Recording request headers by default is deemed more trouble than it's worth as some of them depend on the timestamp or other values that may change after the tests have been recorder thus leading to complex postprocessing of recorded tests. Thus by default the request headers are not recorded.
|
|
1167
1206
|
|
|
1168
1207
|
The genuine use cases for recording request headers (e.g. checking authorization) can be handled manually or by using `enable_reqheaders_recording` in `recorder.rec()` options.
|
|
1169
1208
|
|
|
@@ -1192,7 +1231,7 @@ nock.recorder.rec({
|
|
|
1192
1231
|
|
|
1193
1232
|
### `use_separator` option
|
|
1194
1233
|
|
|
1195
|
-
By default, nock will wrap
|
|
1234
|
+
By default, nock will wrap its output with the separator string `<<<<<<-- cut here -->>>>>>` before and after anything it prints, whether to the console or a custom log function given with the `logging` option.
|
|
1196
1235
|
|
|
1197
1236
|
To disable this, set `use_separator` to false.
|
|
1198
1237
|
|
|
@@ -1380,6 +1419,72 @@ Thanks for wanting to contribute! Take a look at our [Contributing Guide](CONTRI
|
|
|
1380
1419
|
Please note that this project is released with a [Contributor Code of Conduct](./CODE_OF_CONDUCT.md).
|
|
1381
1420
|
By participating in this project you agree to abide by its terms.
|
|
1382
1421
|
|
|
1422
|
+
## Commit Message conventions
|
|
1423
|
+
|
|
1424
|
+
`nock` releases are automated using [semantic-release](https://github.com/semantic-release/semantic-release).
|
|
1425
|
+
To automatically calculate the correct version number as well as changelogs,
|
|
1426
|
+
three commit message conventions need to be followed
|
|
1427
|
+
|
|
1428
|
+
- Commit bug fixes with `fix: ...` or `fix(scope): ...` prefix in commit subject
|
|
1429
|
+
- Commit new features with `feat: ...` or `feat(scope): ...` prefix in commit subject
|
|
1430
|
+
- Commit breaking changes by adding `BREAKING CHANGE: ` in the commit body
|
|
1431
|
+
(not the subject line)
|
|
1432
|
+
|
|
1433
|
+
Other helpful conventions are
|
|
1434
|
+
|
|
1435
|
+
- Commit test files with `test: ...` or `test(scope): ...` prefix
|
|
1436
|
+
- Commit changes to `package.json`, `.gitignore` and other meta files with
|
|
1437
|
+
`chore(filename-without-ext): ...`
|
|
1438
|
+
- Commit changes to README files or comments with `docs: ...`
|
|
1439
|
+
- Code style changes with `style: standard`
|
|
1440
|
+
|
|
1441
|
+
The commit message(s) of a pull request can be fixed using the `squash & merge` button.
|
|
1442
|
+
|
|
1443
|
+
## Generate README TOC
|
|
1444
|
+
|
|
1445
|
+
Make sure to update the README's table of contents whenever you update the README using the following npm script.
|
|
1446
|
+
|
|
1447
|
+
```
|
|
1448
|
+
$ npm run toc
|
|
1449
|
+
```
|
|
1450
|
+
|
|
1451
|
+
## Running tests
|
|
1452
|
+
|
|
1453
|
+
```
|
|
1454
|
+
$ npm test
|
|
1455
|
+
```
|
|
1456
|
+
|
|
1457
|
+
### Airplane mode
|
|
1458
|
+
|
|
1459
|
+
Some of the tests depend on online connectivity. To skip them, set the `AIRPLANE` environment variable to some value.
|
|
1460
|
+
|
|
1461
|
+
```
|
|
1462
|
+
$ export AIRPLANE=true
|
|
1463
|
+
$ npm test
|
|
1464
|
+
```
|
|
1465
|
+
|
|
1466
|
+
## Backers
|
|
1467
|
+
|
|
1468
|
+
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/nock#backer)]
|
|
1469
|
+
|
|
1470
|
+
<a href="https://opencollective.com/nock#backers" target="_blank"><img src="https://opencollective.com/nock/backers.svg?width=890"></a>
|
|
1471
|
+
|
|
1472
|
+
|
|
1473
|
+
## Sponsors
|
|
1474
|
+
|
|
1475
|
+
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/nock#sponsor)]
|
|
1476
|
+
|
|
1477
|
+
<a href="https://opencollective.com/nock/sponsor/0/website" target="_blank"><img src="https://opencollective.com/nock/sponsor/0/avatar.svg"></a>
|
|
1478
|
+
<a href="https://opencollective.com/nock/sponsor/1/website" target="_blank"><img src="https://opencollective.com/nock/sponsor/1/avatar.svg"></a>
|
|
1479
|
+
<a href="https://opencollective.com/nock/sponsor/2/website" target="_blank"><img src="https://opencollective.com/nock/sponsor/2/avatar.svg"></a>
|
|
1480
|
+
<a href="https://opencollective.com/nock/sponsor/3/website" target="_blank"><img src="https://opencollective.com/nock/sponsor/3/avatar.svg"></a>
|
|
1481
|
+
<a href="https://opencollective.com/nock/sponsor/4/website" target="_blank"><img src="https://opencollective.com/nock/sponsor/4/avatar.svg"></a>
|
|
1482
|
+
<a href="https://opencollective.com/nock/sponsor/5/website" target="_blank"><img src="https://opencollective.com/nock/sponsor/5/avatar.svg"></a>
|
|
1483
|
+
<a href="https://opencollective.com/nock/sponsor/6/website" target="_blank"><img src="https://opencollective.com/nock/sponsor/6/avatar.svg"></a>
|
|
1484
|
+
<a href="https://opencollective.com/nock/sponsor/7/website" target="_blank"><img src="https://opencollective.com/nock/sponsor/7/avatar.svg"></a>
|
|
1485
|
+
<a href="https://opencollective.com/nock/sponsor/8/website" target="_blank"><img src="https://opencollective.com/nock/sponsor/8/avatar.svg"></a>
|
|
1486
|
+
<a href="https://opencollective.com/nock/sponsor/9/website" target="_blank"><img src="https://opencollective.com/nock/sponsor/9/avatar.svg"></a>
|
|
1487
|
+
|
|
1383
1488
|
## License
|
|
1384
1489
|
|
|
1385
1490
|
[MIT](LICENSE)
|
package/lib/common.js
CHANGED
|
@@ -50,7 +50,7 @@ var isBinaryBuffer = function(buffer) {
|
|
|
50
50
|
|
|
51
51
|
// Test if the buffer can be reconstructed verbatim from its utf8 encoding.
|
|
52
52
|
var utfEncodedBuffer = buffer.toString('utf8');
|
|
53
|
-
var reconstructedBuffer =
|
|
53
|
+
var reconstructedBuffer = Buffer.from(utfEncodedBuffer, 'utf8');
|
|
54
54
|
var compareBuffers = function(lhs, rhs) {
|
|
55
55
|
if(lhs.length !== rhs.length) {
|
|
56
56
|
return false;
|
|
@@ -80,7 +80,7 @@ var isBinaryBuffer = function(buffer) {
|
|
|
80
80
|
var mergeChunks = function(chunks) {
|
|
81
81
|
|
|
82
82
|
if(_.isEmpty(chunks)) {
|
|
83
|
-
return
|
|
83
|
+
return Buffer.alloc(0);
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
// We assume that all chunks are Buffer objects if the first is buffer object.
|
package/lib/intercept.js
CHANGED
|
@@ -33,7 +33,7 @@ function NetConnectNotAllowedError(host, path) {
|
|
|
33
33
|
|
|
34
34
|
this.name = 'NetConnectNotAllowedError';
|
|
35
35
|
this.code = 'ENETUNREACH'
|
|
36
|
-
this.message = 'Nock:
|
|
36
|
+
this.message = 'Nock: Disallowed net connect for "' + host + path + '"';
|
|
37
37
|
|
|
38
38
|
Error.captureStackTrace(this, this.constructor);
|
|
39
39
|
}
|
package/lib/interceptor.js
CHANGED
|
@@ -43,8 +43,12 @@ function Interceptor(scope, uri, method, requestBody, interceptorOptions) {
|
|
|
43
43
|
this.optional = false;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
Interceptor.prototype.optionally = function optionally() {
|
|
47
|
-
|
|
46
|
+
Interceptor.prototype.optionally = function optionally(value) {
|
|
47
|
+
// The default behaviour of optionally() with no arguments is to make the mock optional.
|
|
48
|
+
value = (typeof value === 'undefined') ? true : value;
|
|
49
|
+
|
|
50
|
+
this.optional = value;
|
|
51
|
+
|
|
48
52
|
return this;
|
|
49
53
|
}
|
|
50
54
|
|
|
@@ -401,7 +405,7 @@ Interceptor.prototype.basicAuth = function basicAuth(options) {
|
|
|
401
405
|
var username = options['user'];
|
|
402
406
|
var password = options['pass'] || '';
|
|
403
407
|
var name = 'authorization';
|
|
404
|
-
var value = 'Basic ' +
|
|
408
|
+
var value = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
|
|
405
409
|
this.interceptorMatchHeaders.push({ name: name, value: value });
|
|
406
410
|
return this;
|
|
407
411
|
};
|
package/lib/match_body.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
var deepEqual = require('deep-equal');
|
|
4
4
|
var qs = require('qs');
|
|
5
5
|
var _ = require('lodash')
|
|
6
|
+
var common = require('./common');
|
|
6
7
|
|
|
7
8
|
module.exports =
|
|
8
9
|
function matchBody(spec, body) {
|
|
@@ -15,6 +16,14 @@ function matchBody(spec, body) {
|
|
|
15
16
|
body = body.toString();
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
if (Buffer.isBuffer(spec)) {
|
|
20
|
+
if (common.isBinaryBuffer(spec)) {
|
|
21
|
+
spec = spec.toString('hex');
|
|
22
|
+
} else {
|
|
23
|
+
spec = spec.toString('utf8');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
18
27
|
var contentType = (
|
|
19
28
|
options.headers &&
|
|
20
29
|
(options.headers['Content-Type'] || options.headers['content-type']) ||
|
package/lib/recorder.js
CHANGED
|
@@ -56,7 +56,7 @@ var getBodyFromChunks = function(chunks, headers) {
|
|
|
56
56
|
body: _.map(chunks, function(chunk) {
|
|
57
57
|
if(!Buffer.isBuffer(chunk)) {
|
|
58
58
|
if (typeof chunk === 'string') {
|
|
59
|
-
chunk =
|
|
59
|
+
chunk = Buffer.from(chunk);
|
|
60
60
|
} else {
|
|
61
61
|
throw new Error('content-encoded responses must all be binary buffers');
|
|
62
62
|
}
|
|
@@ -330,7 +330,7 @@ function record(rec_options) {
|
|
|
330
330
|
res.push = function(data) {
|
|
331
331
|
if (data) {
|
|
332
332
|
if (encoding) {
|
|
333
|
-
data =
|
|
333
|
+
data = Buffer.from(data, encoding);
|
|
334
334
|
}
|
|
335
335
|
dataChunks.push(data);
|
|
336
336
|
}
|
|
@@ -358,7 +358,7 @@ function record(rec_options) {
|
|
|
358
358
|
if (data) {
|
|
359
359
|
debug(thisRecordingId, 'new', proto, 'body chunk');
|
|
360
360
|
if (! Buffer.isBuffer(data)) {
|
|
361
|
-
data =
|
|
361
|
+
data = Buffer.from(data, encoding);
|
|
362
362
|
}
|
|
363
363
|
bodyChunks.push(data);
|
|
364
364
|
}
|
|
@@ -373,7 +373,7 @@ function record(rec_options) {
|
|
|
373
373
|
if (data) {
|
|
374
374
|
debug(thisRecordingId, 'new', proto, 'body chunk');
|
|
375
375
|
if (! Buffer.isBuffer(data)) {
|
|
376
|
-
data =
|
|
376
|
+
data = Buffer.from(data, encoding);
|
|
377
377
|
}
|
|
378
378
|
bodyChunks.push(data);
|
|
379
379
|
}
|
package/lib/request_overrider.js
CHANGED
|
@@ -111,7 +111,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
111
111
|
|
|
112
112
|
/// options.auth
|
|
113
113
|
if (options.auth && (! options.headers || ! options.headers.authorization)) {
|
|
114
|
-
setHeader(req, 'Authorization', 'Basic ' + (
|
|
114
|
+
setHeader(req, 'Authorization', 'Basic ' + (Buffer.from(options.auth)).toString('base64'));
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
if (! req.connection) {
|
|
@@ -125,14 +125,13 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
125
125
|
};
|
|
126
126
|
|
|
127
127
|
req.socket = response.socket = Socket({ proto: options.proto });
|
|
128
|
-
req.socket.connecting = true;
|
|
129
128
|
|
|
130
129
|
req.write = function(buffer, encoding, callback) {
|
|
131
130
|
debug('write', arguments);
|
|
132
131
|
if (!req.aborted) {
|
|
133
132
|
if (buffer) {
|
|
134
133
|
if (!Buffer.isBuffer(buffer)) {
|
|
135
|
-
buffer =
|
|
134
|
+
buffer = Buffer.from(buffer, encoding);
|
|
136
135
|
}
|
|
137
136
|
requestBodyBuffers.push(buffer);
|
|
138
137
|
}
|
|
@@ -319,13 +318,13 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
319
318
|
emitError(new Error('Gzip encoding is currently not supported in this version of Node.'));
|
|
320
319
|
return;
|
|
321
320
|
}
|
|
322
|
-
requestBody = String(zlib.gunzipSync(
|
|
321
|
+
requestBody = String(zlib.gunzipSync(Buffer.from(requestBody, 'hex')), 'hex')
|
|
323
322
|
} else if (requestBody && common.contentEncoding(req.headers, 'deflate')) {
|
|
324
323
|
if (typeof zlib.deflateSync !== 'function') {
|
|
325
324
|
emitError(new Error('Deflate encoding is currently not supported in this version of Node.'));
|
|
326
325
|
return;
|
|
327
326
|
}
|
|
328
|
-
requestBody = String(zlib.inflateSync(
|
|
327
|
+
requestBody = String(zlib.inflateSync(Buffer.from(requestBody, 'hex')), 'hex')
|
|
329
328
|
}
|
|
330
329
|
|
|
331
330
|
requestBody = JSON.parse(requestBody);
|
|
@@ -357,7 +356,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
357
356
|
}
|
|
358
357
|
|
|
359
358
|
responseBuffers = _.map(buffers, function(buffer) {
|
|
360
|
-
return
|
|
359
|
+
return Buffer.from(buffer, 'hex');
|
|
361
360
|
});
|
|
362
361
|
|
|
363
362
|
} else {
|
|
@@ -369,7 +368,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
369
368
|
if(isBinaryRequestBodyBuffer && typeof(responseBody) === 'string') {
|
|
370
369
|
// Try to create the buffer from the interceptor's body response as hex.
|
|
371
370
|
try {
|
|
372
|
-
responseBody =
|
|
371
|
+
responseBody = Buffer.from(responseBody, 'hex');
|
|
373
372
|
} catch(err) {
|
|
374
373
|
debug('exception during Buffer construction from hex data:', responseBody, '-', err);
|
|
375
374
|
}
|
|
@@ -377,7 +376,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
377
376
|
// Creating buffers does not necessarily throw errors, check for difference in size
|
|
378
377
|
if (!responseBody || (interceptor.body.length > 0 && responseBody.length === 0)) {
|
|
379
378
|
// We fallback on constructing buffer from utf8 representation of the body.
|
|
380
|
-
responseBody =
|
|
379
|
+
responseBody = Buffer.from(interceptor.body, 'utf8');
|
|
381
380
|
}
|
|
382
381
|
}
|
|
383
382
|
}
|
|
@@ -403,23 +402,27 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
403
402
|
if (responseBody) {
|
|
404
403
|
debug('transform the response body');
|
|
405
404
|
|
|
406
|
-
if (Array.isArray(responseBody)
|
|
407
|
-
responseBody.length >= 2 &&
|
|
408
|
-
responseBody.length <= 3 &&
|
|
409
|
-
typeof responseBody[0] == 'number')
|
|
410
|
-
{
|
|
405
|
+
if (Array.isArray(responseBody)) {
|
|
411
406
|
debug('response body is array: %j', responseBody);
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
response.
|
|
422
|
-
|
|
407
|
+
|
|
408
|
+
if (!isNaN(Number(responseBody[0])))
|
|
409
|
+
{
|
|
410
|
+
response.statusCode = Number(responseBody[0]);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (responseBody.length >= 2 && responseBody.length <= 3)
|
|
414
|
+
{
|
|
415
|
+
debug('new headers: %j', responseBody[2]);
|
|
416
|
+
if (!response.headers) response.headers = {};
|
|
417
|
+
_.assign(response.headers, responseBody[2] || {});
|
|
418
|
+
debug('response.headers after: %j', response.headers);
|
|
419
|
+
responseBody = responseBody[1];
|
|
420
|
+
|
|
421
|
+
response.rawHeaders = response.rawHeaders || [];
|
|
422
|
+
Object.keys(response.headers).forEach(function(key) {
|
|
423
|
+
response.rawHeaders.push(key, response.headers[key]);
|
|
424
|
+
});
|
|
425
|
+
}
|
|
423
426
|
}
|
|
424
427
|
|
|
425
428
|
if (interceptor.delayInMs) {
|
|
@@ -443,7 +446,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
443
446
|
});
|
|
444
447
|
} else if (responseBody && !Buffer.isBuffer(responseBody)) {
|
|
445
448
|
if (typeof responseBody === 'string') {
|
|
446
|
-
responseBody =
|
|
449
|
+
responseBody = Buffer.from(responseBody);
|
|
447
450
|
} else {
|
|
448
451
|
responseBody = JSON.stringify(responseBody);
|
|
449
452
|
response.headers['content-type'] = 'application/json';
|
package/lib/socket.js
CHANGED
|
@@ -22,6 +22,7 @@ function Socket(options) {
|
|
|
22
22
|
this.writable = true;
|
|
23
23
|
this.readable = true;
|
|
24
24
|
this.destroyed = false;
|
|
25
|
+
this.connecting = false;
|
|
25
26
|
|
|
26
27
|
this.setNoDelay = noop;
|
|
27
28
|
this.setKeepAlive = noop;
|
|
@@ -57,7 +58,7 @@ Socket.prototype.applyDelay = function applyDelay(delayMs) {
|
|
|
57
58
|
};
|
|
58
59
|
|
|
59
60
|
Socket.prototype.getPeerCertificate = function getPeerCertificate() {
|
|
60
|
-
return
|
|
61
|
+
return Buffer.from((Math.random() * 10000 + Date.now()).toString()).toString('base64');
|
|
61
62
|
};
|
|
62
63
|
|
|
63
64
|
Socket.prototype.destroy = function destroy() {
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"testing",
|
|
8
8
|
"isolation"
|
|
9
9
|
],
|
|
10
|
-
"version": "
|
|
10
|
+
"version": "10.0.0",
|
|
11
11
|
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
|
|
12
12
|
"contributors": [
|
|
13
13
|
{
|
|
@@ -185,7 +185,7 @@
|
|
|
185
185
|
"hyperquest": "^2.1.3",
|
|
186
186
|
"isomorphic-fetch": "^2.2.0",
|
|
187
187
|
"markdown-toc": "^1.2.0",
|
|
188
|
-
"needle": "^2.2.
|
|
188
|
+
"needle": "^2.2.2",
|
|
189
189
|
"nyc": "^12.0.1",
|
|
190
190
|
"request": "^2.83.0",
|
|
191
191
|
"request-promise": "^4.2.2",
|
|
@@ -201,7 +201,7 @@
|
|
|
201
201
|
"test": "npm run -s unit",
|
|
202
202
|
"coverage": "nyc tap --harmony ./tests/test_*.js",
|
|
203
203
|
"coveralls": "cat ./coverage/lcov.info | coveralls",
|
|
204
|
-
"lint": "eslint
|
|
204
|
+
"lint": "eslint \"**/*.js\"",
|
|
205
205
|
"toc": "markdown-toc -i README.md && markdown-toc -i CONTRIBUTING.md ",
|
|
206
206
|
"semantic-release": "semantic-release"
|
|
207
207
|
},
|