nock 9.3.3 → 9.4.3
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 -126
- package/lib/interceptor.js +1 -0
- package/lib/request_overrider.js +4 -4
- package/lib/scope.js +1 -2
- package/package.json +6 -6
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 coc+nock@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,10 @@
|
|
|
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
|
-
|
|
8
|
-
Nock is an HTTP mocking and expectations library for Node.js
|
|
7
|
+
> HTTP server mocking and expectations library for Node.js
|
|
9
8
|
|
|
10
9
|
Nock can be used to test modules that perform HTTP requests in isolation.
|
|
11
10
|
|
|
@@ -18,7 +17,7 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
|
|
|
18
17
|
- [How does it work?](#how-does-it-work)
|
|
19
18
|
- [Install](#install)
|
|
20
19
|
* [Node version support](#node-version-support)
|
|
21
|
-
- [
|
|
20
|
+
- [Usage](#usage)
|
|
22
21
|
* [READ THIS! - About interceptors](#read-this---about-interceptors)
|
|
23
22
|
* [Specifying hostname](#specifying-hostname)
|
|
24
23
|
* [Specifying path](#specifying-path)
|
|
@@ -48,7 +47,7 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
|
|
|
48
47
|
* [Request Body filtering](#request-body-filtering)
|
|
49
48
|
* [Request Headers Matching](#request-headers-matching)
|
|
50
49
|
* [Optional Requests](#optional-requests)
|
|
51
|
-
* [Allow __unmocked__ requests on a mocked hostname](#allow-
|
|
50
|
+
* [Allow __unmocked__ requests on a mocked hostname](#allow-__unmocked__-requests-on-a-mocked-hostname)
|
|
52
51
|
- [Expectations](#expectations)
|
|
53
52
|
* [.isDone()](#isdone)
|
|
54
53
|
* [.cleanAll()](#cleanall)
|
|
@@ -73,30 +72,27 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
|
|
|
73
72
|
- [Nock Back](#nock-back)
|
|
74
73
|
* [Setup](#setup)
|
|
75
74
|
+ [Options](#options)
|
|
76
|
-
* [Usage](#usage)
|
|
75
|
+
* [Usage](#usage-1)
|
|
77
76
|
+ [Options](#options-1)
|
|
78
77
|
+ [Modes](#modes)
|
|
79
78
|
- [Debugging](#debugging)
|
|
80
79
|
- [PROTIP](#protip)
|
|
81
80
|
- [Contributing](#contributing)
|
|
82
|
-
* [Generate README TOC](#generate-readme-toc)
|
|
83
|
-
* [Running tests](#running-tests)
|
|
84
|
-
+ [Airplane mode](#airplane-mode)
|
|
85
81
|
- [License](#license)
|
|
86
82
|
|
|
87
83
|
<!-- tocstop -->
|
|
88
84
|
|
|
89
|
-
|
|
85
|
+
## How does it work?
|
|
90
86
|
|
|
91
87
|
Nock works by overriding Node's `http.request` function. Also, it overrides `http.ClientRequest` too to cover for modules that use it directly.
|
|
92
88
|
|
|
93
|
-
|
|
89
|
+
## Install
|
|
94
90
|
|
|
95
91
|
```sh
|
|
96
|
-
$ npm install nock
|
|
92
|
+
$ npm install --save nock
|
|
97
93
|
```
|
|
98
94
|
|
|
99
|
-
|
|
95
|
+
### Node version support
|
|
100
96
|
|
|
101
97
|
| node | nock |
|
|
102
98
|
|---|---|
|
|
@@ -107,7 +103,7 @@ $ npm install nock
|
|
|
107
103
|
| 5 | up to 8.x |
|
|
108
104
|
| 6 | 9.x |
|
|
109
105
|
|
|
110
|
-
|
|
106
|
+
## Usage
|
|
111
107
|
|
|
112
108
|
On your test, you can setup your mocking object like this:
|
|
113
109
|
|
|
@@ -130,14 +126,14 @@ It will intercept an HTTP GET request to '/users/1' and reply with a status 200,
|
|
|
130
126
|
|
|
131
127
|
Then the test can call the module, and the module will do the HTTP requests.
|
|
132
128
|
|
|
133
|
-
|
|
129
|
+
### READ THIS! - About interceptors
|
|
134
130
|
|
|
135
131
|
When you setup an interceptor for a URL and that interceptor is used, it is removed from the interceptor list.
|
|
136
132
|
This means that you can intercept 2 or more calls to the same URL and return different things on each of them.
|
|
137
133
|
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
134
|
If you don’t want interceptors to be removed as they are used, you can use the [.persist()](#persist) method.
|
|
139
135
|
|
|
140
|
-
|
|
136
|
+
### Specifying hostname
|
|
141
137
|
|
|
142
138
|
The request hostname can be a string or a RegExp.
|
|
143
139
|
|
|
@@ -153,9 +149,9 @@ var scope = nock(/example\.com/)
|
|
|
153
149
|
.reply(200, 'domain regex matched');
|
|
154
150
|
```
|
|
155
151
|
|
|
156
|
-
>
|
|
152
|
+
> Note: You can choose to include or not the protocol in the hostname matching.
|
|
157
153
|
|
|
158
|
-
|
|
154
|
+
### Specifying path
|
|
159
155
|
|
|
160
156
|
The request path can be a string, a RegExp or a filter function and you can use any [HTTP verb](#http-verbs).
|
|
161
157
|
|
|
@@ -185,7 +181,7 @@ var scope = nock('http://www.example.com')
|
|
|
185
181
|
.reply(200, 'path using function matched');
|
|
186
182
|
```
|
|
187
183
|
|
|
188
|
-
|
|
184
|
+
### Specifying request body
|
|
189
185
|
|
|
190
186
|
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
187
|
|
|
@@ -232,7 +228,7 @@ nock('http://www.example.com')
|
|
|
232
228
|
```
|
|
233
229
|
|
|
234
230
|
|
|
235
|
-
|
|
231
|
+
### Specifying request query string
|
|
236
232
|
|
|
237
233
|
Nock understands query strings. Instead of placing the entire URL, you can specify the query part as an object:
|
|
238
234
|
|
|
@@ -281,7 +277,7 @@ nock('http://example.com')
|
|
|
281
277
|
.reply(200, {results: [{id: 'pgte'}]});
|
|
282
278
|
```
|
|
283
279
|
|
|
284
|
-
|
|
280
|
+
### Specifying replies
|
|
285
281
|
|
|
286
282
|
You can specify the return status code for a path on the first argument of reply like this:
|
|
287
283
|
|
|
@@ -380,7 +376,7 @@ var scope = nock('http://www.google.com')
|
|
|
380
376
|
});
|
|
381
377
|
```
|
|
382
378
|
|
|
383
|
-
|
|
379
|
+
##### Access original request and headers
|
|
384
380
|
|
|
385
381
|
If you're using the reply callback style, you can access the original client request using `this.req` like this:
|
|
386
382
|
|
|
@@ -394,7 +390,7 @@ var scope = nock('http://www.google.com')
|
|
|
394
390
|
});
|
|
395
391
|
```
|
|
396
392
|
|
|
397
|
-
|
|
393
|
+
#### Replying with errors
|
|
398
394
|
|
|
399
395
|
You can reply with an error like this:
|
|
400
396
|
|
|
@@ -412,16 +408,16 @@ nock('http://www.google.com')
|
|
|
412
408
|
.replyWithError({'message': 'something awful happened', 'code': 'AWFUL_ERROR'});
|
|
413
409
|
```
|
|
414
410
|
|
|
415
|
-
>
|
|
411
|
+
> Note: This will emit an `error` event on the `request` object, not the reply.
|
|
416
412
|
|
|
417
413
|
|
|
418
|
-
|
|
414
|
+
### Specifying headers
|
|
419
415
|
|
|
420
|
-
|
|
416
|
+
#### Header field names are case-insensitive
|
|
421
417
|
|
|
422
418
|
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
419
|
|
|
424
|
-
|
|
420
|
+
#### Specifying Request Headers
|
|
425
421
|
|
|
426
422
|
You can specify the request headers like this:
|
|
427
423
|
|
|
@@ -482,7 +478,7 @@ var scope = nock('http://www.example.com')
|
|
|
482
478
|
.reply(200);
|
|
483
479
|
```
|
|
484
480
|
|
|
485
|
-
|
|
481
|
+
#### Specifying Reply Headers
|
|
486
482
|
|
|
487
483
|
You can specify the reply headers like this:
|
|
488
484
|
|
|
@@ -508,7 +504,7 @@ var scope = nock('http://www.headdy.com')
|
|
|
508
504
|
});
|
|
509
505
|
```
|
|
510
506
|
|
|
511
|
-
|
|
507
|
+
#### Default Reply Headers
|
|
512
508
|
|
|
513
509
|
You can also specify default reply headers for all responses like this:
|
|
514
510
|
|
|
@@ -535,7 +531,7 @@ var scope = nock('http://www.headdy.com')
|
|
|
535
531
|
.reply(200, 'The default headers should come too');
|
|
536
532
|
```
|
|
537
533
|
|
|
538
|
-
|
|
534
|
+
#### Including Content-Length Header Automatically
|
|
539
535
|
|
|
540
536
|
When using `scope.reply()` to set a response body manually, you can have the
|
|
541
537
|
`Content-Length` header calculated automatically.
|
|
@@ -550,7 +546,7 @@ var scope = nock('http://www.headdy.com')
|
|
|
550
546
|
**NOTE:** this does not work with streams or other advanced means of specifying
|
|
551
547
|
the reply body.
|
|
552
548
|
|
|
553
|
-
|
|
549
|
+
#### Including Date Header Automatically
|
|
554
550
|
|
|
555
551
|
You can automatically append a `Date` header to your mock reply:
|
|
556
552
|
|
|
@@ -561,7 +557,7 @@ var scope = nock('http://www.headdy.com')
|
|
|
561
557
|
.reply(200, { hello: 'world' });
|
|
562
558
|
```
|
|
563
559
|
|
|
564
|
-
|
|
560
|
+
### HTTP Verbs
|
|
565
561
|
|
|
566
562
|
Nock supports any HTTP verb, and it has convenience methods for the GET, POST, PUT, HEAD, DELETE, PATCH and MERGE HTTP verbs.
|
|
567
563
|
|
|
@@ -573,7 +569,7 @@ var scope = nock('http://my.domain.com')
|
|
|
573
569
|
.reply(304);
|
|
574
570
|
```
|
|
575
571
|
|
|
576
|
-
|
|
572
|
+
### Support for HTTP and HTTPS
|
|
577
573
|
|
|
578
574
|
By default nock assumes HTTP. If you need to use HTTPS you can specify the `https://` prefix like this:
|
|
579
575
|
|
|
@@ -582,7 +578,7 @@ var scope = nock('https://secure.my.server.com')
|
|
|
582
578
|
// ...
|
|
583
579
|
```
|
|
584
580
|
|
|
585
|
-
|
|
581
|
+
### Non-standard ports
|
|
586
582
|
|
|
587
583
|
You are able to specify a non-standard port like this:
|
|
588
584
|
|
|
@@ -591,7 +587,7 @@ var scope = nock('http://my.server.com:8081')
|
|
|
591
587
|
...
|
|
592
588
|
```
|
|
593
589
|
|
|
594
|
-
|
|
590
|
+
### Repeat response n times
|
|
595
591
|
|
|
596
592
|
You are able to specify the number of times to repeat the same response.
|
|
597
593
|
|
|
@@ -613,7 +609,7 @@ nock('http://zombo.com').get('/').twice().reply(200, 'Ok');
|
|
|
613
609
|
nock('http://zombo.com').get('/').thrice().reply(200, 'Ok');
|
|
614
610
|
```
|
|
615
611
|
|
|
616
|
-
|
|
612
|
+
### Delay the response body
|
|
617
613
|
You are able to specify the number of milliseconds that the response body should be delayed. Response header will be replied immediately.
|
|
618
614
|
`delayBody(1000)` is equivalent to `delay({body: 1000})`.
|
|
619
615
|
|
|
@@ -627,7 +623,7 @@ nock('http://my.server.com')
|
|
|
627
623
|
|
|
628
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 it's `'end'` event until after the delay.
|
|
629
625
|
|
|
630
|
-
|
|
626
|
+
### Delay the response
|
|
631
627
|
|
|
632
628
|
You are able to specify the number of milliseconds that your reply should be delayed.
|
|
633
629
|
|
|
@@ -659,11 +655,11 @@ nock('http://my.server.com')
|
|
|
659
655
|
.reply(200, '<html></html>')
|
|
660
656
|
```
|
|
661
657
|
|
|
662
|
-
|
|
658
|
+
### Delay the connection
|
|
663
659
|
|
|
664
660
|
`delayConnection(1000)` is equivalent to `delay({head: 1000})`.
|
|
665
661
|
|
|
666
|
-
|
|
662
|
+
### Socket timeout
|
|
667
663
|
|
|
668
664
|
You are able to specify the number of milliseconds that your connection should be idle, to simulate a socket timeout.
|
|
669
665
|
|
|
@@ -688,7 +684,7 @@ req.end();
|
|
|
688
684
|
|
|
689
685
|
NOTE: the timeout will be fired immediately, and will not leave the simulated connection idle for the specified period of time.
|
|
690
686
|
|
|
691
|
-
|
|
687
|
+
### Chaining
|
|
692
688
|
|
|
693
689
|
You can chain behaviour like this:
|
|
694
690
|
|
|
@@ -714,7 +710,7 @@ var scope = nock('http://myapp.iriscouch.com')
|
|
|
714
710
|
});
|
|
715
711
|
```
|
|
716
712
|
|
|
717
|
-
|
|
713
|
+
### Scope filtering
|
|
718
714
|
|
|
719
715
|
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
716
|
|
|
@@ -730,7 +726,7 @@ var scope = nock('https://api.dropbox.com', {
|
|
|
730
726
|
.reply(200);
|
|
731
727
|
```
|
|
732
728
|
|
|
733
|
-
|
|
729
|
+
### Path filtering
|
|
734
730
|
|
|
735
731
|
You can also filter the URLs based on a function.
|
|
736
732
|
|
|
@@ -758,7 +754,7 @@ var scope = nock('http://api.myservice.com')
|
|
|
758
754
|
|
|
759
755
|
Note that `scope.filteringPath` is not cumulative: it should only be used once per scope.
|
|
760
756
|
|
|
761
|
-
|
|
757
|
+
### Request Body filtering
|
|
762
758
|
|
|
763
759
|
You can also filter the request body based on a function.
|
|
764
760
|
|
|
@@ -784,7 +780,7 @@ var scope = nock('http://api.myservice.com')
|
|
|
784
780
|
.reply(201, 'OK');
|
|
785
781
|
```
|
|
786
782
|
|
|
787
|
-
|
|
783
|
+
### Request Headers Matching
|
|
788
784
|
|
|
789
785
|
If you need to match requests only if certain request headers match, you can.
|
|
790
786
|
|
|
@@ -821,7 +817,7 @@ var scope = nock('http://api.myservice.com')
|
|
|
821
817
|
})
|
|
822
818
|
```
|
|
823
819
|
|
|
824
|
-
|
|
820
|
+
### Optional Requests
|
|
825
821
|
|
|
826
822
|
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
823
|
|
|
@@ -838,7 +834,7 @@ example.get("/pathB").optionally().reply(200);
|
|
|
838
834
|
example.pendingMocks() // []
|
|
839
835
|
```
|
|
840
836
|
|
|
841
|
-
|
|
837
|
+
### Allow __unmocked__ requests on a mocked hostname
|
|
842
838
|
|
|
843
839
|
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
840
|
|
|
@@ -852,9 +848,9 @@ var scope = nock('http://my.existing.service.com', options)
|
|
|
852
848
|
// GET /other/url => actually makes request to the server
|
|
853
849
|
```
|
|
854
850
|
|
|
855
|
-
>
|
|
851
|
+
> Note: When applying `{allowUnmocked: true}`, if the request is made to the real server, no interceptor is removed.
|
|
856
852
|
|
|
857
|
-
|
|
853
|
+
## Expectations
|
|
858
854
|
|
|
859
855
|
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
856
|
|
|
@@ -874,7 +870,7 @@ setTimeout(function() {
|
|
|
874
870
|
}, 5000);
|
|
875
871
|
```
|
|
876
872
|
|
|
877
|
-
|
|
873
|
+
### .isDone()
|
|
878
874
|
|
|
879
875
|
You can call `isDone()` on a single expectation to determine if the expectation was met:
|
|
880
876
|
|
|
@@ -892,14 +888,14 @@ It is also available in the global scope, which will determine if all expectatio
|
|
|
892
888
|
nock.isDone();
|
|
893
889
|
```
|
|
894
890
|
|
|
895
|
-
|
|
891
|
+
### .cleanAll()
|
|
896
892
|
|
|
897
893
|
You can cleanup all the prepared mocks (could be useful to cleanup some state after a failed test) like this:
|
|
898
894
|
|
|
899
895
|
```js
|
|
900
896
|
nock.cleanAll();
|
|
901
897
|
```
|
|
902
|
-
|
|
898
|
+
### .persist()
|
|
903
899
|
|
|
904
900
|
You can make all the interceptors for a scope persist by calling `.persist()` on it:
|
|
905
901
|
|
|
@@ -920,7 +916,7 @@ var scope = nock('http://example.com').persist().get('/').reply(200, 'ok');
|
|
|
920
916
|
scope.persist(false);
|
|
921
917
|
```
|
|
922
918
|
|
|
923
|
-
|
|
919
|
+
### .pendingMocks()
|
|
924
920
|
|
|
925
921
|
If a scope is not done, you can inspect the scope to infer which ones are still pending using the `scope.pendingMocks()` function:
|
|
926
922
|
|
|
@@ -936,7 +932,7 @@ It is also available in the global scope:
|
|
|
936
932
|
console.error('pending mocks: %j', nock.pendingMocks());
|
|
937
933
|
```
|
|
938
934
|
|
|
939
|
-
|
|
935
|
+
### .activeMocks()
|
|
940
936
|
|
|
941
937
|
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
938
|
|
|
@@ -952,7 +948,7 @@ It is also available in the global scope:
|
|
|
952
948
|
console.error('active mocks: %j', nock.activeMocks());
|
|
953
949
|
```
|
|
954
950
|
|
|
955
|
-
|
|
951
|
+
### .isActive()
|
|
956
952
|
|
|
957
953
|
Your tests may sometimes want to deactivate the nock interceptor.
|
|
958
954
|
Once deactivated, nock needs to be re-activated to work.
|
|
@@ -963,7 +959,7 @@ Sample:
|
|
|
963
959
|
if (!nock.isActive()) nock.activate()
|
|
964
960
|
```
|
|
965
961
|
|
|
966
|
-
|
|
962
|
+
## Logging
|
|
967
963
|
|
|
968
964
|
Nock can log matches if you pass in a log function like this:
|
|
969
965
|
|
|
@@ -973,7 +969,7 @@ var google = nock('http://google.com')
|
|
|
973
969
|
...
|
|
974
970
|
```
|
|
975
971
|
|
|
976
|
-
|
|
972
|
+
## Restoring
|
|
977
973
|
|
|
978
974
|
You can restore the HTTP interceptor to the normal unmocked behaviour by calling:
|
|
979
975
|
|
|
@@ -984,7 +980,7 @@ nock.restore();
|
|
|
984
980
|
|
|
985
981
|
**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
982
|
|
|
987
|
-
|
|
983
|
+
## Activating
|
|
988
984
|
|
|
989
985
|
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
986
|
|
|
@@ -994,7 +990,7 @@ nock.activate();
|
|
|
994
990
|
|
|
995
991
|
**note**: To check if nock HTTP interceptor is active or deactive, use [nock.isActive()](#isactive).
|
|
996
992
|
|
|
997
|
-
|
|
993
|
+
## Turning Nock Off (experimental!)
|
|
998
994
|
|
|
999
995
|
You can bypass Nock completely by setting `NOCK_OFF` environment variable to `"true"`.
|
|
1000
996
|
|
|
@@ -1004,7 +1000,7 @@ This way you can have your tests hit the real servers just by switching on this
|
|
|
1004
1000
|
$ NOCK_OFF=true node my_test.js
|
|
1005
1001
|
```
|
|
1006
1002
|
|
|
1007
|
-
|
|
1003
|
+
## Enable/Disable real HTTP request
|
|
1008
1004
|
|
|
1009
1005
|
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
1006
|
|
|
@@ -1064,7 +1060,7 @@ nock.cleanAll();
|
|
|
1064
1060
|
nock.enableNetConnect();
|
|
1065
1061
|
```
|
|
1066
1062
|
|
|
1067
|
-
|
|
1063
|
+
## Recording
|
|
1068
1064
|
|
|
1069
1065
|
This is a cool feature:
|
|
1070
1066
|
|
|
@@ -1084,7 +1080,7 @@ In order to stop recording you should call `nock.restore()` and recording will s
|
|
|
1084
1080
|
|
|
1085
1081
|
**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
1082
|
|
|
1087
|
-
|
|
1083
|
+
### `dont_print` option
|
|
1088
1084
|
|
|
1089
1085
|
If you just want to capture the generated code into a var as an array you can use:
|
|
1090
1086
|
|
|
@@ -1102,7 +1098,7 @@ Copy and paste that code into your tests, customize at will, and you're done! Yo
|
|
|
1102
1098
|
|
|
1103
1099
|
(Remember that you should do this one test at a time).
|
|
1104
1100
|
|
|
1105
|
-
|
|
1101
|
+
### `output_objects` option
|
|
1106
1102
|
|
|
1107
1103
|
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
1104
|
|
|
@@ -1165,7 +1161,7 @@ nockDefs.forEach(function(def) {
|
|
|
1165
1161
|
var nocks = nock.define(nockDefs);
|
|
1166
1162
|
```
|
|
1167
1163
|
|
|
1168
|
-
|
|
1164
|
+
### `enable_reqheaders_recording` option
|
|
1169
1165
|
|
|
1170
1166
|
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
1167
|
|
|
@@ -1181,7 +1177,7 @@ nock.recorder.rec({
|
|
|
1181
1177
|
|
|
1182
1178
|
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
1179
|
|
|
1184
|
-
|
|
1180
|
+
### `logging` option
|
|
1185
1181
|
|
|
1186
1182
|
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
1183
|
|
|
@@ -1194,7 +1190,7 @@ nock.recorder.rec({
|
|
|
1194
1190
|
});
|
|
1195
1191
|
```
|
|
1196
1192
|
|
|
1197
|
-
|
|
1193
|
+
### `use_separator` option
|
|
1198
1194
|
|
|
1199
1195
|
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
1196
|
|
|
@@ -1206,7 +1202,7 @@ nock.recorder.rec({
|
|
|
1206
1202
|
});
|
|
1207
1203
|
```
|
|
1208
1204
|
|
|
1209
|
-
|
|
1205
|
+
### .removeInterceptor()
|
|
1210
1206
|
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
1207
|
|
|
1212
1208
|
Examples:
|
|
@@ -1232,14 +1228,14 @@ var interceptor = nock('http://example.org')
|
|
|
1232
1228
|
nock.removeInterceptor(interceptor);
|
|
1233
1229
|
```
|
|
1234
1230
|
|
|
1235
|
-
|
|
1231
|
+
## Events
|
|
1236
1232
|
|
|
1237
1233
|
A scope emits the following events:
|
|
1238
1234
|
|
|
1239
|
-
* `emit('request', function(req, interceptor))`;
|
|
1235
|
+
* `emit('request', function(req, interceptor, body))`;
|
|
1240
1236
|
* `emit('replied', function(req, interceptor))`;
|
|
1241
1237
|
|
|
1242
|
-
|
|
1238
|
+
### Global no match event
|
|
1243
1239
|
|
|
1244
1240
|
You can also listen for no match events like this:
|
|
1245
1241
|
|
|
@@ -1249,11 +1245,11 @@ nock.emitter.on('no match', function(req) {
|
|
|
1249
1245
|
});
|
|
1250
1246
|
```
|
|
1251
1247
|
|
|
1252
|
-
|
|
1248
|
+
## Nock Back
|
|
1253
1249
|
|
|
1254
1250
|
fixture recording support and playback
|
|
1255
1251
|
|
|
1256
|
-
|
|
1252
|
+
### Setup
|
|
1257
1253
|
|
|
1258
1254
|
**You must specify a fixture directory before using, for example:
|
|
1259
1255
|
|
|
@@ -1266,13 +1262,13 @@ nockBack.fixtures = '/path/to/fixtures/';
|
|
|
1266
1262
|
nockBack.setMode('record');
|
|
1267
1263
|
```
|
|
1268
1264
|
|
|
1269
|
-
|
|
1265
|
+
#### Options
|
|
1270
1266
|
|
|
1271
1267
|
- `nockBack.fixtures` : path to fixture directory
|
|
1272
1268
|
- `nockBack.setMode()` : the mode to use
|
|
1273
1269
|
|
|
1274
1270
|
|
|
1275
|
-
|
|
1271
|
+
### Usage
|
|
1276
1272
|
|
|
1277
1273
|
By default if the fixture doesn't exist, a `nockBack` will create a new fixture and save the recorded output
|
|
1278
1274
|
for you. The next time you run the test, if the fixture exists, it will be loaded in.
|
|
@@ -1335,7 +1331,7 @@ return nockBack('promisedFixture.json')
|
|
|
1335
1331
|
});
|
|
1336
1332
|
```
|
|
1337
1333
|
|
|
1338
|
-
|
|
1334
|
+
#### Options
|
|
1339
1335
|
|
|
1340
1336
|
As an optional second parameter you can pass the following options
|
|
1341
1337
|
|
|
@@ -1345,7 +1341,7 @@ As an optional second parameter you can pass the following options
|
|
|
1345
1341
|
- `recorder`: custom options to pass to the recorder
|
|
1346
1342
|
|
|
1347
1343
|
|
|
1348
|
-
|
|
1344
|
+
#### Modes
|
|
1349
1345
|
|
|
1350
1346
|
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
1347
|
|
|
@@ -1357,14 +1353,14 @@ to set the mode call `nockBack.setMode(mode)` or run the tests with the `NOCK_BA
|
|
|
1357
1353
|
|
|
1358
1354
|
- lockdown: use recorded nocks, disables all http calls even when not nocked, doesn't record
|
|
1359
1355
|
|
|
1360
|
-
|
|
1356
|
+
## Debugging
|
|
1361
1357
|
Nock uses debug, so just run with environmental variable DEBUG set to nock.*
|
|
1362
1358
|
|
|
1363
1359
|
```js
|
|
1364
1360
|
$ DEBUG=nock.* node my_test.js
|
|
1365
1361
|
```
|
|
1366
1362
|
|
|
1367
|
-
|
|
1363
|
+
## PROTIP
|
|
1368
1364
|
|
|
1369
1365
|
If you don't want to match the request body you can use this trick (by @theycallmeswift):
|
|
1370
1366
|
|
|
@@ -1377,57 +1373,15 @@ var scope = nock('http://api.myservice.com')
|
|
|
1377
1373
|
.reply(200, 'OK');
|
|
1378
1374
|
```
|
|
1379
1375
|
|
|
1380
|
-
|
|
1376
|
+
## Contributing
|
|
1377
|
+
|
|
1378
|
+
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
1379
|
|
|
1382
1380
|
Please note that this project is released with a [Contributor Code of Conduct](./CODE_OF_CONDUCT.md).
|
|
1383
1381
|
By participating in this project you agree to abide by its terms.
|
|
1384
1382
|
|
|
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
|
|
1383
|
+
## License
|
|
1430
1384
|
|
|
1431
1385
|
[MIT](LICENSE)
|
|
1432
1386
|
|
|
1433
|
-
Copyright (c) 2011-2017 [Pedro Teixeira](http://about.me/pedroteixeira) and other [contributors](https://github.com/
|
|
1387
|
+
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,6 +306,7 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
|
|
|
306
306
|
|
|
307
307
|
if (typeof this.uri === 'function') {
|
|
308
308
|
matches = matchQueries &&
|
|
309
|
+
method === this.method &&
|
|
309
310
|
common.matchStringOrRegexp(matchKey, this.basePath) &&
|
|
310
311
|
this.uri.call(this, path);
|
|
311
312
|
} else {
|
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.3",
|
|
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",
|
|
@@ -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": {
|