nock 8.0.0 → 8.2.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.
Files changed (42) hide show
  1. package/.travis.yml +1 -0
  2. package/CHANGELOG.md +107 -114
  3. package/README.md +103 -42
  4. package/lib/common.js +15 -1
  5. package/lib/intercept.js +24 -11
  6. package/lib/interceptor.js +21 -22
  7. package/lib/match_body.js +1 -1
  8. package/lib/recorder.js +6 -3
  9. package/lib/request_overrider.js +3 -3
  10. package/lib/scope.js +30 -28
  11. package/package.json +10 -7
  12. package/tests/test_back.js +5 -2
  13. package/tests/test_body_match.js +40 -0
  14. package/tests/test_browserify.js +54 -52
  15. package/tests/test_intercept.js +195 -47
  16. package/tests/test_isomorphic_fetch.js +45 -45
  17. package/.npmignore +0 -2
  18. package/coverage/coverage.json +0 -1
  19. package/coverage/lcov-report/base.css +0 -212
  20. package/coverage/lcov-report/index.html +0 -106
  21. package/coverage/lcov-report/nock/index.html +0 -93
  22. package/coverage/lcov-report/nock/index.js.html +0 -98
  23. package/coverage/lcov-report/nock/lib/back.js.html +0 -1022
  24. package/coverage/lcov-report/nock/lib/common.js.html +0 -1070
  25. package/coverage/lcov-report/nock/lib/delayed_body.js.html +0 -305
  26. package/coverage/lcov-report/nock/lib/global_emitter.js.html +0 -71
  27. package/coverage/lcov-report/nock/lib/index.html +0 -223
  28. package/coverage/lcov-report/nock/lib/intercept.js.html +0 -1253
  29. package/coverage/lcov-report/nock/lib/interceptor.js.html +0 -1670
  30. package/coverage/lcov-report/nock/lib/match_body.js.html +0 -275
  31. package/coverage/lcov-report/nock/lib/mixin.js.html +0 -107
  32. package/coverage/lcov-report/nock/lib/recorder.js.html +0 -1323
  33. package/coverage/lcov-report/nock/lib/request_overrider.js.html +0 -1643
  34. package/coverage/lcov-report/nock/lib/scope.js.html +0 -1133
  35. package/coverage/lcov-report/nock/lib/socket.js.html +0 -272
  36. package/coverage/lcov-report/prettify.css +0 -1
  37. package/coverage/lcov-report/prettify.js +0 -1
  38. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  39. package/coverage/lcov-report/sorter.js +0 -158
  40. package/coverage/lcov.info +0 -2428
  41. package/tests/browserify-public/browserify-bundle.js +0 -34332
  42. package/tests/test.js +0 -32
package/.travis.yml CHANGED
@@ -5,6 +5,7 @@ node_js:
5
5
  - "0.12"
6
6
  - 4
7
7
  - 5
8
+ - 6
8
9
  script: "npm run coverage"
9
10
  sudo: false
10
11
  after_success: "npm run coveralls"
package/CHANGELOG.md CHANGED
@@ -1,3 +1,110 @@
1
+ 8.2.2 / 2017-10-15
2
+ ==================
3
+
4
+ * Set `this` to request on socket event. [#993](https://github.com/node-nock/nock/pull/993)
5
+
6
+ 8.2.0 / 2016-10-23
7
+ ==================
8
+
9
+ * 8.2.0
10
+ * Add changelog to the dev dependencies
11
+ * Ignore browserify-bundle.js
12
+ * Update browserify-bundle.js
13
+ * Merge pull request [#723](https://github.com/node-nock/nock/issues/723) from pimterry/optional-mocks
14
+ Add optionally() and make isDone and pendingMocks consistent.
15
+ * Include all review markup changes in browserify-bundle
16
+ * Add .activeMocks() to recreate previous .pendingMocks() behaviour
17
+ * Add times+optional and persist+optional tests
18
+ * Document assumption in pendingMocks()
19
+ * Clarify optional mock behaviour a little in README
20
+ * Merge pull request [#721](https://github.com/node-nock/nock/issues/721) from pimterry/removeInterceptor-from-pending
21
+ Update PendingMocks when an interceptor is removed
22
+ * Merge pull request [#722](https://github.com/node-nock/nock/issues/722) from Ginden/no-garbage-in-npm
23
+ Removed coverage folder from npm
24
+ * Merge pull request [#668](https://github.com/node-nock/nock/issues/668) from abdulito/master
25
+ Support http OPTIONS method
26
+ * Merge pull request [#725](https://github.com/node-nock/nock/issues/725) from node-nock/greenkeeper-async-2.1.1
27
+ Update async to version 2.1.1 🚀
28
+ * Merge pull request [#719](https://github.com/node-nock/nock/issues/719) from ltegman/fix/body-spec-regex-in-array
29
+ Handle arrays like objects in match_body
30
+ * Handle arrays like objects in match_body
31
+ * chore(package): update async to version 2.1.1
32
+ https://greenkeeper.io/
33
+ * Ad documentation for optionally()
34
+ * Add optionally(), to replace and improve the undocumented 'requireDone'
35
+ * Make pendingMocks and isDone behave consistently
36
+ * Removed coverage folder from npm
37
+ * Update PendingMocks when an interceptor is removed
38
+ * Update CHANGELOG
39
+
40
+ 8.1.0 / 2016-10-10
41
+ ==================
42
+
43
+ * 8.1.0
44
+ * Enable harmony mode when running tests
45
+ * Remove duplicated Interceptor.replyWithFile function
46
+ * Merge pull request [#710](https://github.com/node-nock/nock/issues/710) from hugoduraes/master
47
+ Leave rawHeaders case untouched on nock's replies
48
+ * Leave rawHeaders case untouched on nock's replies
49
+ * Re-enable previously skipped tests
50
+ * Merge pull request [#586](https://github.com/node-nock/nock/issues/586) from Elergy/master
51
+ Fix some broken tests
52
+ * Add nodejs v6 to travis config
53
+ * use public TravisCI badge icon, which shows failing tests.
54
+ * Skip a couple of failing specs (until we get them fixed)
55
+ * Remove tests/test.js
56
+ Also:
57
+ - Use nyc as the CLI of istanbul
58
+ - Use tap binary directly for running tests
59
+ * Support http OPTIONS method
60
+ * Fix some issues to run library with Node v0.10:
61
+ 1. Set lodash version to ~4.9.0
62
+ 2. Add data-event listener for http's res message
63
+ * Tests that depends on amazon.com's statusCode are fixed
64
+ Symbol with charCode 160 is replaced to space
65
+ * Merge pull request [#531](https://github.com/node-nock/nock/issues/531) from node-nock/greenkeeper-request-2.71.0
66
+ Update request to version 2.71.0 🚀
67
+ * Merge branch 'master' of github.com:node-nock/nock
68
+ * added snyk vulerabilities badge
69
+ * chore(package): update request to version 2.71.0
70
+ http://greenkeeper.io/
71
+ * Merge pull request [#528](https://github.com/node-nock/nock/issues/528) from mooyoul/improve-readme-toc
72
+ Improved Table of Contents section of README
73
+ * Changed `About Interceptors` title of TOC
74
+ ... just to clarify
75
+ * Lint README.md
76
+ * Added some missing TOC entries
77
+ * Fixed broken TOC link
78
+ * changelog update
79
+
80
+ 8.0.0 / 2016-04-06
81
+ ==================
82
+
83
+ * v8.0.0
84
+ * added missing dep
85
+ * Merge branch 'greenkeeper-request-2.70.0'
86
+ * Merge branch 'master' into greenkeeper-request-2.70.0
87
+ * making the latest version of lodash work
88
+ * Merge pull request [#521](https://github.com/node-nock/nock/issues/521) from node-nock/greenkeeper-update-all
89
+ Update all dependencies 🌴
90
+ * fixes for latest tap version
91
+ * chore(package): update request to version 2.70.0
92
+ http://greenkeeper.io/
93
+ * chore(package): update dependencies
94
+ http://greenkeeper.io/
95
+
96
+ 7.7.3 / 2016-04-04
97
+ ==================
98
+
99
+ * v7.7.3
100
+ * Merge pull request [#520](https://github.com/node-nock/nock/issues/520) from ericsaboia/master
101
+ properly remove interceptor with regex domain from the list after used
102
+ * properly remove interceptor with regex domain from the list after used
103
+ * Merge pull request [#519](https://github.com/node-nock/nock/issues/519) from Byron-TW/patch-1
104
+ Fix typo
105
+ * Fix typo
106
+ * changelog update
107
+
1
108
  7.7.2 / 2016-03-25
2
109
  ==================
3
110
 
@@ -73,117 +180,3 @@
73
180
  * Add support for full callback `reply()`.
74
181
  This addresses a bug with the `reply()` function where a request without
75
182
  a body caused full callbacks to error. (See pgte/nock[#489](https://github.com/node-nock/nock/issues/489))
76
- * changelog update
77
-
78
- 7.3.0 / 2016-02-24
79
- ==================
80
-
81
- * v7.3.0
82
- * browserify bundle update
83
- * Merge branch 'Limess-remove-interceptor-by-instance'
84
- * Call removeInterceptor() with instance
85
- Previously removeInterceptor() only took an options object. This was
86
- inconvenient if the client didn't wish to expose urls directly in their
87
- tests.
88
- removeInterceptor now also takes an instance of `Interceptor` and uses
89
- the instance variables `baseUrl` and `_key` to filter for removal.
90
- Added 3 test cases to cover http, https and regex path matching.
91
- * Use lodash size function
92
- You cannot effectively compare anything to an empty object declaration. {} === {} will always equal false. This is to address the attended purpose by the comment provided.
93
- * changelog update
94
-
95
- 7.2.2 / 2016-02-19
96
- ==================
97
-
98
- * v7.2.2
99
- * browserify bundle update
100
- * Merge branch 'mdlavin-v7-performance-improvement'
101
- * Increase test coverage of allowUnmocked when only mismatch is the body
102
- * Reuse _.defaults where possible to reduce code
103
- * Do not stringify request options unless they will be logged
104
- * Optimize for large number of interceptors
105
- * changelog update
106
-
107
- 7.2.1 / 2016-02-15
108
- ==================
109
-
110
- * v7.2.1
111
- * Merge branch 'owiber-oliver-callbackfix'
112
- * Merge branch 'oliver-callbackfix' of git://github.com/owiber/nock into owiber-oliver-callbackfix
113
-
114
- 7.2.0 / 2016-02-15
115
- ==================
116
-
117
- * v7.2.0
118
- * Merge branch 'master' of github.com:pgte/nock
119
- * mkdirp is optional because browsers. addresses [#475](https://github.com/node-nock/nock/issues/475)
120
- * fix key when interceptor path is specified with callback
121
- * Merge pull request [#465](https://github.com/node-nock/nock/issues/465) from JemiloII/patch-1
122
- * Fixing typos
123
- * added @BinChang as contributor
124
- * changelog update
125
-
126
- 7.1.0 / 2016-01-29
127
- ==================
128
-
129
- * v7.1.0
130
- * Merge branch 'BinChang-delayBody'
131
- * add a new api delayBody() and update the syntax of delay().
132
- * Merge pull request [#462](https://github.com/node-nock/nock/issues/462) from four43/patch-2
133
- Add clean all to example about being done with the test
134
- * Add clean all to example about being done with the test
135
-
136
- 7.0.2 / 2016-01-27
137
- ==================
138
-
139
- * v7.0.2
140
- * isStream test fixed for null objects
141
-
142
- 7.0.1 / 2016-01-27
143
- ==================
144
-
145
- * v7.0.1
146
- * for when content type header is an array. should fix [#460](https://github.com/node-nock/nock/issues/460)
147
- * Merge branch 'BinChang-common_is_stream'
148
- * remove isStream to the common lib.
149
-
150
- 7.0.0 / 2016-01-25
151
- ==================
152
-
153
- * v7.0.0
154
- * Merge branch 'wprater-patch-1'
155
- * Merge branch 'patch-1' of git://github.com/wprater/nock into wprater-patch-1
156
- * Merge branch 'BinChang-scope_interceptor'
157
- * updated broswerify bundle
158
- * Merge branch 'scope_interceptor' of git://github.com/BinChang/nock into BinChang-scope_interceptor
159
- * changelog update
160
-
161
- 6.0.1 / 2016-01-25
162
- ==================
163
-
164
- * v6.0.1
165
- * Merge branch 'alekbarszczewski-reply-headers-fix'
166
- * browserify bundle update
167
- * dont execute `replace` on a non-string
168
- recorded bodies can be an object (json). dont try to use a string function on them.
169
- * rebase with master
170
- * move interceptor to its own file.
171
- * split scope and intercept
172
- * Fixed issue with header function evaluation + added 2 tests
173
- * changelog update
174
-
175
- 6.0.0 / 2016-01-21
176
- ==================
177
-
178
- * v6.0.0
179
- * Merge branch 'BinChang-complex_query_string'
180
- * add qs into package dependency block.
181
- * add test_complex_querystring into test list
182
- * minor update
183
- * support complex GET query based the behavior of request module.
184
- * changelog update
185
-
186
- 5.5.0 / 2016-01-19
187
- ==================
188
-
189
- * v5.5.0
package/README.md CHANGED
@@ -1,65 +1,86 @@
1
1
  # Nock
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/node-nock/nock.svg?branch=master)](http://travis-ci.org/node-nock/nock?branch=master)
3
+ [![Build Status](https://travis-ci.org/node-nock/nock.svg?branch=master)](https://travis-ci.org/node-nock/nock)
4
4
  [![Coverage Status](https://coveralls.io/repos/github/node-nock/nock/badge.svg?branch=master)](https://coveralls.io/github/node-nock/nock?branch=master)
5
+ [![Known Vulnerabilities](https://snyk.io/test/npm/nock/badge.svg)](https://snyk.io/test/npm/nock)
5
6
  [![Chat](https://img.shields.io/badge/help-gitter-eb9348.svg?style=flat)](https://gitter.im/node-nock/nock)
6
7
 
8
+
7
9
  Nock is an HTTP mocking and expectations library for Node.js
8
10
 
9
11
  Nock can be used to test modules that perform HTTP requests in isolation.
10
12
 
11
13
  For instance, if a module performs HTTP requests to a CouchDB server or makes HTTP requests to the Amazon API, you can test that module in isolation.
12
14
 
13
- <!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc/generate-toc again -->
14
15
  **Table of Contents**
15
16
 
17
+ <!-- toc -->
18
+
16
19
  - [Install](#install)
20
+ * [Node version support](#node-version-support)
17
21
  - [Use](#use)
18
- - [READ THIS](#read-this---about-interceptors)
19
- - [Specifying domain](#specifying-domain)
20
- - [Specifying path](#specifying-path)
21
- - [Specifying request body](#specifying-request-body)
22
- - [Specifying replies](#specifying-replies)
23
- - [Specifying headers](#specifying-headers)
24
- - [Header field names are case-insensitive](#header-field-names-are-case-insensitive)
25
- - [Specifying Request Headers](#specifying-request-headers)
26
- - [Specifying Reply Headers](#specifying-reply-headers)
27
- - [Default Reply Headers](#default-reply-headers)
28
- - [HTTP Verbs](#http-verbs)
29
- - [Support for HTTP and HTTPS](#support-for-http-and-https)
30
- - [Non-standard ports](#non-standard-ports)
31
- - [Repeat response n times](#repeat-response-n-times)
32
- - [Delay the response body](#delay-the-response-body)
33
- - [Delay the response](#delay-the-response)
34
- - [Delay the connection](#delay-the-connection)
35
- - [Socket timeout](#socket-timeout)
36
- - [Chaining](#chaining)
37
- - [Scope filtering](#scope-filtering)
38
- - [Path filtering](#path-filtering)
39
- - [Request Body filtering](#request-body-filtering)
40
- - [Request Headers Matching](#request-headers-matching)
41
- - [Allow __unmocked__ requests on a mocked hostname](#allow-unmocked-requests-on-a-mocked-hostname)
22
+ * [READ THIS! - About interceptors](#read-this---about-interceptors)
23
+ * [Specifying hostname](#specifying-hostname)
24
+ * [Specifying path](#specifying-path)
25
+ * [Specifying request body](#specifying-request-body)
26
+ * [Specifying request query string](#specifying-request-query-string)
27
+ * [Specifying replies](#specifying-replies)
28
+ - [Access original request and headers](#access-original-request-and-headers)
29
+ + [Replying with errors](#replying-with-errors)
30
+ * [Specifying headers](#specifying-headers)
31
+ + [Header field names are case-insensitive](#header-field-names-are-case-insensitive)
32
+ + [Specifying Request Headers](#specifying-request-headers)
33
+ + [Specifying Reply Headers](#specifying-reply-headers)
34
+ + [Default Reply Headers](#default-reply-headers)
35
+ + [Including Content-Length Header Automatically](#including-content-length-header-automatically)
36
+ + [Including Date Header Automatically](#including-date-header-automatically)
37
+ * [HTTP Verbs](#http-verbs)
38
+ * [Support for HTTP and HTTPS](#support-for-http-and-https)
39
+ * [Non-standard ports](#non-standard-ports)
40
+ * [Repeat response n times](#repeat-response-n-times)
41
+ * [Delay the response body](#delay-the-response-body)
42
+ * [Delay the response](#delay-the-response)
43
+ * [Delay the connection](#delay-the-connection)
44
+ * [Socket timeout](#socket-timeout)
45
+ * [Chaining](#chaining)
46
+ * [Scope filtering](#scope-filtering)
47
+ * [Path filtering](#path-filtering)
48
+ * [Request Body filtering](#request-body-filtering)
49
+ * [Request Headers Matching](#request-headers-matching)
50
+ * [Optional Requests](#optional-requests)
51
+ * [Allow __unmocked__ requests on a mocked hostname](#allow-__unmocked__-requests-on-a-mocked-hostname)
42
52
  - [Expectations](#expectations)
43
- - [.isDone()](#isdone)
44
- - [.cleanAll()](#cleanall)
45
- - [.persist()](#persist)
46
- - [.pendingMocks()](#pendingmocks)
53
+ * [.isDone()](#isdone)
54
+ * [.cleanAll()](#cleanall)
55
+ * [.persist()](#persist)
56
+ * [.pendingMocks()](#pendingmocks)
57
+ * [.activeMocks()](#activemocks)
47
58
  - [Logging](#logging)
48
59
  - [Restoring](#restoring)
49
60
  - [Turning Nock Off (experimental!)](#turning-nock-off-experimental)
50
61
  - [Enable/Disable real HTTP request](#enabledisable-real-http-request)
51
62
  - [Recording](#recording)
52
- - [`dont_print` option](#dontprint-option)
53
- - [`output_objects` option](#outputobjects-option)
54
- - [`enable_reqheaders_recording` option](#enablereqheadersrecording-option)
55
- - [.removeInterceptor()](#removeinterceptor)
63
+ * [`dont_print` option](#dont_print-option)
64
+ * [`output_objects` option](#output_objects-option)
65
+ * [`enable_reqheaders_recording` option](#enable_reqheaders_recording-option)
66
+ * [`logging` option](#logging-option)
67
+ * [`use_separator` option](#use_separator-option)
68
+ * [.removeInterceptor()](#removeinterceptor)
69
+ - [Events](#events)
70
+ * [Global no match event](#global-no-match-event)
71
+ - [Nock Back](#nock-back)
72
+ * [Setup](#setup)
73
+ + [Options](#options)
74
+ * [Usage](#usage)
75
+ + [Options](#options-1)
76
+ + [Modes](#modes)
56
77
  - [How does it work?](#how-does-it-work)
57
78
  - [Debugging](#debugging)
58
79
  - [PROTIP](#protip)
80
+ - [Generate Changelog](#generate-changelog)
59
81
  - [License](#license)
60
82
 
61
- <!-- markdown-toc end -->
62
-
83
+ <!-- tocstop -->
63
84
 
64
85
  # Install
65
86
 
@@ -67,6 +88,17 @@ For instance, if a module performs HTTP requests to a CouchDB server or makes HT
67
88
  $ npm install nock
68
89
  ```
69
90
 
91
+ ## Node version support
92
+
93
+ | node | nock |
94
+ |---|---|
95
+ | 0.10 | up to 8.x |
96
+ | 0.11 | up to 8.x |
97
+ | 0.12 | up to 8.x |
98
+ | 4 | 8.x |
99
+ | 5 | up to 8.x |
100
+ | 6 | 8.x |
101
+
70
102
  # Use
71
103
 
72
104
  On your test, you can setup your mocking object like this:
@@ -793,6 +825,23 @@ var scope = nock('http://api.myservice.com')
793
825
  })
794
826
  ```
795
827
 
828
+ ## Optional Requests
829
+
830
+ 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.
831
+
832
+ ```js
833
+ var example = nock("http://example.com");
834
+ example.pendingMocks() // []
835
+ example.get("/pathA").reply(200);
836
+ example.pendingMocks() // ["GET http://example.com:80/path"]
837
+
838
+ // ...After a request to example.com/pathA:
839
+ example.pendingMocks() // []
840
+
841
+ example.get("/pathB").optionally().reply(200);
842
+ example.pendingMocks() // []
843
+ ```
844
+
796
845
  ## Allow __unmocked__ requests on a mocked hostname
797
846
 
798
847
  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:
@@ -883,6 +932,22 @@ It is also available in the global scope:
883
932
  console.error('pending mocks: %j', nock.pendingMocks());
884
933
  ```
885
934
 
935
+ ## .activeMocks()
936
+
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.
938
+
939
+ You probably don't need to use this - it mainly exists as a mechanism to recreate the previous (now-changed) behavior of `pendingMocks()`.
940
+
941
+ ```js
942
+ console.error('active mocks: %j', scope.activeMocks());
943
+ ```
944
+
945
+ It is also available in the global scope:
946
+
947
+ ```js
948
+ console.error('active mocks: %j', nock.activeMocks());
949
+ ```
950
+
886
951
  # Logging
887
952
 
888
953
  Nock can log matches if you pass in a log function like this:
@@ -1151,7 +1216,7 @@ You can also listen for no match events like this:
1151
1216
 
1152
1217
  ```js
1153
1218
  nock.emitter.on('no match', function(req) {
1154
-
1219
+
1155
1220
  });
1156
1221
  ```
1157
1222
 
@@ -1256,7 +1321,7 @@ to set the mode call `nockBack.setMode(mode)` or run the tests with the `NOCK_BA
1256
1321
 
1257
1322
  Nock works by overriding Node's `http.request` function. Also, it overrides `http.ClientRequest` too to cover for modules that use it directly.
1258
1323
 
1259
- #Debugging
1324
+ # Debugging
1260
1325
  Nock uses debug, so just run with environmental variable DEBUG set to nock.*
1261
1326
 
1262
1327
  ```js
@@ -1279,10 +1344,6 @@ var scope = nock('http://api.myservice.com')
1279
1344
 
1280
1345
  # Generate Changelog
1281
1346
 
1282
- ```
1283
- $ npm install changelog -g
1284
- ```
1285
-
1286
1347
  ```
1287
1348
  $ npm run changelog
1288
1349
  ```
package/lib/common.js CHANGED
@@ -221,6 +221,20 @@ var headersFieldsArrayToLowerCase = function (headers) {
221
221
  }));
222
222
  };
223
223
 
224
+ var headersArrayToObject = function (rawHeaders) {
225
+ if(!_.isArray(rawHeaders)) {
226
+ return rawHeaders;
227
+ }
228
+
229
+ var headers = {};
230
+
231
+ for (var i=0, len=rawHeaders.length; i<len; i=i+2) {
232
+ headers[rawHeaders[i]] = rawHeaders[i+1];
233
+ }
234
+
235
+ return headers;
236
+ };
237
+
224
238
  /**
225
239
  * Deletes the given `fieldName` property from `headers` object by performing
226
240
  * case-insensitive search through keys.
@@ -326,10 +340,10 @@ exports.isContentEncoded = isContentEncoded;
326
340
  exports.isJSONContent = isJSONContent;
327
341
  exports.headersFieldNamesToLowerCase = headersFieldNamesToLowerCase;
328
342
  exports.headersFieldsArrayToLowerCase = headersFieldsArrayToLowerCase;
343
+ exports.headersArrayToObject = headersArrayToObject;
329
344
  exports.deleteHeadersField = deleteHeadersField;
330
345
  exports.percentEncode = percentEncode;
331
346
  exports.percentDecode = percentDecode;
332
347
  exports.matchStringOrRegexp = matchStringOrRegexp;
333
348
  exports.formatQueryValue = formatQueryValue;
334
349
  exports.isStream = isStream;
335
-
package/lib/intercept.js CHANGED
@@ -188,8 +188,10 @@ function removeInterceptor(options) {
188
188
  if (allInterceptors[baseUrl] && allInterceptors[baseUrl].scopes.length > 0) {
189
189
  if (key) {
190
190
  for (var i = 0; i < allInterceptors[baseUrl].scopes.length; i++) {
191
- if (allInterceptors[baseUrl].scopes[i]._key === key) {
191
+ var interceptor = allInterceptors[baseUrl].scopes[i];
192
+ if (interceptor._key === key) {
192
193
  allInterceptors[baseUrl].scopes.splice(i, 1);
194
+ interceptor.scope.remove(key, interceptor);
193
195
  break;
194
196
  }
195
197
  }
@@ -293,24 +295,34 @@ function isActive() {
293
295
 
294
296
  }
295
297
 
296
- function isDone() {
297
- return _.every(allInterceptors, function(interceptors) {
298
- return _.every(interceptors.scopes, function(interceptor) {
299
- return interceptor.__nock_scope.isDone();
300
- });
301
- });
302
- }
303
-
304
- function pendingMocks() {
298
+ function interceptorScopes() {
305
299
  return _.reduce(allInterceptors, function(result, interceptors) {
306
300
  for (var interceptor in interceptors.scopes) {
307
- result = result.concat(interceptors.scopes[interceptor].__nock_scope.pendingMocks());
301
+ result = result.concat(interceptors.scopes[interceptor].__nock_scope);
308
302
  }
309
303
 
310
304
  return result;
311
305
  }, []);
312
306
  }
313
307
 
308
+ function isDone() {
309
+ return _.every(interceptorScopes(), function(scope) {
310
+ return scope.isDone();
311
+ });
312
+ }
313
+
314
+ function pendingMocks() {
315
+ return _.flatten(_.map(interceptorScopes(), function(scope) {
316
+ return scope.pendingMocks();
317
+ }));
318
+ }
319
+
320
+ function activeMocks() {
321
+ return _.flatten(_.map(interceptorScopes(), function(scope) {
322
+ return scope.activeMocks();
323
+ }));
324
+ }
325
+
314
326
  function activate() {
315
327
 
316
328
  if(originalClientRequest) {
@@ -390,6 +402,7 @@ module.exports.activate = activate;
390
402
  module.exports.isActive = isActive;
391
403
  module.exports.isDone = isDone;
392
404
  module.exports.pendingMocks = pendingMocks;
405
+ module.exports.activeMocks = activeMocks;
393
406
  module.exports.enableNetConnect = enableNetConnect;
394
407
  module.exports.disableNetConnect = disableNetConnect;
395
408
  module.exports.overrideClientRequest = overrideClientRequest;
@@ -40,6 +40,13 @@ function Interceptor(scope, uri, method, requestBody, interceptorOptions) {
40
40
 
41
41
  this.delayInMs = 0;
42
42
  this.delayConnectionInMs = 0;
43
+
44
+ this.optional = false;
45
+ }
46
+
47
+ Interceptor.prototype.optionally = function optionally() {
48
+ this.optional = true;
49
+ return this;
43
50
  }
44
51
 
45
52
  Interceptor.prototype.replyWithError = function replyWithError(errorMessage) {
@@ -51,7 +58,7 @@ Interceptor.prototype.replyWithError = function replyWithError(errorMessage) {
51
58
  return this.scope;
52
59
  };
53
60
 
54
- Interceptor.prototype.reply = function reply(statusCode, body, headers) {
61
+ Interceptor.prototype.reply = function reply(statusCode, body, rawHeaders) {
55
62
  if (arguments.length <= 2 && _.isFunction(statusCode)) {
56
63
  body = statusCode;
57
64
  statusCode = 200;
@@ -59,11 +66,11 @@ Interceptor.prototype.reply = function reply(statusCode, body, headers) {
59
66
 
60
67
  this.statusCode = statusCode;
61
68
 
62
- // We use lower-case headers throughout Nock.
63
- headers = common.headersFieldNamesToLowerCase(headers);
64
-
65
69
  _.defaults(this.options, this.scope.scopeOptions);
66
70
 
71
+ // convert rawHeaders from Array to Object
72
+ var headers = common.headersArrayToObject(rawHeaders);
73
+
67
74
  if (this.scope._defaultReplyHeaders) {
68
75
  headers = headers || {};
69
76
  headers = mixin(this.scope._defaultReplyHeaders, headers);
@@ -75,23 +82,26 @@ Interceptor.prototype.reply = function reply(statusCode, body, headers) {
75
82
  }
76
83
 
77
84
  if (headers !== undefined) {
78
- this.headers = {};
79
85
  this.rawHeaders = [];
80
86
 
81
87
  // makes sure all keys in headers are in lower case
82
- for (var key2 in headers) {
83
- if (headers.hasOwnProperty(key2)) {
84
- this.headers[key2.toLowerCase()] = headers[key2];
85
- this.rawHeaders.push(key2);
86
- this.rawHeaders.push(headers[key2]);
88
+ for (var key in headers) {
89
+ if (headers.hasOwnProperty(key)) {
90
+ this.rawHeaders.push(key);
91
+ this.rawHeaders.push(headers[key]);
87
92
  }
88
93
  }
94
+
95
+ // We use lower-case headers throughout Nock.
96
+ this.headers = common.headersFieldNamesToLowerCase(headers);
97
+
98
+ debug('reply.headers:', this.headers);
89
99
  debug('reply.rawHeaders:', this.rawHeaders);
90
100
  }
91
101
 
92
102
  // If the content is not encoded we may need to transform the response body.
93
103
  // Otherwise we leave it as it is.
94
- if (!common.isContentEncoded(headers)) {
104
+ if (!common.isContentEncoded(this.headers)) {
95
105
  if (body && typeof(body) !== 'string' &&
96
106
  typeof(body) !== 'function' &&
97
107
  !Buffer.isBuffer(body) &&
@@ -129,17 +139,6 @@ Interceptor.prototype.replyWithFile = function replyWithFile(statusCode, filePat
129
139
  return this.reply(statusCode, readStream, headers);
130
140
  };
131
141
 
132
- Interceptor.prototype.replyWithFile = function replyWithFile(statusCode, filePath, headers) {
133
- if (! fs) {
134
- throw new Error('No fs');
135
- }
136
- var readStream = fs.createReadStream(filePath);
137
- readStream.pause();
138
- this.filePath = filePath;
139
- return this.reply(statusCode, readStream, headers);
140
- };
141
-
142
-
143
142
  // Also match request headers
144
143
  // https://github.com/pgte/nock/issues/163
145
144
  Interceptor.prototype.reqheaderMatches = function reqheaderMatches(options, key) {
package/lib/match_body.js CHANGED
@@ -57,7 +57,7 @@ function deepEqualExtended(spec, body) {
57
57
  if (spec && spec.constructor === RegExp) {
58
58
  return spec.test(body);
59
59
  }
60
- if (spec && spec.constructor === Object && body) {
60
+ if (spec && (spec.constructor === Object || spec.constructor === Array) && body) {
61
61
  var keys = Object.keys(spec);
62
62
  for (var i = 0; i < keys.length; i++) {
63
63
  if (!deepEqualExtended(spec[keys[i]], body[keys[i]])) {
package/lib/recorder.js CHANGED
@@ -94,8 +94,8 @@ function generateRequestAndResponseObject(req, bodyChunks, options, res, dataChu
94
94
  body: getBodyFromChunks(bodyChunks),
95
95
  status: res.statusCode,
96
96
  response: getBodyFromChunks(dataChunks, res.headers),
97
- headers: res.headers,
98
- reqheaders: req._headers
97
+ rawHeaders: res.rawHeaders || res.headers,
98
+ reqheaders: req._headers
99
99
  };
100
100
 
101
101
  }
@@ -153,7 +153,10 @@ function generateRequestAndResponse(req, bodyChunks, options, res, dataChunks) {
153
153
  ret.push(res.statusCode.toString());
154
154
  ret.push(', ');
155
155
  ret.push(JSON.stringify(responseBody));
156
- if (res.headers) {
156
+ if (res.rawHeaders) {
157
+ ret.push(', ');
158
+ ret.push(inspect(res.rawHeaders));
159
+ } else if (res.headers) {
157
160
  ret.push(', ');
158
161
  ret.push(inspect(res.headers));
159
162
  }