webmock icon indicating copy to clipboard operation
webmock copied to clipboard

Webmock/Net:HTTP (via faraday) opens a connection when disallowing connections.

Open james-lawrence opened this issue 9 years ago • 12 comments

      WebMock.disable_net_connect!(:allow_localhost => false)
      # ....
      stub_request(:get, "http://localhost:1234")
      # ....

will result in the following:

Faraday::ConnectionFailed:
Connection refused - connect(2) for "localhost" port 1234

it opens the connection even though it shouldn't be opening any outgoing connections (because we're mocking). is this intended behaviour?

james-lawrence avatar Jan 19 '16 15:01 james-lawrence

Can you please provide a failing code example?

bblimke avatar Jan 19 '16 15:01 bblimke

require 'spec_helper'
require 'webmock/rspec'
require 'pry'

describe Failure do
  before :all do
    WebMock.disable_net_connect!(:allow_localhost => false)
  end

  describe "failing example" do
    it "should not fail" do
      url = "http://localhost:9080"
      stub_request(:delete, url)

      conn = Faraday.new(:url => url) do |faraday|
        faraday.request  :json                    # json params
        faraday.response :logger                  # log requests to STDOUT
        faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
      end

      conn.delete do |req|
        req.body = {}
      end
    end
  end

james-lawrence avatar Jan 19 '16 15:01 james-lawrence

ok so is that related to delete request?

In your first example you used :get

bblimke avatar Jan 19 '16 15:01 bblimke

no. happens on every type I've tried. Get, Post, Delete.

require 'spec_helper'
require 'webmock/rspec'
require 'pry'

describe Failure do
  before :all do
    WebMock.disable_net_connect!(:allow_localhost => false)
  end

  describe "failing example" do
    let(:url) do
      "http://localhost:9080"
    end

    let(:conn) do
      conn = Faraday.new(:url => url) do |faraday|
        faraday.request  :json                    # json params
        faraday.response :logger                  # log requests to STDOUT
        faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
      end
    end

    it "should not fail on delete" do
      stub_request(:delete, url)

      conn.delete do |req|
        req.body = {}
      end
    end

    it "should not fail on post" do
      stub_request(:post, url)

      conn.post do |req|
        req.body = {}
      end
    end

    it "should not fail on get" do
      stub_request(:get, url)

      conn.get do |req|
        req.body = {}
      end
    end
  end
end

james-lawrence avatar Jan 19 '16 15:01 james-lawrence

It's probably because the connection is opened before any request is made.

WebMock can only intercept http requests by their pattern.

You open a new connection at conn = Faraday.new(:url => url), and at that point WebMock doesn't know what requests are going to follow.

bblimke avatar Jan 19 '16 21:01 bblimke

order of the Faraday.new didn't seem to matter, my actual code that exposed it calls invokes Faraday.new after the mocks were set up. But I'll double check tomorrow morning.

james-lawrence avatar Jan 19 '16 21:01 james-lawrence

sure, but the stubs are on http requests, not on https connections.

request is stubbed by url pattern. connection only uses host, protocol and port.

There can be multiple requests done using same connection. Some of them stubbed, some of them not.

I'm surprised it's happening because WebMock by default prevents Net::HTTP from doing any connections. It's possible that Faraday creates a new connection in a different way.

bblimke avatar Jan 19 '16 21:01 bblimke

possibly, I'll investigate how faraday makes the connection, and let you know.

james-lawrence avatar Jan 20 '16 00:01 james-lawrence

I just hit this problem to, because the Oauth2 client gem uses Faraday. Any updates?

trammel avatar Jul 19 '16 04:07 trammel

Another write in for hitting the problem, also with the Oauth2 gem.

jseifer avatar Oct 14 '16 16:10 jseifer

Can someone provide a failing code snippet to reproduce the issue?

bblimke avatar Oct 14 '16 20:10 bblimke

https://github.com/bblimke/webmock/issues/622 fixed it for me. Use WebMock.enable!.

jsteel avatar Jun 29 '18 14:06 jsteel