"Protocol "http:" not supported. Expected "https:"" when using superagent
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top 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 >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
This should get published in the next version (#660).
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 aprotocol
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 thehttps.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 thehttp
orhttps
module), it falls back to creating a URL beginning withhttp
which then causes an error to be thrown in themsw
library.One possible solution that comes to my mind would be to propagate the
protocol
variable all the way from thehandleRequestFunction
(insrc/interceptors/ClientRequest/index.ts
) to thenormalizeHttpRequestParams
function and use theprotocol
variable to put together the correct URL (if there is noprotocol
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 🙂