Webmock/Net:HTTP (via faraday) opens a connection when disallowing connections.
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?
Can you please provide a failing code example?
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
ok so is that related to delete request?
In your first example you used :get
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
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.
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.
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.
possibly, I'll investigate how faraday makes the connection, and let you know.
I just hit this problem to, because the Oauth2 client gem uses Faraday. Any updates?
Another write in for hitting the problem, also with the Oauth2 gem.
Can someone provide a failing code snippet to reproduce the issue?
https://github.com/bblimke/webmock/issues/622 fixed it for me. Use WebMock.enable!.