-
-
Notifications
You must be signed in to change notification settings - Fork 735
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
Buffer.from doesn't allow "null" chunk #1944
Comments
Are you calling If
So when Nock is in record mode, the error messaging is slightly different, however, erring in general is the correct action. |
In fact, I'm just using axios ... I Doesn't call req directly . And I run tests with jest (with nodejs http resquest), and I've two tests that produce this error, but Doesn't produce the error if run standalone .... |
Can you provide a simple runkit example that uses axios and nock to recreate the error? |
@mastermatt as I say, running my test alone doesn't produce the error, the error is only throw when I run all my tests ... ( and, it doesn't throw the error last time ) So, I'll need to do some search to find what is really the problem, and why I receive null here ... I will try some things to find the error when back at work (monday) |
Ok, so Some informations (I'm doing some search) . I update recordChunk function in Recorder.js
And, here is my trace
And in the trace, we see this :
And this line is : // Ends the current native request
RedirectableRequest.prototype.end = function (data, encoding, callback) {
// Shift parameters if necessary
if (typeof data === "function") {
callback = data;
data = encoding = null;
}
else if (typeof encoding === "function") {
callback = encoding;
encoding = null;
}
// Write data and end
var currentRequest = this._currentRequest;
console.log('follow-redirects/index.js');
console.log(data || "", (data || "").length);
this.write(data || "", encoding, function () {
currentRequest.end(null, null, callback); //119
});
}; So, it seems that |
Ok, so by doing some more search . I arrive at Recoder req.end ( ~ line 349 ), with
so, doesn't call recordChunk ( ~ line 360 ), because So, I arrive in intercepted_request_router.js req.end (~ line 142), with :
and call req.write at ~ line 154 . So finally I arrive at Recoder req.write ( ~ line 336 ), with
And so calling recordChunk with chunk null .... So, It seems, write is not called before end, but end call write ... But I'm not an expert of node HTTP client, I always use a library to do this ... I'll continue my research, just, is it normal that end call write with null ? ( end check if chunk is not null to call recordChunk, but pass null to write, and write doesn't check if recordChunk is null ) Edit : It seems, in most of the case, oldEnd is the function here https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js#L745 ,
from intercepted_request_router.js (~ line 77 ) Edit 2 : |
Hi! I appreciate your digging into this. I wanted to convey that in order to merge a patch for a problem like this, we will need a reproducible test example. That's for two reasons: (1) we need to include a test case that's invoked either via nock's public API or via an http request which covers the issue, and (2) we need to make sure we're maintaining parity with Node's behavior, not implementing workarounds for bugs in client libraries. |
yes yes I totally understand ... I'm just trying to search how this bug appear (and if it's really a bug) :/ ... So for the moment, I didn't have any idea how to produce a reproductible this bug each time ... But, if you have any idea, about things to check/search, I'm interested |
Hum, sorry, I'm always trying to find the error ... Is it possible that nock record and try to mock at the same time ? Just to be sure, to record, I use :
and to play I use :
( when doing some request, I use nock for some specific things sometimes, so I need to reactivate before ) |
Hello, I've add breakpoints on this lines :
and
so it breaks at line 318 And it breaks on line 324 : ( so you don't restore next time it breaks at line 318
This seems strange for me ... but maybe normal ? ( I can reproduce this strange process if you want ) |
Breakpoints pause execution at the beginning of a line, so the line has yet to execute. |
@mastermatt And below, debugger pass in So, originalClientRequest seems undefined Maybe more clear with debug logs ?
Has you can see, debug say :
( +635ms come from my breakpoint )
( +865ms is my breakpoint ) It seems, this strange behavior, come because I import nock two times, and one in
Maybe linked with the way jest manage setup ? and tests ? |
So, It seems that I find a solution to resolve this problem . Here is a comparaison of my fix : master...thib3113:master I'm not sure it's perfect, but it seems to work for my project . ( just, it seems your tests doesn't work on windows, so I'll need to build a linux Vm to start the tests ) Edit : So, no idea about how to write a test for it ( write a test, that start some jest tests ? ) ( I doesn't know if you need to support jest, or if jest need to support you, but in my opinion a mocking library need to test compatibility with test frameworks ? ) |
Ahh I think I missed where you mentioned you are running Jest. |
Hum, yes seems to be linked ... Ok, so if I understand correctly, jest reset import between each tests, but can't reset the "native imports" (like So, what can be the solution ? If I correctly understand, you can't really fix this ? (or checking if you already update the native library, and skip the update if already done) . So, the solutions I see, are :
|
Is the suggested method of calling |
Hum, I'm testing ... ( In the time I wrote my message, all tests seems to run correctly ) But, in fact, because I import nock in Maybe an update in the readme about this, can be interessting ? |
What is the expected behavior?
no error
What is the actual behavior?
In some case, an error is Throw :
Possible solution
here you check if
typeof chunk !== 'undefined'
, buttypeof null === 'object'
.So, the chunk pass in recordChunk, and recordChunk try to do
Buffer.from(chunk, [...])
, and chunk is null.How to reproduce the issue
I doesn't know how I produce it (for the moment), but It seems to be a little check
Having problem producing a test case? Try and ask the community for help. If the test case cannot be reproduced, the Nock community might not be able to help you.
Does the bug have a test case?
no
Versions
The text was updated successfully, but these errors were encountered: