supertest icon indicating copy to clipboard operation
supertest copied to clipboard

[fix] agent.query() is shadowed by HTTP method handler and therefore fails with "TypeError: Invalid URL"

Open sergburn opened this issue 5 months ago • 0 comments

Describe the bug

Node.js version: 20.19.2

OS version: Mac OS 15.4.1

Description: An attempt to call supertest.agent.query(<object>) fails with "TypeError: Invalid URL". This does not happen in Node v20.19.1, but does so in 20.19.2, apparently because of llhttp upgrade. The "query" is another HTTP method and methods.forEach() in agent.js overwrites the query() function in superagent. (However, it looks like the same mistake is made in superagent as well)

Verified that same happens in Node 22.15.1

Actual behavior

The call fails with TypeError.

node:internal/url:818
      href = bindingUrl.parse(input, base, true);
                        ^

TypeError: Invalid URL
    at new URL (node:internal/url:818:25)
    at TestAgent._attachCookies (.../node_modules/superagent/lib/node/agent.js:69:17)
    at TestAgent.<computed> [as query] (.../node_modules/supertest/lib/agent.js:77:10

Expected behavior

This call is expected to store query parameters map in Agent's "defaults" to be re-used with following post()/get()/put() etc calls.

Code to reproduce

const supertest = require('supertest');
const agent = supertest.agent('http://localhost:3000');
agent.query({ param: 'test' });

Checklist

  • [x] I have searched through GitHub issues for similar issues.
  • [x] I have completely read through the README and documentation.
  • [x] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

sergburn avatar May 19 '25 06:05 sergburn