diff --git a/packages/https-proxy/lib/server.coffee b/packages/https-proxy/lib/server.coffee index a409774600..fe68de7265 100644 --- a/packages/https-proxy/lib/server.coffee +++ b/packages/https-proxy/lib/server.coffee @@ -139,6 +139,8 @@ class Server browserSocket.resume() + port or= "443" + if upstreamProxy = @_getProxyForUrl("https://#{hostname}:#{port}") # todo: as soon as all requests are intercepted, this can go away since this is just for pass-through debug("making proxied connection %o", { diff --git a/packages/https-proxy/test/unit/server_spec.coffee b/packages/https-proxy/test/unit/server_spec.coffee index acf7f51440..4cfcfa19be 100644 --- a/packages/https-proxy/test/unit/server_spec.coffee +++ b/packages/https-proxy/test/unit/server_spec.coffee @@ -43,33 +43,56 @@ describe "lib/server", -> socket.destroy = @sandbox.stub() head = {} - onError = (err, socket, head, port) -> + onError = (err, socket2, head2, port) -> expect(err.message).to.eq("connect ECONNREFUSED 127.0.0.1:8444") - expect(socket).to.eq(socket) - expect(head).to.eq(head) + expect(socket).to.eq(socket2) + expect(head).to.eq(head2) expect(port).to.eq("8444") expect(socket.destroy).to.be.calledOnce done() - @setup({onError: onError}) + @setup({ onError }) .then (srv) -> conn = srv._makeDirectConnection({url: "localhost:8444"}, socket, head) return + ## https://github.com/cypress-io/cypress/issues/3250 + it "does not crash when an erroneous URL is provided, just destroys socket", (done) -> + socket = new EE() + socket.destroy = @sandbox.stub() + head = {} + + onError = (err, socket2, head2, port) -> + expect(err.message).to.eq("connect ECONNREFUSED 127.0.0.1:443") + + expect(socket).to.eq(socket2) + expect(head).to.eq(head2) + expect(port).to.eq("443") + + expect(socket.destroy).to.be.calledOnce + + done() + + @setup({ onError }) + .then (srv) -> + conn = srv._makeDirectConnection({url: "%7Balgolia_application_id%7D-dsn.algolia.net:443"}, socket, head) + + return + it "with proxied connection calls options.onError with err and port and destroys the client socket", (done) -> socket = new EE() socket.destroy = @sandbox.stub() head = {} - onError = (err, socket, head, port) -> + onError = (err, socket2, head2, port) -> expect(err.message).to.eq("A connection to the upstream proxy could not be established: connect ECONNREFUSED 127.0.0.1:8444") - expect(socket).to.eq(socket) - expect(head).to.eq(head) + expect(socket).to.eq(socket2) + expect(head).to.eq(head2) expect(port).to.eq("11111") expect(socket.destroy).to.be.calledOnce @@ -79,7 +102,7 @@ describe "lib/server", -> process.env.HTTPS_PROXY = 'http://localhost:8444' process.env.NO_PROXY = '' - @setup({onError: onError}) + @setup({ onError }) .then (srv) -> conn = srv._makeDirectConnection({url: "should-not-reach.invalid:11111"}, socket, head)