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
reply with an asynchroneous response. #283
Conversation
yeah, we don't support an asynchronous handler. I would love a PR with that :). Basically nock would have to check function arguments length to see if the function would or not be asynchronous and provide a callback when it is. |
Yes it seems that mocha is doing a similar thing with the |
The code to add stand apparently there: https://github.com/pgte/nock/blob/master/lib/request_overrider.js#L264-L268 |
responseBody = new Buffer(responseBody); | ||
} else { | ||
responseBody = JSON.stringify(responseBody); | ||
function continueWithResponseBody(responseBody) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After this line I didn't touch the code just indent it because of the new function definition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that the callback should be using the standard callback pattern: error-first, so that it can be easily composed with other I/O.
This allows stuff like:
.reply(function(cb) {
fs.readFile(file, cb);
});
or even better:
.reply(fs.readFile.bind(fs, file));
Then the answer comes: what happens when there is an error happens? I propose that it's just thrown so that the test stops.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes good point, I was thinking about it and then I realize that there should not be errors at that point but you've made your point, I will change that.
Updated with @pgte comments. |
@Natim thanks! Landed on v1.2.0 |
Awesome, thanks @pgte. |
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’s related. Thank you! |
I am trying to do the following:
The aim is to basically use the local file storage api to mock the remote aws one.
But by the time I am reading the buffer, it is still empty.
The good call would be to be able to get a callback on the reply callback function in order to tell nock when it is finished.