I don't understand how to setup webmock (or any stubbing library) to stub only specific requests to a particular url (www.example.com).
I am doing Cucumber tests on a browser and I want to allow connection to any site except for those requests I want to stub.
For my particular case, I want to stub the access to www.example.com/article/:article_id
and deliver a HTML file of the page I had previously downloaded.
Following this link and other questions
# Gemfile
gem 'webmock'
# features/support/webmock.rb
require 'webmock/cucumber'
My Cucumber env file
# features/env.rb
# Allow all server connections by default
WebMock.allow_net_connect!
Before('@stub-example.com') do
stub_request(:get, 'https://www.example.com').to_rack(FakeExampleDotCom)
end
I am currently stuck at this stage, because the Stub isn't working and my test tries to connect to the real website. I suppose WebMock.allow_net_connect!
disables any stub. I cannot just disable_net_connect!
since I want to authorize every website (and not just localhost) and only "blacklist" www.example.com
and stub it. How can I do that ?
FYI : My Sinatra app that serves the HTML file
class FakeExampleDotCom < Sinatra::Base
get 'article/:article_id' do
html_response 200, "#{params[:article_id]}_article.html"
end
private
# Returns the HTML file corresponding to the article
def html_response(response_code, file_name)
content_type :html
status response_code
File.open(Rails.root.join('features', 'assets', file_name)).read
end
end