nock 9.3.2 → 9.4.2
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/CODE_OF_CONDUCT.md +1 -1
- package/CONTRIBUTING.md +60 -0
- package/LICENSE +1 -1
- package/README.md +80 -125
- package/lib/interceptor.js +1 -1
- package/lib/request_overrider.js +4 -4
- package/lib/scope.js +1 -2
- package/package.json +7 -7
package/CODE_OF_CONDUCT.md
CHANGED
|
@@ -34,7 +34,7 @@ This Code of Conduct applies both within project spaces and in public spaces whe
|
|
|
34
34
|
|
|
35
35
|
## Enforcement
|
|
36
36
|
|
|
37
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at nock+coc@martynus.net. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at nock+coc@martynus.net or richard@maintainer.io. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
|
38
38
|
|
|
39
39
|
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
|
40
40
|
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Contribute
|
|
2
|
+
|
|
3
|
+
👋 Thanks for thinking about contributing to nock! We, the maintainers, are glad you're here and will be excited to help you get started if you have any questions. For now, here are some basic instructions for how we manage this project.
|
|
4
|
+
|
|
5
|
+
Please note that this project is released with a [Contributor Code of Conduct](./CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.
|
|
6
|
+
|
|
7
|
+
**Table of Contents**
|
|
8
|
+
|
|
9
|
+
<!-- toc -->
|
|
10
|
+
|
|
11
|
+
- [Commit Message conventions](#commit-message-conventions)
|
|
12
|
+
- [Generate README TOC](#generate-readme-toc)
|
|
13
|
+
- [Running tests](#running-tests)
|
|
14
|
+
* [Airplane mode](#airplane-mode)
|
|
15
|
+
|
|
16
|
+
<!-- tocstop -->
|
|
17
|
+
|
|
18
|
+
### Commit Message conventions
|
|
19
|
+
|
|
20
|
+
`nock` releases are automated using [semantic-release](https://github.com/semantic-release/semantic-release).
|
|
21
|
+
To automatically calculate the correct version number as well as changelogs,
|
|
22
|
+
three commit message conventions need to be followed
|
|
23
|
+
|
|
24
|
+
- Commit bug fixes with `fix: ...` or `fix(scope): ...` prefix in commit subject
|
|
25
|
+
- Commit new features with `feat: ...` or `feat(scope): ...` prefix in commit subject
|
|
26
|
+
- Commit breaking changes by adding `BREAKING CHANGE: ` in the commit body
|
|
27
|
+
(not the subject line)
|
|
28
|
+
|
|
29
|
+
Other helpful conventions are
|
|
30
|
+
|
|
31
|
+
- Commit test files with `test: ...` or `test(scope): ...` prefix
|
|
32
|
+
- Commit changes to `package.json`, `.gitignore` and other meta files with
|
|
33
|
+
`chore(filename-without-ext): ...`
|
|
34
|
+
- Commit changes to README files or comments with `docs: ...`
|
|
35
|
+
- Code style changes with `style: standard`
|
|
36
|
+
|
|
37
|
+
The commit message(s) of a pull request can be fixed using the `squash & merge` button.
|
|
38
|
+
|
|
39
|
+
### Generate README TOC
|
|
40
|
+
|
|
41
|
+
Make sure to update the README's table of contents whenever you update the README using the following npm script.
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
$ npm run toc
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Running tests
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
$ npm test
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
#### Airplane mode
|
|
54
|
+
|
|
55
|
+
Some of the tests depend on online connectivity. To skip them, set the `AIRPLANE` environment variable to some value.
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
$ export AIRPLANE=true
|
|
59
|
+
$ npm test
|
|
60
|
+
```
|
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2011-2018 Pedro Teixeira and other contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# Nock
|
|
2
2
|
|
|
3
|
-
[](https://travis-ci.org/nock/nock)
|
|
4
|
+
[](https://coveralls.io/github/nock/nock?branch=master)
|
|
5
|
+
[](https://greenkeeper.io/)
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
Nock is an HTTP mocking and expectations library for Node.js
|
|
8
|
+
Nock is an HTTP server mocking and expectations library for Node.js.
|
|
9
9
|
|
|
10
10
|
Nock can be used to test modules that perform HTTP requests in isolation.
|
|
11
11
|
|
|
@@ -18,7 +18,7 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
|
|
|
18
18
|
- [How does it work?](#how-does-it-work)
|
|
19
19
|
- [Install](#install)
|
|
20
20
|
* [Node version support](#node-version-support)
|
|
21
|
-
- [
|
|
21
|
+
- [Usage](#usage)
|
|
22
22
|
* [READ THIS! - About interceptors](#read-this---about-interceptors)
|
|
23
23
|
* [Specifying hostname](#specifying-hostname)
|
|
24
24
|
* [Specifying path](#specifying-path)
|
|
@@ -48,7 +48,7 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
|
|
|
48
48
|
* [Request Body filtering](#request-body-filtering)
|
|
49
49
|
* [Request Headers Matching](#request-headers-matching)
|
|
50
50
|
* [Optional Requests](#optional-requests)
|
|
51
|
-
* [Allow __unmocked__ requests on a mocked hostname](#allow-
|
|
51
|
+
* [Allow __unmocked__ requests on a mocked hostname](#allow-__unmocked__-requests-on-a-mocked-hostname)
|
|
52
52
|
- [Expectations](#expectations)
|
|
53
53
|
* [.isDone()](#isdone)
|
|
54
54
|
* [.cleanAll()](#cleanall)
|
|
@@ -73,30 +73,27 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
|
|
|
73
73
|
- [Nock Back](#nock-back)
|
|
74
74
|
* [Setup](#setup)
|
|
75
75
|
+ [Options](#options)
|
|
76
|
-
* [Usage](#usage)
|
|
76
|
+
* [Usage](#usage-1)
|
|
77
77
|
+ [Options](#options-1)
|
|
78
78
|
+ [Modes](#modes)
|
|
79
79
|
- [Debugging](#debugging)
|
|
80
80
|
- [PROTIP](#protip)
|
|
81
81
|
- [Contributing](#contributing)
|
|
82
|
-
* [Generate README TOC](#generate-readme-toc)
|
|
83
|
-
* [Running tests](#running-tests)
|
|
84
|
-
+ [Airplane mode](#airplane-mode)
|
|
85
82
|
- [License](#license)
|
|
86
83
|
|
|
87
84
|
<!-- tocstop -->
|
|
88
85
|
|
|
89
|
-
|
|
86
|
+
## How does it work?
|
|
90
87
|
|
|
91
88
|
Nock works by overriding Node's `http.request` function. Also, it overrides `http.ClientRequest` too to cover for modules that use it directly.
|
|
92
89
|
|
|
93
|
-
|
|
90
|
+
## Install
|
|
94
91
|
|
|
95
92
|
```sh
|
|
96
|
-
$ npm install nock
|
|
93
|
+
$ npm install --save nock
|
|
97
94
|
```
|
|
98
95
|
|
|
99
|
-
|
|
96
|
+
### Node version support
|
|
100
97
|
|
|
101
98
|
| node | nock |
|
|
102
99
|
|---|---|
|
|
@@ -107,7 +104,7 @@ $ npm install nock
|
|
|
107
104
|
| 5 | up to 8.x |
|
|
108
105
|
| 6 | 9.x |
|
|
109
106
|
|
|
110
|
-
|
|
107
|
+
## Usage
|
|
111
108
|
|
|
112
109
|
On your test, you can setup your mocking object like this:
|
|
113
110
|
|
|
@@ -130,14 +127,14 @@ It will intercept an HTTP GET request to '/users/1' and reply with a status 200,
|
|
|
130
127
|
|
|
131
128
|
Then the test can call the module, and the module will do the HTTP requests.
|
|
132
129
|
|
|
133
|
-
|
|
130
|
+
### READ THIS! - About interceptors
|
|
134
131
|
|
|
135
132
|
When you setup an interceptor for a URL and that interceptor is used, it is removed from the interceptor list.
|
|
136
133
|
This means that you can intercept 2 or more calls to the same URL and return different things on each of them.
|
|
137
134
|
It also means that you must setup one interceptor for each request you are going to have, otherwise nock will throw an error because that URL was not present in the interceptor list.
|
|
138
135
|
If you don’t want interceptors to be removed as they are used, you can use the [.persist()](#persist) method.
|
|
139
136
|
|
|
140
|
-
|
|
137
|
+
### Specifying hostname
|
|
141
138
|
|
|
142
139
|
The request hostname can be a string or a RegExp.
|
|
143
140
|
|
|
@@ -153,9 +150,9 @@ var scope = nock(/example\.com/)
|
|
|
153
150
|
.reply(200, 'domain regex matched');
|
|
154
151
|
```
|
|
155
152
|
|
|
156
|
-
>
|
|
153
|
+
> Note: You can choose to include or not the protocol in the hostname matching.
|
|
157
154
|
|
|
158
|
-
|
|
155
|
+
### Specifying path
|
|
159
156
|
|
|
160
157
|
The request path can be a string, a RegExp or a filter function and you can use any [HTTP verb](#http-verbs).
|
|
161
158
|
|
|
@@ -185,7 +182,7 @@ var scope = nock('http://www.example.com')
|
|
|
185
182
|
.reply(200, 'path using function matched');
|
|
186
183
|
```
|
|
187
184
|
|
|
188
|
-
|
|
185
|
+
### Specifying request body
|
|
189
186
|
|
|
190
187
|
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
188
|
|
|
@@ -232,7 +229,7 @@ nock('http://www.example.com')
|
|
|
232
229
|
```
|
|
233
230
|
|
|
234
231
|
|
|
235
|
-
|
|
232
|
+
### Specifying request query string
|
|
236
233
|
|
|
237
234
|
Nock understands query strings. Instead of placing the entire URL, you can specify the query part as an object:
|
|
238
235
|
|
|
@@ -281,7 +278,7 @@ nock('http://example.com')
|
|
|
281
278
|
.reply(200, {results: [{id: 'pgte'}]});
|
|
282
279
|
```
|
|
283
280
|
|
|
284
|
-
|
|
281
|
+
### Specifying replies
|
|
285
282
|
|
|
286
283
|
You can specify the return status code for a path on the first argument of reply like this:
|
|
287
284
|
|
|
@@ -380,7 +377,7 @@ var scope = nock('http://www.google.com')
|
|
|
380
377
|
});
|
|
381
378
|
```
|
|
382
379
|
|
|
383
|
-
|
|
380
|
+
##### Access original request and headers
|
|
384
381
|
|
|
385
382
|
If you're using the reply callback style, you can access the original client request using `this.req` like this:
|
|
386
383
|
|
|
@@ -394,7 +391,7 @@ var scope = nock('http://www.google.com')
|
|
|
394
391
|
});
|
|
395
392
|
```
|
|
396
393
|
|
|
397
|
-
|
|
394
|
+
#### Replying with errors
|
|
398
395
|
|
|
399
396
|
You can reply with an error like this:
|
|
400
397
|
|
|
@@ -412,16 +409,16 @@ nock('http://www.google.com')
|
|
|
412
409
|
.replyWithError({'message': 'something awful happened', 'code': 'AWFUL_ERROR'});
|
|
413
410
|
```
|
|
414
411
|
|
|
415
|
-
>
|
|
412
|
+
> Note: This will emit an `error` event on the `request` object, not the reply.
|
|
416
413
|
|
|
417
414
|
|
|
418
|
-
|
|
415
|
+
### Specifying headers
|
|
419
416
|
|
|
420
|
-
|
|
417
|
+
#### Header field names are case-insensitive
|
|
421
418
|
|
|
422
419
|
Per [HTTP/1.1 4.2 Message Headers](http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2) specification, all message headers are case insensitive and thus internally Nock uses lower-case for all field names even if some other combination of cases was specified either in mocking specification or in mocked requests themselves.
|
|
423
420
|
|
|
424
|
-
|
|
421
|
+
#### Specifying Request Headers
|
|
425
422
|
|
|
426
423
|
You can specify the request headers like this:
|
|
427
424
|
|
|
@@ -482,7 +479,7 @@ var scope = nock('http://www.example.com')
|
|
|
482
479
|
.reply(200);
|
|
483
480
|
```
|
|
484
481
|
|
|
485
|
-
|
|
482
|
+
#### Specifying Reply Headers
|
|
486
483
|
|
|
487
484
|
You can specify the reply headers like this:
|
|
488
485
|
|
|
@@ -508,7 +505,7 @@ var scope = nock('http://www.headdy.com')
|
|
|
508
505
|
});
|
|
509
506
|
```
|
|
510
507
|
|
|
511
|
-
|
|
508
|
+
#### Default Reply Headers
|
|
512
509
|
|
|
513
510
|
You can also specify default reply headers for all responses like this:
|
|
514
511
|
|
|
@@ -535,7 +532,7 @@ var scope = nock('http://www.headdy.com')
|
|
|
535
532
|
.reply(200, 'The default headers should come too');
|
|
536
533
|
```
|
|
537
534
|
|
|
538
|
-
|
|
535
|
+
#### Including Content-Length Header Automatically
|
|
539
536
|
|
|
540
537
|
When using `scope.reply()` to set a response body manually, you can have the
|
|
541
538
|
`Content-Length` header calculated automatically.
|
|
@@ -550,7 +547,7 @@ var scope = nock('http://www.headdy.com')
|
|
|
550
547
|
**NOTE:** this does not work with streams or other advanced means of specifying
|
|
551
548
|
the reply body.
|
|
552
549
|
|
|
553
|
-
|
|
550
|
+
#### Including Date Header Automatically
|
|
554
551
|
|
|
555
552
|
You can automatically append a `Date` header to your mock reply:
|
|
556
553
|
|
|
@@ -561,7 +558,7 @@ var scope = nock('http://www.headdy.com')
|
|
|
561
558
|
.reply(200, { hello: 'world' });
|
|
562
559
|
```
|
|
563
560
|
|
|
564
|
-
|
|
561
|
+
### HTTP Verbs
|
|
565
562
|
|
|
566
563
|
Nock supports any HTTP verb, and it has convenience methods for the GET, POST, PUT, HEAD, DELETE, PATCH and MERGE HTTP verbs.
|
|
567
564
|
|
|
@@ -573,7 +570,7 @@ var scope = nock('http://my.domain.com')
|
|
|
573
570
|
.reply(304);
|
|
574
571
|
```
|
|
575
572
|
|
|
576
|
-
|
|
573
|
+
### Support for HTTP and HTTPS
|
|
577
574
|
|
|
578
575
|
By default nock assumes HTTP. If you need to use HTTPS you can specify the `https://` prefix like this:
|
|
579
576
|
|
|
@@ -582,7 +579,7 @@ var scope = nock('https://secure.my.server.com')
|
|
|
582
579
|
// ...
|
|
583
580
|
```
|
|
584
581
|
|
|
585
|
-
|
|
582
|
+
### Non-standard ports
|
|
586
583
|
|
|
587
584
|
You are able to specify a non-standard port like this:
|
|
588
585
|
|
|
@@ -591,7 +588,7 @@ var scope = nock('http://my.server.com:8081')
|
|
|
591
588
|
...
|
|
592
589
|
```
|
|
593
590
|
|
|
594
|
-
|
|
591
|
+
### Repeat response n times
|
|
595
592
|
|
|
596
593
|
You are able to specify the number of times to repeat the same response.
|
|
597
594
|
|
|
@@ -613,7 +610,7 @@ nock('http://zombo.com').get('/').twice().reply(200, 'Ok');
|
|
|
613
610
|
nock('http://zombo.com').get('/').thrice().reply(200, 'Ok');
|
|
614
611
|
```
|
|
615
612
|
|
|
616
|
-
|
|
613
|
+
### Delay the response body
|
|
617
614
|
You are able to specify the number of milliseconds that the response body should be delayed. Response header will be replied immediately.
|
|
618
615
|
`delayBody(1000)` is equivalent to `delay({body: 1000})`.
|
|
619
616
|
|
|
@@ -627,7 +624,7 @@ nock('http://my.server.com')
|
|
|
627
624
|
|
|
628
625
|
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 it's `'end'` event until after the delay.
|
|
629
626
|
|
|
630
|
-
|
|
627
|
+
### Delay the response
|
|
631
628
|
|
|
632
629
|
You are able to specify the number of milliseconds that your reply should be delayed.
|
|
633
630
|
|
|
@@ -659,11 +656,11 @@ nock('http://my.server.com')
|
|
|
659
656
|
.reply(200, '<html></html>')
|
|
660
657
|
```
|
|
661
658
|
|
|
662
|
-
|
|
659
|
+
### Delay the connection
|
|
663
660
|
|
|
664
661
|
`delayConnection(1000)` is equivalent to `delay({head: 1000})`.
|
|
665
662
|
|
|
666
|
-
|
|
663
|
+
### Socket timeout
|
|
667
664
|
|
|
668
665
|
You are able to specify the number of milliseconds that your connection should be idle, to simulate a socket timeout.
|
|
669
666
|
|
|
@@ -688,7 +685,7 @@ req.end();
|
|
|
688
685
|
|
|
689
686
|
NOTE: the timeout will be fired immediately, and will not leave the simulated connection idle for the specified period of time.
|
|
690
687
|
|
|
691
|
-
|
|
688
|
+
### Chaining
|
|
692
689
|
|
|
693
690
|
You can chain behaviour like this:
|
|
694
691
|
|
|
@@ -714,7 +711,7 @@ var scope = nock('http://myapp.iriscouch.com')
|
|
|
714
711
|
});
|
|
715
712
|
```
|
|
716
713
|
|
|
717
|
-
|
|
714
|
+
### Scope filtering
|
|
718
715
|
|
|
719
716
|
You can filter the scope (protocol, domain or port) of nock through a function. The filtering function is accepted at the `filteringScope` field of the `options` argument.
|
|
720
717
|
|
|
@@ -730,7 +727,7 @@ var scope = nock('https://api.dropbox.com', {
|
|
|
730
727
|
.reply(200);
|
|
731
728
|
```
|
|
732
729
|
|
|
733
|
-
|
|
730
|
+
### Path filtering
|
|
734
731
|
|
|
735
732
|
You can also filter the URLs based on a function.
|
|
736
733
|
|
|
@@ -758,7 +755,7 @@ var scope = nock('http://api.myservice.com')
|
|
|
758
755
|
|
|
759
756
|
Note that `scope.filteringPath` is not cumulative: it should only be used once per scope.
|
|
760
757
|
|
|
761
|
-
|
|
758
|
+
### Request Body filtering
|
|
762
759
|
|
|
763
760
|
You can also filter the request body based on a function.
|
|
764
761
|
|
|
@@ -784,7 +781,7 @@ var scope = nock('http://api.myservice.com')
|
|
|
784
781
|
.reply(201, 'OK');
|
|
785
782
|
```
|
|
786
783
|
|
|
787
|
-
|
|
784
|
+
### Request Headers Matching
|
|
788
785
|
|
|
789
786
|
If you need to match requests only if certain request headers match, you can.
|
|
790
787
|
|
|
@@ -821,7 +818,7 @@ var scope = nock('http://api.myservice.com')
|
|
|
821
818
|
})
|
|
822
819
|
```
|
|
823
820
|
|
|
824
|
-
|
|
821
|
+
### Optional Requests
|
|
825
822
|
|
|
826
823
|
By default every mocked request is expected to be made exactly once, and until it is it'll appear in `scope.pendingMocks()`, and `scope.isDone()` will return false (see [expectations](#expectations)). In many cases this is fine, but in some (especially cross-test setup code) it's useful to be able to mock a request that may or may not happen. You can do this with `optionally()`. Optional requests are consumed just like normal ones once matched, but they do not appear in `pendingMocks()`, and `isDone()` will return true for scopes with only optional requests pending.
|
|
827
824
|
|
|
@@ -838,7 +835,7 @@ example.get("/pathB").optionally().reply(200);
|
|
|
838
835
|
example.pendingMocks() // []
|
|
839
836
|
```
|
|
840
837
|
|
|
841
|
-
|
|
838
|
+
### Allow __unmocked__ requests on a mocked hostname
|
|
842
839
|
|
|
843
840
|
If you need some request on the same host name to be mocked and some others to **really** go through the HTTP stack, you can use the `allowUnmocked` option like this:
|
|
844
841
|
|
|
@@ -852,9 +849,9 @@ var scope = nock('http://my.existing.service.com', options)
|
|
|
852
849
|
// GET /other/url => actually makes request to the server
|
|
853
850
|
```
|
|
854
851
|
|
|
855
|
-
>
|
|
852
|
+
> Note: When applying `{allowUnmocked: true}`, if the request is made to the real server, no interceptor is removed.
|
|
856
853
|
|
|
857
|
-
|
|
854
|
+
## Expectations
|
|
858
855
|
|
|
859
856
|
Every time an HTTP request is performed for a scope that is mocked, Nock expects to find a handler for it. If it doesn't, it will throw an error.
|
|
860
857
|
|
|
@@ -874,7 +871,7 @@ setTimeout(function() {
|
|
|
874
871
|
}, 5000);
|
|
875
872
|
```
|
|
876
873
|
|
|
877
|
-
|
|
874
|
+
### .isDone()
|
|
878
875
|
|
|
879
876
|
You can call `isDone()` on a single expectation to determine if the expectation was met:
|
|
880
877
|
|
|
@@ -892,14 +889,14 @@ It is also available in the global scope, which will determine if all expectatio
|
|
|
892
889
|
nock.isDone();
|
|
893
890
|
```
|
|
894
891
|
|
|
895
|
-
|
|
892
|
+
### .cleanAll()
|
|
896
893
|
|
|
897
894
|
You can cleanup all the prepared mocks (could be useful to cleanup some state after a failed test) like this:
|
|
898
895
|
|
|
899
896
|
```js
|
|
900
897
|
nock.cleanAll();
|
|
901
898
|
```
|
|
902
|
-
|
|
899
|
+
### .persist()
|
|
903
900
|
|
|
904
901
|
You can make all the interceptors for a scope persist by calling `.persist()` on it:
|
|
905
902
|
|
|
@@ -920,7 +917,7 @@ var scope = nock('http://example.com').persist().get('/').reply(200, 'ok');
|
|
|
920
917
|
scope.persist(false);
|
|
921
918
|
```
|
|
922
919
|
|
|
923
|
-
|
|
920
|
+
### .pendingMocks()
|
|
924
921
|
|
|
925
922
|
If a scope is not done, you can inspect the scope to infer which ones are still pending using the `scope.pendingMocks()` function:
|
|
926
923
|
|
|
@@ -936,7 +933,7 @@ It is also available in the global scope:
|
|
|
936
933
|
console.error('pending mocks: %j', nock.pendingMocks());
|
|
937
934
|
```
|
|
938
935
|
|
|
939
|
-
|
|
936
|
+
### .activeMocks()
|
|
940
937
|
|
|
941
938
|
You can see every mock that is currently active (i.e. might potentially reply to requests) in a scope using `scope.activeMocks()`. A mock is active if it is pending, optional but not yet completed, or persisted. Mocks that have intercepted their requests and are no longer doing anything are the only mocks which won't appear here.
|
|
942
939
|
|
|
@@ -952,7 +949,7 @@ It is also available in the global scope:
|
|
|
952
949
|
console.error('active mocks: %j', nock.activeMocks());
|
|
953
950
|
```
|
|
954
951
|
|
|
955
|
-
|
|
952
|
+
### .isActive()
|
|
956
953
|
|
|
957
954
|
Your tests may sometimes want to deactivate the nock interceptor.
|
|
958
955
|
Once deactivated, nock needs to be re-activated to work.
|
|
@@ -963,7 +960,7 @@ Sample:
|
|
|
963
960
|
if (!nock.isActive()) nock.activate()
|
|
964
961
|
```
|
|
965
962
|
|
|
966
|
-
|
|
963
|
+
## Logging
|
|
967
964
|
|
|
968
965
|
Nock can log matches if you pass in a log function like this:
|
|
969
966
|
|
|
@@ -973,7 +970,7 @@ var google = nock('http://google.com')
|
|
|
973
970
|
...
|
|
974
971
|
```
|
|
975
972
|
|
|
976
|
-
|
|
973
|
+
## Restoring
|
|
977
974
|
|
|
978
975
|
You can restore the HTTP interceptor to the normal unmocked behaviour by calling:
|
|
979
976
|
|
|
@@ -984,7 +981,7 @@ nock.restore();
|
|
|
984
981
|
|
|
985
982
|
**note 2**: restore will also remove the http interceptor itself. You need to run [nock.activate()](#activating) to re-activate the http interceptor. Without re-activation, nock will not intercept any calls.
|
|
986
983
|
|
|
987
|
-
|
|
984
|
+
## Activating
|
|
988
985
|
|
|
989
986
|
Only for cases where nock has been deactivated using [nock.restore()](#restoring), you can reactivate the HTTP interceptor to start intercepting HTTP calls using:
|
|
990
987
|
|
|
@@ -994,7 +991,7 @@ nock.activate();
|
|
|
994
991
|
|
|
995
992
|
**note**: To check if nock HTTP interceptor is active or deactive, use [nock.isActive()](#isactive).
|
|
996
993
|
|
|
997
|
-
|
|
994
|
+
## Turning Nock Off (experimental!)
|
|
998
995
|
|
|
999
996
|
You can bypass Nock completely by setting `NOCK_OFF` environment variable to `"true"`.
|
|
1000
997
|
|
|
@@ -1004,7 +1001,7 @@ This way you can have your tests hit the real servers just by switching on this
|
|
|
1004
1001
|
$ NOCK_OFF=true node my_test.js
|
|
1005
1002
|
```
|
|
1006
1003
|
|
|
1007
|
-
|
|
1004
|
+
## Enable/Disable real HTTP request
|
|
1008
1005
|
|
|
1009
1006
|
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.
|
|
1010
1007
|
|
|
@@ -1064,7 +1061,7 @@ nock.cleanAll();
|
|
|
1064
1061
|
nock.enableNetConnect();
|
|
1065
1062
|
```
|
|
1066
1063
|
|
|
1067
|
-
|
|
1064
|
+
## Recording
|
|
1068
1065
|
|
|
1069
1066
|
This is a cool feature:
|
|
1070
1067
|
|
|
@@ -1084,7 +1081,7 @@ In order to stop recording you should call `nock.restore()` and recording will s
|
|
|
1084
1081
|
|
|
1085
1082
|
**ATTENTION!:** when recording is enabled, nock does no validation, nor will any mocks be enabled. Please be sure to turn off recording before attempting to use any mocks in your tests.
|
|
1086
1083
|
|
|
1087
|
-
|
|
1084
|
+
### `dont_print` option
|
|
1088
1085
|
|
|
1089
1086
|
If you just want to capture the generated code into a var as an array you can use:
|
|
1090
1087
|
|
|
@@ -1102,7 +1099,7 @@ Copy and paste that code into your tests, customize at will, and you're done! Yo
|
|
|
1102
1099
|
|
|
1103
1100
|
(Remember that you should do this one test at a time).
|
|
1104
1101
|
|
|
1105
|
-
|
|
1102
|
+
### `output_objects` option
|
|
1106
1103
|
|
|
1107
1104
|
In case you want to generate the code yourself or use the test data in some other way, you can pass the `output_objects` option to `rec`:
|
|
1108
1105
|
|
|
@@ -1165,7 +1162,7 @@ nockDefs.forEach(function(def) {
|
|
|
1165
1162
|
var nocks = nock.define(nockDefs);
|
|
1166
1163
|
```
|
|
1167
1164
|
|
|
1168
|
-
|
|
1165
|
+
### `enable_reqheaders_recording` option
|
|
1169
1166
|
|
|
1170
1167
|
Recording request headers by default is deemed more trouble than its 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.
|
|
1171
1168
|
|
|
@@ -1181,7 +1178,7 @@ nock.recorder.rec({
|
|
|
1181
1178
|
|
|
1182
1179
|
Note that even when request headers recording is enabled Nock will never record `user-agent` headers. `user-agent` values change with the version of Node and underlying operating system and are thus useless for matching as all that they can indicate is that the user agent isn't the one that was used to record the tests.
|
|
1183
1180
|
|
|
1184
|
-
|
|
1181
|
+
### `logging` option
|
|
1185
1182
|
|
|
1186
1183
|
Nock will print using `console.log` by default (assuming that `dont_print` is `false`). If a different function is passed into `logging`, nock will send the log string (or object, when using `output_objects`) to that function. Here's a basic example.
|
|
1187
1184
|
|
|
@@ -1194,7 +1191,7 @@ nock.recorder.rec({
|
|
|
1194
1191
|
});
|
|
1195
1192
|
```
|
|
1196
1193
|
|
|
1197
|
-
|
|
1194
|
+
### `use_separator` option
|
|
1198
1195
|
|
|
1199
1196
|
By default, nock will wrap it's 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.
|
|
1200
1197
|
|
|
@@ -1206,7 +1203,7 @@ nock.recorder.rec({
|
|
|
1206
1203
|
});
|
|
1207
1204
|
```
|
|
1208
1205
|
|
|
1209
|
-
|
|
1206
|
+
### .removeInterceptor()
|
|
1210
1207
|
This allows removing a specific interceptor. This can be either an interceptor instance or options for a url. It's useful when there's a list of common interceptors shared between tests, where an individual test requires one of the shared interceptors to behave differently.
|
|
1211
1208
|
|
|
1212
1209
|
Examples:
|
|
@@ -1232,14 +1229,14 @@ var interceptor = nock('http://example.org')
|
|
|
1232
1229
|
nock.removeInterceptor(interceptor);
|
|
1233
1230
|
```
|
|
1234
1231
|
|
|
1235
|
-
|
|
1232
|
+
## Events
|
|
1236
1233
|
|
|
1237
1234
|
A scope emits the following events:
|
|
1238
1235
|
|
|
1239
|
-
* `emit('request', function(req, interceptor))`;
|
|
1236
|
+
* `emit('request', function(req, interceptor, body))`;
|
|
1240
1237
|
* `emit('replied', function(req, interceptor))`;
|
|
1241
1238
|
|
|
1242
|
-
|
|
1239
|
+
### Global no match event
|
|
1243
1240
|
|
|
1244
1241
|
You can also listen for no match events like this:
|
|
1245
1242
|
|
|
@@ -1249,11 +1246,11 @@ nock.emitter.on('no match', function(req) {
|
|
|
1249
1246
|
});
|
|
1250
1247
|
```
|
|
1251
1248
|
|
|
1252
|
-
|
|
1249
|
+
## Nock Back
|
|
1253
1250
|
|
|
1254
1251
|
fixture recording support and playback
|
|
1255
1252
|
|
|
1256
|
-
|
|
1253
|
+
### Setup
|
|
1257
1254
|
|
|
1258
1255
|
**You must specify a fixture directory before using, for example:
|
|
1259
1256
|
|
|
@@ -1266,13 +1263,13 @@ nockBack.fixtures = '/path/to/fixtures/';
|
|
|
1266
1263
|
nockBack.setMode('record');
|
|
1267
1264
|
```
|
|
1268
1265
|
|
|
1269
|
-
|
|
1266
|
+
#### Options
|
|
1270
1267
|
|
|
1271
1268
|
- `nockBack.fixtures` : path to fixture directory
|
|
1272
1269
|
- `nockBack.setMode()` : the mode to use
|
|
1273
1270
|
|
|
1274
1271
|
|
|
1275
|
-
|
|
1272
|
+
### Usage
|
|
1276
1273
|
|
|
1277
1274
|
By default if the fixture doesn't exist, a `nockBack` will create a new fixture and save the recorded output
|
|
1278
1275
|
for you. The next time you run the test, if the fixture exists, it will be loaded in.
|
|
@@ -1335,7 +1332,7 @@ return nockBack('promisedFixture.json')
|
|
|
1335
1332
|
});
|
|
1336
1333
|
```
|
|
1337
1334
|
|
|
1338
|
-
|
|
1335
|
+
#### Options
|
|
1339
1336
|
|
|
1340
1337
|
As an optional second parameter you can pass the following options
|
|
1341
1338
|
|
|
@@ -1345,7 +1342,7 @@ As an optional second parameter you can pass the following options
|
|
|
1345
1342
|
- `recorder`: custom options to pass to the recorder
|
|
1346
1343
|
|
|
1347
1344
|
|
|
1348
|
-
|
|
1345
|
+
#### Modes
|
|
1349
1346
|
|
|
1350
1347
|
to set the mode call `nockBack.setMode(mode)` or run the tests with the `NOCK_BACK_MODE` environment variable set before loading nock. If the mode needs to be changed programatically, the following is valid: `nockBack.setMode(nockBack.currentMode)`
|
|
1351
1348
|
|
|
@@ -1357,14 +1354,14 @@ to set the mode call `nockBack.setMode(mode)` or run the tests with the `NOCK_BA
|
|
|
1357
1354
|
|
|
1358
1355
|
- lockdown: use recorded nocks, disables all http calls even when not nocked, doesn't record
|
|
1359
1356
|
|
|
1360
|
-
|
|
1357
|
+
## Debugging
|
|
1361
1358
|
Nock uses debug, so just run with environmental variable DEBUG set to nock.*
|
|
1362
1359
|
|
|
1363
1360
|
```js
|
|
1364
1361
|
$ DEBUG=nock.* node my_test.js
|
|
1365
1362
|
```
|
|
1366
1363
|
|
|
1367
|
-
|
|
1364
|
+
## PROTIP
|
|
1368
1365
|
|
|
1369
1366
|
If you don't want to match the request body you can use this trick (by @theycallmeswift):
|
|
1370
1367
|
|
|
@@ -1377,57 +1374,15 @@ var scope = nock('http://api.myservice.com')
|
|
|
1377
1374
|
.reply(200, 'OK');
|
|
1378
1375
|
```
|
|
1379
1376
|
|
|
1380
|
-
|
|
1377
|
+
## Contributing
|
|
1378
|
+
|
|
1379
|
+
Thanks for wanting to contribute! Take a look at our [Contributing Guide](CONTRIBUTING.md) for notes on our commit message conventions and how to run tests.
|
|
1381
1380
|
|
|
1382
1381
|
Please note that this project is released with a [Contributor Code of Conduct](./CODE_OF_CONDUCT.md).
|
|
1383
1382
|
By participating in this project you agree to abide by its terms.
|
|
1384
1383
|
|
|
1385
|
-
##
|
|
1386
|
-
|
|
1387
|
-
`nock` releases are automated using [semantic-release](https://github.com/semantic-release/semantic-release).
|
|
1388
|
-
To automatically calculate the correct version number as well as changelogs,
|
|
1389
|
-
three commit message conventions need to be followed
|
|
1390
|
-
|
|
1391
|
-
- Commit bug fixes with `fix: ...` or `fix(scope): ...` prefix in commit subject
|
|
1392
|
-
- Commit new features with `feat: ...` or `feat(scope): ...` prefix in commit subject
|
|
1393
|
-
- Commit breaking changes by adding `BREAKING CHANGE: ` in the commit body
|
|
1394
|
-
(not the subject line)
|
|
1395
|
-
|
|
1396
|
-
Other helpful conventions are
|
|
1397
|
-
|
|
1398
|
-
- Commit test files with `test: ...` or `test(scope): ...` prefix
|
|
1399
|
-
- Commit changes to `package.json`, `.gitignore` and other meta files with
|
|
1400
|
-
`chore(filename-without-ext): ...`
|
|
1401
|
-
- Commit changes to README files or comments with `docs: ...`
|
|
1402
|
-
- Code style changes with `style: standard`
|
|
1403
|
-
|
|
1404
|
-
The commit message(s) of a pull request can be fixed using the `squash & merge` button.
|
|
1405
|
-
|
|
1406
|
-
## Generate README TOC
|
|
1407
|
-
|
|
1408
|
-
Make sure to update the README's table of contents whenever you update the README using the following npm script.
|
|
1409
|
-
|
|
1410
|
-
```
|
|
1411
|
-
$ npm run toc
|
|
1412
|
-
```
|
|
1413
|
-
|
|
1414
|
-
## Running tests
|
|
1415
|
-
|
|
1416
|
-
```
|
|
1417
|
-
$ npm test
|
|
1418
|
-
```
|
|
1419
|
-
|
|
1420
|
-
### Airplane mode
|
|
1421
|
-
|
|
1422
|
-
Some of the tests depend on online connectivity. To skip them, set the `AIRPLANE` environment variable to some value.
|
|
1423
|
-
|
|
1424
|
-
```
|
|
1425
|
-
$ export AIRPLANE=true
|
|
1426
|
-
$ npm test
|
|
1427
|
-
```
|
|
1428
|
-
|
|
1429
|
-
# License
|
|
1384
|
+
## License
|
|
1430
1385
|
|
|
1431
1386
|
[MIT](LICENSE)
|
|
1432
1387
|
|
|
1433
|
-
Copyright (c) 2011-2017 [Pedro Teixeira](http://about.me/pedroteixeira) and other [contributors](https://github.com/
|
|
1388
|
+
Copyright (c) 2011-2017 [Pedro Teixeira](http://about.me/pedroteixeira) and other [contributors](https://github.com/nock/nock/graphs/contributors).
|
package/lib/interceptor.js
CHANGED
|
@@ -306,7 +306,7 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
|
|
|
306
306
|
|
|
307
307
|
if (typeof this.uri === 'function') {
|
|
308
308
|
matches = matchQueries &&
|
|
309
|
-
|
|
309
|
+
common.matchStringOrRegexp(matchKey, this.basePath) &&
|
|
310
310
|
this.uri.call(this, path);
|
|
311
311
|
} else {
|
|
312
312
|
matches = method === this.method &&
|
package/lib/request_overrider.js
CHANGED
|
@@ -288,7 +288,7 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
288
288
|
interceptor.req = req;
|
|
289
289
|
req.headers = req.getHeaders ? req.getHeaders() : req._headers;
|
|
290
290
|
|
|
291
|
-
interceptor.scope.emit('request', req, interceptor);
|
|
291
|
+
interceptor.scope.emit('request', req, interceptor, requestBody);
|
|
292
292
|
|
|
293
293
|
if (typeof interceptor.errorMessage !== 'undefined') {
|
|
294
294
|
interceptor.interceptionCounter++;
|
|
@@ -313,14 +313,14 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
|
|
|
313
313
|
|
|
314
314
|
|
|
315
315
|
if (typeof interceptor.body === 'function') {
|
|
316
|
-
if (requestBody && common.isJSONContent(
|
|
317
|
-
if (requestBody && common.contentEncoding(
|
|
316
|
+
if (requestBody && common.isJSONContent(req.headers)) {
|
|
317
|
+
if (requestBody && common.contentEncoding(req.headers, 'gzip')) {
|
|
318
318
|
if (typeof zlib.gunzipSync !== 'function') {
|
|
319
319
|
emitError(new Error('Gzip encoding is currently not supported in this version of Node.'));
|
|
320
320
|
return;
|
|
321
321
|
}
|
|
322
322
|
requestBody = String(zlib.gunzipSync(new Buffer(requestBody, 'hex')), 'hex')
|
|
323
|
-
} else if (requestBody && common.contentEncoding(
|
|
323
|
+
} else if (requestBody && common.contentEncoding(req.headers, 'deflate')) {
|
|
324
324
|
if (typeof zlib.deflateSync !== 'function') {
|
|
325
325
|
emitError(new Error('Deflate encoding is currently not supported in this version of Node.'));
|
|
326
326
|
return;
|
package/lib/scope.js
CHANGED
|
@@ -9,7 +9,6 @@ var globalIntercept = require('./intercept')
|
|
|
9
9
|
, _ = require('lodash')
|
|
10
10
|
, debug = require('debug')('nock.scope')
|
|
11
11
|
, EventEmitter = require('events').EventEmitter
|
|
12
|
-
, extend = require('util')._extend
|
|
13
12
|
, globalEmitter = require('./global_emitter')
|
|
14
13
|
, util = require('util')
|
|
15
14
|
, Interceptor = require('./interceptor') ;
|
|
@@ -346,7 +345,7 @@ function define(nockDefs) {
|
|
|
346
345
|
return nocks;
|
|
347
346
|
}
|
|
348
347
|
|
|
349
|
-
module.exports =
|
|
348
|
+
module.exports = Object.assign(startScope, {
|
|
350
349
|
cleanAll: cleanAll,
|
|
351
350
|
activate: globalIntercept.activate,
|
|
352
351
|
isActive: globalIntercept.isActive,
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nock",
|
|
3
|
-
"description": "HTTP
|
|
3
|
+
"description": "HTTP server mocking and expectations library for Node.js",
|
|
4
4
|
"tags": [
|
|
5
5
|
"Mock",
|
|
6
6
|
"HTTP",
|
|
7
7
|
"testing",
|
|
8
8
|
"isolation"
|
|
9
9
|
],
|
|
10
|
-
"version": "9.
|
|
10
|
+
"version": "9.4.2",
|
|
11
11
|
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>",
|
|
12
12
|
"contributors": [
|
|
13
13
|
{
|
|
@@ -157,10 +157,10 @@
|
|
|
157
157
|
],
|
|
158
158
|
"repository": {
|
|
159
159
|
"type": "git",
|
|
160
|
-
"url": "https://github.com/
|
|
160
|
+
"url": "https://github.com/nock/nock.git"
|
|
161
161
|
},
|
|
162
162
|
"bugs": {
|
|
163
|
-
"url": "http://github.com/
|
|
163
|
+
"url": "http://github.com/nock/nock/issues"
|
|
164
164
|
},
|
|
165
165
|
"engines": [
|
|
166
166
|
"node >= 4.0"
|
|
@@ -181,7 +181,7 @@
|
|
|
181
181
|
"async": "^2.6.0",
|
|
182
182
|
"aws-sdk": "^2.202.0",
|
|
183
183
|
"coveralls": "^3.0.0",
|
|
184
|
-
"eslint": "^
|
|
184
|
+
"eslint": "^5.0.0",
|
|
185
185
|
"glob": "^7.1.1",
|
|
186
186
|
"hyperquest": "^2.1.3",
|
|
187
187
|
"isomorphic-fetch": "^2.2.0",
|
|
@@ -191,7 +191,7 @@
|
|
|
191
191
|
"nyc": "^12.0.1",
|
|
192
192
|
"request": "^2.83.0",
|
|
193
193
|
"request-promise": "^4.2.2",
|
|
194
|
-
"restify-clients": "^2.
|
|
194
|
+
"restify-clients": "^2.2.0",
|
|
195
195
|
"rimraf": "^2.6.2",
|
|
196
196
|
"semantic-release": "^15.0.0",
|
|
197
197
|
"superagent": "^3.8.2",
|
|
@@ -204,7 +204,7 @@
|
|
|
204
204
|
"coverage": "nyc tap --harmony ./tests/test_*.js",
|
|
205
205
|
"coveralls": "cat ./coverage/lcov.info | coveralls",
|
|
206
206
|
"lint": "eslint '**/*.js'",
|
|
207
|
-
"toc": "markdown-toc -i README.md",
|
|
207
|
+
"toc": "markdown-toc -i README.md && markdown-toc -i CONTRIBUTING.md ",
|
|
208
208
|
"semantic-release": "semantic-release"
|
|
209
209
|
},
|
|
210
210
|
"nyc": {
|