Sha256: 251b3c1ad30bb470ffe9acbc9b111f2955c861a87bafa6cd0c4895db4021a212

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

require 'rack/proxy'
require 'capybara/webmock'

class Capybara::Webmock::Proxy < Rack::Proxy
  PID_FILE = File.join('tmp', 'pids', 'capybara_webmock_proxy.pid')

  def initialize(pid)
    write_pid(pid)
    ensure_log_exists
  end

  def call(env)
    @streaming = true
    super
  end

  def perform_request(env)
    request = Rack::Request.new(env)
    allowed_urls = ['127.0.0.1', 'localhost', %r{(.*\.|\A)lvh.me}]

    if allowed_url?(allowed_urls, request.host)
      super(env)
    else
      ['200', {'Content-Type' => 'text/html'}, ['']]
    end
  end

  def self.remove_pid
    File.delete(PID_FILE) if File.exist?(PID_FILE)
  end

  def rewrite_response(triplet)
    status, headers, body = triplet
    headers.delete "transfer-encoding"
    triplet
  end

  private

  def allowed_url?(urls, host)
    case urls
    when Array
      urls.any? { |url| allowed_url?(url, host) }
    when Regexp
      urls =~ host
    when String
      urls == host
    end
  end

  def write_pid(pid)
    tmp_dir = 'tmp'
    pid_dir = File.join(tmp_dir, 'pids')
    Dir.mkdir(tmp_dir) unless Dir.exist?(tmp_dir)
    Dir.mkdir(pid_dir) unless Dir.exist?(pid_dir)
    File.write(PID_FILE, pid)
  end

  def ensure_log_exists
    log_file = File.join('log', 'test.log')
    Dir.mkdir('log') unless Dir.exist?('log')
    File.open(log_file, 'a') { |f| f.write "" }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
capybara-webmock-0.4.4 lib/capybara/webmock/proxy.rb
capybara-webmock-0.4.2 lib/capybara/webmock/proxy.rb