Nock is excellent! Something that would make it even excellenter is if nock could be used to simulate connection level errors:
nock("http://api.example.com")
.get("/some/path")
.failWith(new Error("socket hang up"));
Especially since these can be otherwise cumbersome to test. Keep up the good work!
+1
This would be extremely useful, especially when developing/testing API clients.
+1
+1
+1
Would this also support mocking no-response? I have a use-case where I want to test what happens when the other end is simply not available. e.g. the same thing that would happen if you did curl http://asdlkjfldskjfdsal.com.
+1
+1
+1 to this
Big :thumbsup: I find that in most cases, whenever I want to unit-test what happens under certain responses, I also want to test what happens if the service is down.
A similar request was closed at #97, but I don't know if it's a project scope issue, or because it can't be done.
As a workaround, for the specific case of the service not responding, I currently use:
nock('http://service.com').get('/down').delayConnection(500).reply(200)
unirest.get('http://service.com/down').timeout(100).end(function(err) {
// err = ETIMEDOUT
});
+1
+1
@rprieto nice hack
+1
+1
:+1:
In the end i solved it with something like:
nock('http://www.google.com')
.get('/')
.replyWithError({code: 'ETIMEDOUT'})
Which looks clean, it does not involve delay and stuff, and as it seems to me it is enough to simulate ETIMEDOUT having in mind that im using request-promise lib where i'm checking for ETIMEDOUT like:
if (err.error && err.error.code === 'ETIMEDOUT')
Same can be applied for ENOTFOUND, ECONNRESET, ECONNREFUSED...
Hi Guys I had this same issue with superagent when trying to test timeout, took about a whole weekend to put it together that it is because superagent always retries requests by default, in fact deafult is 3 http://visionmedia.github.io/superagent/#retrying-requests , so on the second retry nock has no url to match thats why you get
{ Error: Nock: No match for request {
"method": "POST",
"url": "http://someurl.com/",
"body": "some_string"
}
at end (/Users/eyo.okon/Sites/node-graphql-client/node_modules/nock/lib/request_overrider.js:260:17)
at OverriddenClientRequest.RequestOverrider.req.end (/Users/eyo.okon/Sites/node-graphql-client/node_modules/nock/lib/request_overrider.js:160:7)
at Request._end (/Users/eyo.okon/Sites/node-graphql-client/node_modules/superagent/lib/node/index.js:961:9)
at RequestBase._retry (/Users/eyo.okon/Sites/node-graphql-client/node_modules/superagent/lib/request-base.js:176:15)
at Request.callback (/Users/eyo.okon/Sites/node-graphql-client/node_modules/superagent/lib/node/index.js:657:17)
at RequestBase._timeoutError (/Users/eyo.okon/Sites/node-graphql-client/node_modules/superagent/lib/request-base.js:573:8)
at Timeout.<anonymous> (/Users/eyo.okon/Sites/node-graphql-client/node_modules/superagent/lib/request-base.js:582:12)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5) at Timer.listOnTimeout (timers.js:207:5) status: 404, statusCode: 404, response: undefined, retries: 1 }
on setting retry to 0 one gets the correct error, and test passes... 馃暫馃従
Hopefully this helps someone
I had the same experience as what mazley93 had using node-request-retry.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it鈥檚 related. Thank you!
Most helpful comment
In the end i solved it with something like:
Which looks clean, it does not involve
delayand stuff, and as it seems to me it is enough to simulateETIMEDOUThaving in mind that im usingrequest-promiselib where i'm checking forETIMEDOUTlike:if (err.error && err.error.code === 'ETIMEDOUT')Same can be applied for
ENOTFOUND, ECONNRESET, ECONNREFUSED...