1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | const { addInterceptor, isOn } = require('./intercept')
|
7 | const common = require('./common')
|
8 | const assert = require('assert')
|
9 | const url = require('url')
|
10 | const debug = require('debug')('nock.scope')
|
11 | const { EventEmitter } = require('events')
|
12 | const util = require('util')
|
13 | const Interceptor = require('./interceptor')
|
14 |
|
15 | let fs
|
16 |
|
17 | try {
|
18 | fs = require('fs')
|
19 | } catch (err) {
|
20 |
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | class Scope extends EventEmitter {
|
35 | constructor(basePath, options) {
|
36 | super()
|
37 |
|
38 | this.keyedInterceptors = {}
|
39 | this.interceptors = []
|
40 | this.transformPathFunction = null
|
41 | this.transformRequestBodyFunction = null
|
42 | this.matchHeaders = []
|
43 | this.logger = debug
|
44 | this.scopeOptions = options || {}
|
45 | this.urlParts = {}
|
46 | this._persist = false
|
47 | this.contentLen = false
|
48 | this.date = null
|
49 | this.basePath = basePath
|
50 | this.basePathname = ''
|
51 | this.port = null
|
52 | this._defaultReplyHeaders = []
|
53 |
|
54 | if (!(basePath instanceof RegExp)) {
|
55 | this.urlParts = url.parse(basePath)
|
56 | this.port =
|
57 | this.urlParts.port || (this.urlParts.protocol === 'http:' ? 80 : 443)
|
58 | this.basePathname = this.urlParts.pathname.replace(/\/$/, '')
|
59 | this.basePath = `${this.urlParts.protocol}//${this.urlParts.hostname}:${this.port}`
|
60 | }
|
61 | }
|
62 |
|
63 | add(key, interceptor) {
|
64 | if (!(key in this.keyedInterceptors)) {
|
65 | this.keyedInterceptors[key] = []
|
66 | }
|
67 | this.keyedInterceptors[key].push(interceptor)
|
68 | addInterceptor(
|
69 | this.basePath,
|
70 | interceptor,
|
71 | this,
|
72 | this.scopeOptions,
|
73 | this.urlParts.hostname
|
74 | )
|
75 | }
|
76 |
|
77 | remove(key, interceptor) {
|
78 | if (this._persist) {
|
79 | return
|
80 | }
|
81 | const arr = this.keyedInterceptors[key]
|
82 | if (arr) {
|
83 | arr.splice(arr.indexOf(interceptor), 1)
|
84 | if (arr.length === 0) {
|
85 | delete this.keyedInterceptors[key]
|
86 | }
|
87 | }
|
88 | }
|
89 |
|
90 | intercept(uri, method, requestBody, interceptorOptions) {
|
91 | const ic = new Interceptor(
|
92 | this,
|
93 | uri,
|
94 | method,
|
95 | requestBody,
|
96 | interceptorOptions
|
97 | )
|
98 |
|
99 | this.interceptors.push(ic)
|
100 | return ic
|
101 | }
|
102 |
|
103 | get(uri, requestBody, options) {
|
104 | return this.intercept(uri, 'GET', requestBody, options)
|
105 | }
|
106 |
|
107 | post(uri, requestBody, options) {
|
108 | return this.intercept(uri, 'POST', requestBody, options)
|
109 | }
|
110 |
|
111 | put(uri, requestBody, options) {
|
112 | return this.intercept(uri, 'PUT', requestBody, options)
|
113 | }
|
114 |
|
115 | head(uri, requestBody, options) {
|
116 | return this.intercept(uri, 'HEAD', requestBody, options)
|
117 | }
|
118 |
|
119 | patch(uri, requestBody, options) {
|
120 | return this.intercept(uri, 'PATCH', requestBody, options)
|
121 | }
|
122 |
|
123 | merge(uri, requestBody, options) {
|
124 | return this.intercept(uri, 'MERGE', requestBody, options)
|
125 | }
|
126 |
|
127 | delete(uri, requestBody, options) {
|
128 | return this.intercept(uri, 'DELETE', requestBody, options)
|
129 | }
|
130 |
|
131 | options(uri, requestBody, options) {
|
132 | return this.intercept(uri, 'OPTIONS', requestBody, options)
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | pendingMocks() {
|
139 | return this.activeMocks().filter(key =>
|
140 | this.keyedInterceptors[key].some(({ interceptionCounter, optional }) => {
|
141 | const persistedAndUsed = this._persist && interceptionCounter > 0
|
142 | return !persistedAndUsed && !optional
|
143 | })
|
144 | )
|
145 | }
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | activeMocks() {
|
151 | return Object.keys(this.keyedInterceptors)
|
152 | }
|
153 |
|
154 | isDone() {
|
155 | if (!isOn()) {
|
156 | return true
|
157 | }
|
158 |
|
159 | return this.pendingMocks().length === 0
|
160 | }
|
161 |
|
162 | done() {
|
163 | assert.ok(
|
164 | this.isDone(),
|
165 | `Mocks not yet satisfied:\n${this.pendingMocks().join('\n')}`
|
166 | )
|
167 | }
|
168 |
|
169 | buildFilter() {
|
170 | const filteringArguments = arguments
|
171 |
|
172 | if (arguments[0] instanceof RegExp) {
|
173 | return function(candidate) {
|
174 |
|
175 | if (typeof candidate !== 'string') {
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | throw Error(
|
181 | `Nock internal assertion failed: typeof candidate is ${typeof candidate}. If you encounter this error, please report it as a bug.`
|
182 | )
|
183 | }
|
184 | return candidate.replace(filteringArguments[0], filteringArguments[1])
|
185 | }
|
186 | } else if (typeof arguments[0] === 'function') {
|
187 | return arguments[0]
|
188 | }
|
189 | }
|
190 |
|
191 | filteringPath() {
|
192 | this.transformPathFunction = this.buildFilter.apply(this, arguments)
|
193 | if (!this.transformPathFunction) {
|
194 | throw new Error(
|
195 | 'Invalid arguments: filtering path should be a function or a regular expression'
|
196 | )
|
197 | }
|
198 | return this
|
199 | }
|
200 |
|
201 | filteringRequestBody() {
|
202 | this.transformRequestBodyFunction = this.buildFilter.apply(this, arguments)
|
203 | if (!this.transformRequestBodyFunction) {
|
204 | throw new Error(
|
205 | 'Invalid arguments: filtering request body should be a function or a regular expression'
|
206 | )
|
207 | }
|
208 | return this
|
209 | }
|
210 |
|
211 | matchHeader(name, value) {
|
212 |
|
213 | this.matchHeaders.push({ name: name.toLowerCase(), value })
|
214 | return this
|
215 | }
|
216 |
|
217 | defaultReplyHeaders(headers) {
|
218 | this._defaultReplyHeaders = common.headersInputToRawArray(headers)
|
219 | return this
|
220 | }
|
221 |
|
222 | log(newLogger) {
|
223 | this.logger = newLogger
|
224 | return this
|
225 | }
|
226 |
|
227 | persist(flag = true) {
|
228 | if (typeof flag !== 'boolean') {
|
229 | throw new Error('Invalid arguments: argument should be a boolean')
|
230 | }
|
231 | this._persist = flag
|
232 | return this
|
233 | }
|
234 |
|
235 | |
236 |
|
237 |
|
238 |
|
239 | shouldPersist() {
|
240 | return this._persist
|
241 | }
|
242 |
|
243 | replyContentLength() {
|
244 | this.contentLen = true
|
245 | return this
|
246 | }
|
247 |
|
248 | replyDate(d) {
|
249 | this.date = d || new Date()
|
250 | return this
|
251 | }
|
252 | }
|
253 |
|
254 | function loadDefs(path) {
|
255 | if (!fs) {
|
256 | throw new Error('No fs')
|
257 | }
|
258 |
|
259 | const contents = fs.readFileSync(path)
|
260 | return JSON.parse(contents)
|
261 | }
|
262 |
|
263 | function load(path) {
|
264 | return define(loadDefs(path))
|
265 | }
|
266 |
|
267 | function getStatusFromDefinition(nockDef) {
|
268 |
|
269 | if (nockDef.reply !== undefined) {
|
270 | const parsedReply = parseInt(nockDef.reply, 10)
|
271 | if (isNaN(parsedReply)) {
|
272 | throw Error('`reply`, when present, must be a numeric string')
|
273 | }
|
274 |
|
275 | return parsedReply
|
276 | }
|
277 |
|
278 | const DEFAULT_STATUS_OK = 200
|
279 | return nockDef.status || DEFAULT_STATUS_OK
|
280 | }
|
281 |
|
282 | function getScopeFromDefinition(nockDef) {
|
283 |
|
284 | if (nockDef.port !== undefined) {
|
285 |
|
286 | const options = url.parse(nockDef.scope)
|
287 | if (options.port === null) {
|
288 | return `${nockDef.scope}:${nockDef.port}`
|
289 | } else {
|
290 | if (parseInt(options.port) !== parseInt(nockDef.port)) {
|
291 | throw new Error(
|
292 | 'Mismatched port numbers in scope and port properties of nock definition.'
|
293 | )
|
294 | }
|
295 | }
|
296 | }
|
297 |
|
298 | return nockDef.scope
|
299 | }
|
300 |
|
301 | function tryJsonParse(string) {
|
302 | try {
|
303 | return JSON.parse(string)
|
304 | } catch (err) {
|
305 | return string
|
306 | }
|
307 | }
|
308 |
|
309 |
|
310 | const emitAsteriskDeprecation = util.deprecate(
|
311 | () => {},
|
312 | 'Skipping body matching using "*" is deprecated. Set the definition body to undefined instead.',
|
313 | 'NOCK1579'
|
314 | )
|
315 |
|
316 | function define(nockDefs) {
|
317 | const scopes = []
|
318 |
|
319 | nockDefs.forEach(function(nockDef) {
|
320 | const nscope = getScopeFromDefinition(nockDef)
|
321 | const npath = nockDef.path
|
322 | if (!nockDef.method) {
|
323 | throw Error('Method is required')
|
324 | }
|
325 | const method = nockDef.method.toLowerCase()
|
326 | const status = getStatusFromDefinition(nockDef)
|
327 | const rawHeaders = nockDef.rawHeaders || []
|
328 | const reqheaders = nockDef.reqheaders || {}
|
329 | const badheaders = nockDef.badheaders || []
|
330 | const options = { ...nockDef.options }
|
331 |
|
332 |
|
333 |
|
334 |
|
335 | options.reqheaders = reqheaders
|
336 | options.badheaders = badheaders
|
337 |
|
338 | let { body } = nockDef
|
339 |
|
340 | if (body === '*') {
|
341 |
|
342 |
|
343 |
|
344 |
|
345 | emitAsteriskDeprecation()
|
346 | body = undefined
|
347 | }
|
348 |
|
349 |
|
350 |
|
351 | let response
|
352 | if (!nockDef.response) {
|
353 | response = ''
|
354 |
|
355 | } else if (nockDef.responseIsBinary) {
|
356 | response = Buffer.from(nockDef.response, 'hex')
|
357 | } else {
|
358 | response =
|
359 | typeof nockDef.response === 'string'
|
360 | ? tryJsonParse(nockDef.response)
|
361 | : nockDef.response
|
362 | }
|
363 |
|
364 | const scope = new Scope(nscope, options)
|
365 |
|
366 |
|
367 | Object.entries(reqheaders).forEach(([fieldName, value]) => {
|
368 | scope.matchHeader(fieldName, value)
|
369 | })
|
370 |
|
371 | const acceptableFilters = ['filteringRequestBody', 'filteringPath']
|
372 | acceptableFilters.forEach(filter => {
|
373 | if (nockDef[filter]) {
|
374 | scope[filter](nockDef[filter])
|
375 | }
|
376 | })
|
377 |
|
378 | scope.intercept(npath, method, body).reply(status, response, rawHeaders)
|
379 |
|
380 | scopes.push(scope)
|
381 | })
|
382 |
|
383 | return scopes
|
384 | }
|
385 |
|
386 | module.exports = {
|
387 | Scope,
|
388 | load,
|
389 | loadDefs,
|
390 | define,
|
391 | }
|