{"action":"created","issue":{"url":"https://api.github.com/repos/pgte/nock/issues/278","labels_url":"https://api.github.com/repos/pgte/nock/issues/278/labels{/name}","comments_url":"https://api.github.com/repos/pgte/nock/issues/278/comments","events_url":"https://api.github.com/repos/pgte/nock/issues/278/events","html_url":"https://github.com/pgte/nock/issues/278","id":60215511,"number":278,"title":"back to nock, after nock.restore","user":{"login":"PavelPolyakov","id":839290,"avatar_url":"https://avatars.githubusercontent.com/u/839290?v=3","gravatar_id":"","url":"https://api.github.com/users/PavelPolyakov","html_url":"https://github.com/PavelPolyakov","followers_url":"https://api.github.com/users/PavelPolyakov/followers","following_url":"https://api.github.com/users/PavelPolyakov/following{/other_user}","gists_url":"https://api.github.com/users/PavelPolyakov/gists{/gist_id}","starred_url":"https://api.github.com/users/PavelPolyakov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PavelPolyakov/subscriptions","organizations_url":"https://api.github.com/users/PavelPolyakov/orgs","repos_url":"https://api.github.com/users/PavelPolyakov/repos","events_url":"https://api.github.com/users/PavelPolyakov/events{/privacy}","received_events_url":"https://api.github.com/users/PavelPolyakov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-03-07T17:24:30Z","updated_at":"2015-04-07T13:49:35Z","closed_at":null,"body":"Hi,\r\n\r\nAssuming I want to perform the next : \r\n1. Mock the request with nock, make a request and receive the mock results\r\n2. Unmock the request, do the real request and receive the real results\r\n3. Mock the server again, do the request and receive the mock results\r\n\r\nHow could this be done?\r\n\r\nI've experimented with:\r\n~~~javascript\r\nvar nock = require('nock');\r\nvar request = require('request');\r\nvar options = {\r\n    formData: {\r\n        authToken: 222,\r\n        data: 111\r\n    }\r\n};\r\nnock('http://pavelpolyakov.com/', {\r\n    allowUnmocked: true\r\n}).post('/post-proxy.php').reply(200, {\r\n    id: 'hello'\r\n});\r\nrequest.post('http://pavelpolyakov.com/post-proxy.php', options, function(err, response, body) {\r\n    console.log(\"1/\", body);\r\n    nock.restore();\r\n    request.post('http://pavelpolyakov.com/post-proxy.php', options, function(err, response, body) {\r\n        console.log(\"2/\", body);\r\n        nock('http://pavelpolyakov.com/', {\r\n            allowUnmocked: true\r\n        }).post('/post-proxy.php').reply(200, {\r\n            id: 'hello 2'\r\n        });\r\n        request.post('http://pavelpolyakov.com/post-proxy.php', options, function(err, response, body) {\r\n            console.log(\"3/\", body);\r\n        });\r\n    });\r\n});\r\n~~~\r\n\r\nBut the 3rd query goes to the real server as well. It seems that I can do the ```nock.restore()``` only once? How could I go back to the mocking after I did the ```nock.restore()``` ? Or how should I solve the situations like this (to be honest, it's just a question, I haven't met those situations in real life).\r\n\r\nIs there a way to turn the nock off and on in the realtime?\r\n\r\nRegards,"},"comment":{"url":"https://api.github.com/repos/pgte/nock/issues/comments/90559297","html_url":"https://github.com/pgte/nock/issues/278#issuecomment-90559297","issue_url":"https://api.github.com/repos/pgte/nock/issues/278","id":90559297,"user":{"login":"dominykas","id":505619,"avatar_url":"https://avatars.githubusercontent.com/u/505619?v=3","gravatar_id":"","url":"https://api.github.com/users/dominykas","html_url":"https://github.com/dominykas","followers_url":"https://api.github.com/users/dominykas/followers","following_url":"https://api.github.com/users/dominykas/following{/other_user}","gists_url":"https://api.github.com/users/dominykas/gists{/gist_id}","starred_url":"https://api.github.com/users/dominykas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dominykas/subscriptions","organizations_url":"https://api.github.com/users/dominykas/orgs","repos_url":"https://api.github.com/users/dominykas/repos","events_url":"https://api.github.com/users/dominykas/events{/privacy}","received_events_url":"https://api.github.com/users/dominykas/received_events","type":"User","site_admin":false},"created_at":"2015-04-07T13:49:35Z","updated_at":"2015-04-07T13:49:35Z","body":"What you're looking for is:\r\n```js\r\nif (!nock.isActive()) { nock.activate(); }\r\n```\r\n\r\nAs a side note, I must say, that my personal opinion is that hijacking everything during `require` is a horrible horrible pattern and should be fixed to explicitly require activation.\r\n"}}