Skip to content

Conversation

@cristiklein
Copy link
Contributor

@tomas Unsure if this is the right fix, but npx mocha test/redirect_with_bad_redirector.js passes. It could be that some more stuff needs to be cleared before sending the second request. Perhaps done() needs something like a partial parameters, so it does most of the cleanup, except calling callback.

@tomas
Copy link
Owner

tomas commented Nov 19, 2025

Regardless of the test, does this fix handle the scenario described in the issue?

@cristiklein
Copy link
Contributor Author

Regardless of the test, does this fix handle the scenario described in the issue?

Absolutely. Here is how I verified:

With the fix:

cklein@minitrueee:~/elastisys/needle$ git log -1
commit a52baad790cf6174bc3aa0349425bd4fcb8677e6 (HEAD -> fix-redirect-with-head-with-body, origin/fix-redirect-with-head-with-body)
Author: Cristian Klein <cristian.klein@elastisys.com>
Date:   Wed Nov 19 18:36:22 2025 +0100

    Rename to unlisten_errors
cklein@minitrueee:~/elastisys/needle$ npx mocha test/redirect_with_bad_redirector.js 


  redirects with bad redirector
    ✓ calls back exactly once
    ✓ calls back exactly once with follow_keep_method


  2 passing (35ms)

Without the fix:

cklein@minitrueee:~/elastisys/needle$ git diff
diff --git a/lib/needle.js b/lib/needle.js
index 9a116a4..fd462d3 100644
--- a/lib/needle.js
+++ b/lib/needle.js
@@ -564,7 +564,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
 
         var redirect_url = utils.resolve_url(headers.location, uri);
         debug('Redirecting to ' +  redirect_url.toString());
-        unlisten_errors();
+        //unlisten_errors();
         return self.send_request(++count, method, redirect_url.toString(), config, post_data, out, callback);
       } else if (config.follow_max > 0) {
         return done(new Error('Max redirects reached. Possible loop in: ' + headers.location));
cklein@minitrueee:~/elastisys/needle$ npx mocha test/redirect_with_bad_redirector.js 


  redirects with bad redirector
    ✓ calls back exactly once
    1) calls back exactly once
    ✓ calls back exactly once with follow_keep_method
    2) calls back exactly once with follow_keep_method


  2 passing (41ms)
  2 failing

  1) redirects with bad redirector
       calls back exactly once:
     Error: done() called multiple times
      at /home/cklein/elastisys/needle/test/redirect_with_bad_redirector.js:49:7
      at done (lib/needle.js:479:14)
      at PassThrough.<anonymous> (lib/needle.js:740:9)
      at PassThrough.emit (node:events:531:35)
      at endReadableNT (node:internal/streams/readable:1698:12)
      at process.processTicksAndRejections (node:internal/process/task_queues:90:21)

  2) redirects with bad redirector
       calls back exactly once with follow_keep_method:
     Error: done() called multiple times
      at /home/cklein/elastisys/needle/test/redirect_with_bad_redirector.js:62:7
      at done (lib/needle.js:479:14)
      at PassThrough.<anonymous> (lib/needle.js:740:9)
      at PassThrough.emit (node:events:531:35)
      at endReadableNT (node:internal/streams/readable:1698:12)
      at process.processTicksAndRejections (node:internal/process/task_queues:90:21)

done() called multiple times means that needle called the callback twice, which in turn called mocha's done() twice.

Does this help confirm that the fix works as intended?

@tomas
Copy link
Owner

tomas commented Nov 19, 2025

Yup, perfect. I'll go ahead and merge. Thanks!

@tomas tomas merged commit f86dbd2 into tomas:master Nov 19, 2025
@cristiklein cristiklein deleted the fix-redirect-with-head-with-body branch December 16, 2025 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants