Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: hashrocket/capybara-webmock
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: i4a/capybara-webmock
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Oct 26, 2020

  1. Support configuration of allowed hosts

    Fixes #35
    atd committed Oct 26, 2020
    Copy the full SHA
    0fc15dc View commit details
Showing with 19 additions and 4 deletions.
  1. +1 −1 README.md
  2. +9 −2 lib/capybara/webmock.rb
  3. +9 −1 lib/capybara/webmock/proxy.rb
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ Then in your RSpec configuration:
RSpec.configure do |config|
config.before(:each) do |example|
if example.metadata[:type] == :feature
Capybara::Webmock.start
Capybara::Webmock.start(allowed_hosts: 'test.localnet,external.service')
end
end

11 changes: 9 additions & 2 deletions lib/capybara/webmock.rb
Original file line number Diff line number Diff line change
@@ -12,12 +12,19 @@ module Webmock
class << self
attr_accessor :port_number, :pid_file, :kill_timeout, :start_timeout

def start
def start(options = {})
if @pid.nil?
kill_old_process
gem_path = File.dirname(__FILE__)
proxy_file = File.join(gem_path, 'webmock', 'config.ru')
stdin, stdout, wait_thr = Open3.popen2e({ "PROXY_PORT_NUMBER" => port_number.to_s }, "rackup", proxy_file)
stdin, stdout, wait_thr = Open3.popen2e(
{
"PROXY_PORT_NUMBER" => port_number.to_s,
"PROXY_ALLOWED_HOSTS" => options[:allowed_hosts]
},
"rackup",
proxy_file
)
stdin.close
@stdout = stdout
@pid = wait_thr[:pid]
10 changes: 9 additions & 1 deletion lib/capybara/webmock/proxy.rb
Original file line number Diff line number Diff line change
@@ -4,6 +4,14 @@
class Capybara::Webmock::Proxy < Rack::Proxy
ALLOWED_HOSTS = allowed_hosts = ['127.0.0.1', 'localhost', /(.*\.|\A)lvh.me/]

def initialize(*args, &block)
hosts = ENV['PROXY_ALLOWED_HOSTS']

@allowed_hosts = (hosts ? hosts.split(',') : []) | ALLOWED_HOSTS

super
end

def call(env)
@streaming = true
super
@@ -28,7 +36,7 @@ def perform_request(env)
private

def allowed_host?(host)
ALLOWED_HOSTS.any? do |allowed_host|
@allowed_hosts.any? do |allowed_host|
case allowed_host
when Regexp
allowed_host =~ host