`TypeError [ERR_INVALID_URL]: Invalid URL: [object Object]` for requests not made with an `URL` object
See original GitHub issueEnvironment
Name | Version |
---|---|
msw | 0.28.1 |
node | 15.14.0 |
OS | Debian |
Request handlers
import { setupServer } from 'msw/node'
import { rest } from 'msw'
const server = setupServer(
rest.get('https://github.com', (req, res, ctx) => {
return res(ctx.status(200));
})
)
server.listen()
Actual request
import Wreck from '@hapi/wreck';
const request = async () => {
return await Wreck.get('https://github.com');
}
Current behavior
Requests made in application code that don’t use the URL
object can’t be mocked, and cause an Invalid URL
error:
TypeError [ERR_INVALID_URL]: Invalid URL: [object Object]
at new NodeError (node:internal/errors:329:5)
at onParseError (node:internal/url:537:9)
at new URL (node:internal/url:613:5)
at CookieStore.get (/srv/node_modules/msw/node/lib/index.js:7404:28)
at setRequestCookies (/srv/node_modules/msw/node/lib/index.js:7550:109)
at /srv/node_modules/msw/node/lib/index.js:7609:21
at Generator.next (<anonymous>)
at /srv/node_modules/msw/node/lib/index.js:53:71
at new Promise (<anonymous>)
at __awaiter (/srv/node_modules/msw/node/lib/index.js:49:12) {
input: '[object Object]',
code: 'ERR_INVALID_URL',
trace: [ { method: 'GET', url: 'https://github.com' } ]
}
Expected behavior
Requests somewhat comforming to the structure of an URL
object can be mocked as well.
Details
In my example, I’m using the Wreck
module provided by Hapi to make HTTP requests. This causes the follow chain of events:
- Wreck uses a simple object to construct the
url
of therequest
(instead of the nativeURL
) - since msw calls
.toString
when setting request cookies and reading response cookies, this causes the above mentioned error
This could be fixed by using request.url.href
instead of request.url.toString()
, which also seems to be the behaviour of calling toString
on a standard URL
object.
I’ll be happy to provide a PR for this, but there might be a better way to support this (or it might not be desired to do so).
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
javascript - Calling Endpoint from script fails with TypeError ...
Calling Endpoint from script fails with TypeError [ERR_INVALID_URL]: Invalid URL ... at eval (/src/routes/index.svelte:37:10) at Object.
Read more >Node.js Client TypeError [ERR_INVALID_URL]: Invalid URL
Hello, I am trying to deploy a 1Password Connect Server via Docker as per your instructions but, I am getting the following error...
Read more >URL | Node.js v19.3.0 Documentation
When parsed, a URL object is returned containing properties for each of these ... A TypeError will be thrown if the input or...
Read more >Invalid URL Path Error - OutSystems 11 Documentation
The Invalid URL Path error is issued in the following situations: Message: 'URL Path' property must start with '/'. Cause: The specified URL...
Read more >Nodejs - Getting error messages on the last part of the lesson
internal/url.js:279 throw new ERR_INVALID_URL(input); ^ TypeError ... it req.url to then destrucure the newly created object and get the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No worries! We can bump the “interceptors” version in a new pull request here and add it to the next release.
I think what happens is that this condition is
true
:https://github.com/mswjs/interceptors/blob/16fd35622efe6328497ade15e21a84f6afda0a93/src/interceptors/ClientRequest/utils/normalizeHttpRequestParams.ts#L70
also supported by debug logs:
So the interceptor assumes if
origin
is present, it’s aURL
, and doesn’t process it further?