| 10 | const matchBody = require('./match_body') |
| 11 | |
| 12 | module.exports = class Interceptor { |
| 13 | /** |
| 14 | * |
| 15 | * Valid argument types for `uri`: |
| 16 | * - A string used for strict comparisons with pathname. |
| 17 | * The search portion of the URI may also be postfixed, in which case the search params |
| 18 | * are striped and added via the `query` method. |
| 19 | * - A RegExp instance that tests against only the pathname of requests. |
| 20 | * - A synchronous function bound to this Interceptor instance. It's provided the pathname |
| 21 | * of requests and must return a boolean denoting if the request is considered a match. |
| 22 | */ |
| 23 | constructor(scope, uri, method, requestBody, interceptorOptions) { |
| 24 | const uriIsStr = typeof uri === 'string' |
| 25 | // Check for leading slash. Uri can be either a string or a regexp, but |
| 26 | // When enabled filteringScope ignores the passed URL entirely so we skip validation. |
| 27 | |
| 28 | if ( |
| 29 | uriIsStr && |
| 30 | !scope.scopeOptions.filteringScope && |
| 31 | !scope.basePathname && |
| 32 | !uri.startsWith('/') && |
| 33 | !uri.startsWith('*') |
| 34 | ) { |
| 35 | throw Error( |
| 36 | `Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything) (got: ${uri})`, |
| 37 | ) |
| 38 | } |
| 39 | |
| 40 | if (!method) { |
| 41 | throw new Error( |
| 42 | 'The "method" parameter is required for an intercept call.', |
| 43 | ) |
| 44 | } |
| 45 | |
| 46 | this.scope = scope |
| 47 | this.interceptorMatchHeaders = [] |
| 48 | this.method = method.toUpperCase() |
| 49 | this.uri = uri |
| 50 | this._key = `${this.method} ${scope.basePath}${scope.basePathname}${ |
| 51 | uriIsStr ? '' : '/' |
| 52 | }${uri}` |
| 53 | this.basePath = this.scope.basePath |
| 54 | this.path = uriIsStr ? scope.basePathname + uri : uri |
| 55 | this.queries = null |
| 56 | |
| 57 | this.options = interceptorOptions || {} |
| 58 | this.counter = 1 |
| 59 | this._requestBody = requestBody |
| 60 | |
| 61 | // We use lower-case header field names throughout Nock. |
| 62 | this.reqheaders = common.headersFieldNamesToLowerCase( |
| 63 | scope.scopeOptions.reqheaders || {}, |
| 64 | true, |
| 65 | ) |
| 66 | this.badheaders = common.headersFieldsArrayToLowerCase( |
| 67 | scope.scopeOptions.badheaders || [], |
| 68 | ) |
| 69 |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…