question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`TypeError [ERR_INVALID_URL]: Invalid URL: [object Object]` for requests not made with an `URL` object

See original GitHub issue

Environment

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:

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:closed
  • Created 2 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
kettanaitocommented, Apr 22, 2021

No worries! We can bump the “interceptors” version in a new pull request here and add it to the next release.

1reaction
lauraseidlercommented, Apr 21, 2021

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:

http normalizeHttpRequestParams given a URL: {
  setHost: false,
  host: 'github.com',
  origin: 'https://github.com',
  searchParams: URLSearchParams {},
  protocol: 'https:',
  hostname: 'github.com',
  hash: '',
  search: '',
  pathname: '/',
  path: '/',
  href: 'https://github.com/',
  method: 'GET',
  headers: { host: 'github.com' },
  agent: Agent {
    _events: [Object: null prototype] {
      free: [Function (anonymous)],
      newListener: [Function: maybeEnableKeylog]
    },
    _eventsCount: 2,
    _maxListeners: undefined,
    defaultPort: 443,
    protocol: 'https:',
    options: { maxSockets: Infinity, path: null },
    requests: {},
    sockets: {},
    freeSockets: {},
    keepAliveMsecs: 1000,
    keepAlive: false,
    maxSockets: Infinity,
    maxFreeSockets: 256,
    scheduling: 'lifo',
    maxTotalSockets: Infinity,
    totalSocketCount: 0,
    maxCachedSessions: 100,
    _sessionCache: { map: {}, list: [] },
    [Symbol(kCapture)]: false
  }
} +0ms

So the interceptor assumes if origin is present, it’s a URL, and doesn’t process it further?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found