Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple interceptor enable option #2256

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Expand Up @@ -157,6 +157,8 @@ This means that you can intercept 2 or more calls to the same URL and return dif
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.
If you don’t want interceptors to be removed as they are used, you can use the [.persist()](#persist) method.

> You can always disable an interceptor by setting `Interceptor.enabled` to false

### Specifying hostname

The request hostname can be a string or a RegExp.
Expand Down Expand Up @@ -1064,7 +1066,7 @@ Only for cases where nock has been deactivated using [nock.restore()](#restoring
nock.activate()
```

**note**: To check if nock HTTP interceptor is active or inactive, use [nock.isActive()](#isactive).
**note**: To check if nock HTTP interceptor is active or inactive, use [nock.isActive()](#isactive). This will not check for the property `.enabled`

## Turning Nock Off (experimental!)

Expand Down Expand Up @@ -1300,6 +1302,8 @@ nock.recorder.rec({

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.

> If you expect to use the interceptor later, you can set the `Interceptor.enabled` property to `false` to temporarily disable the interceptor

Examples:

```js
Expand Down Expand Up @@ -1335,6 +1339,11 @@ interceptor.reply(200, 'OK')
nock.removeInterceptor(interceptor)
```

```js
const interceptor = nock('http://example.org').get('somePath')
interceptor.enabled = false
```

## Events

A scope emits the following events:
Expand Down
4 changes: 2 additions & 2 deletions lib/intercepted_request_router.js
Expand Up @@ -294,8 +294,8 @@ class InterceptedRequestRouter {
requestBodyIsUtf8Representable ? 'utf8' : 'hex'
)

const matchedInterceptor = interceptors.find(i =>
i.match(req, options, requestBodyString)
const matchedInterceptor = interceptors.find(
i => i.match(req, options, requestBodyString) && i.enabled
)

if (matchedInterceptor) {
Expand Down
1 change: 1 addition & 0 deletions lib/interceptor.js
Expand Up @@ -53,6 +53,7 @@ module.exports = class Interceptor {
this.interceptorMatchHeaders = []
this.method = method.toUpperCase()
this.uri = uri
this.enabled = true
this._key = `${this.method} ${scope.basePath}${scope.basePathname}${
uriIsStr ? '' : '/'
}${uri}`
Expand Down
44 changes: 44 additions & 0 deletions tests/test_intercept_toggle.js
@@ -0,0 +1,44 @@
'use strict'

const { expect } = require('chai')
const nock = require('..')

const got = require('./got_client')
const servers = require('./servers')

// Tests for unnocking via "enabled" property

describe('toggling interceptors', () => {
it('interceptor is enabled', async () => {
const { origin } = await servers.startHttpServer((req, res) => {
res.json(JSON.stringify({ status: false }))
})

const interceptor = nock(`${origin}/`)

interceptor.get('/').reply(200, { status: true })

let { body } = await got(`${origin}/`)

body = JSON.parse(body)

expect(body.status).to.equal(true)
})

it('interceptor is disabled', async () => {
const { origin } = await servers.startHttpServer((req, res) => {
res.writeHead(200)
res.end(JSON.stringify({ status: false }))
})

const interceptor = nock(`${origin}/`)

interceptor.enabled = false

let { body } = await got(`${origin}/`)

body = JSON.parse(body)

expect(body.status).to.equal(false)
})
})