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.

"Protocol "http:" not supported. Expected "https:"" when using superagent

See original GitHub issue

Describe the bug

When I try to use msw alongside superagent in Node, it throws an error:

UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_PROTOCOL]: Protocol "http:" not supported. Expected "https:"

(I’m aware of issues such as https://github.com/mswjs/msw/issues/209 but this one seems to be different)

Environment

  • msw: 0.27.0
  • nodejs: 12.14.0
  • npm: 0.0.0

Please also provide your browser version. n/a

To Reproduce

Clone the repository on https://github.com/tomdohnal/msw-superagent-repro or run this piece of code using node

const { rest } = require("msw");
const { setupServer } = require("msw/node");
const request = require("superagent");
const fetch = require("node-fetch");

setupServer(
  rest.get("https://google.com", (req, res, ctx) => {
    return res(ctx.status(200), ctx.text("Hi from google"));
  })
).listen();

request.get("https://google.com").end(); // -> throws an error

// fetch("https://google.com")
//   .then((res) => res.text())
//   .then(console.log); -> works just fine

Expected behavior

No error should be thrown and the call should be mocked

Screenshots

n/a

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
kettanaitocommented, Mar 23, 2021

This should get published in the next version (#660).

2reactions
tomdohnalcommented, Mar 8, 2021

OK so I did some debugging and found out that the problem is caused by the fact that when superagent (the HTTP(S) client) makes an HTTPS request, it doesn’t pass a protocol property to the request options.

As per https://nodejs.org/api/https.html#https_https_request_options_callback the default value of the protocol option passed to the https.request call is (not surprisingly 😄) https so there is no need to be explicit about it.

However, as the normalizeHttpRequestParams only accept the request options object as an argument (and doesn’t accept the info about if the request is being made using the http or https module), it falls back to creating a URL beginning with http which then causes an error to be thrown in the msw library.

One possible solution that comes to my mind would be to propagate the protocol variable all the way from the handleRequestFunction (in src/interceptors/ClientRequest/index.ts) to the normalizeHttpRequestParams function and use the protocol variable to put together the correct URL (if there is no protocol variable on the request options object).

I’m happy to send a PR with the fix but first I wanted to discuss how to approach this 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Bug] Protocol "http:" not supported. Expected "https:" #691
I am using superagent on node.js to make external api calls. I use an https agent on my requests like this: const options...
Read more >
Node js Error: Protocol "https:" not supported. Expected "http:"
My project needs to request a JSON from an API, so I can use the data it provides. I use the http get...
Read more >
A Complete Guide to Making HTTP Requests in Node.js
This article will discuss more on HTTP, HTTP requests, ways to make this requests in Node and how we can handle errors.
Read more >
Protocol "http:" not supported. Expected "https:"
Hi, I'm developing a Next.js application with the next-auth library to integrate Auth0 as the provider. In my application when I try to...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun.
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