Sha256: f04e7e58f820235d9f51df30631a4efe368a4b2e6952bdd086a76c1b51ce2ddc

Contents?: true

Size: 1.36 KB

Versions: 27

Compression:

Stored size: 1.36 KB

Contents

require 'tempfile'
require 'shellwords'

module Percy
  module Capybara
    module HttpFetcher
      class Response < Struct.new(:body, :content_type); end

      def self.fetch(url)
        tempfile = Tempfile.new('percy-capybara-fetch')
        temppath = tempfile.path

        # Close and delete the tempfile, we just wanted the name. Also, we use the existence of the
        # file as a signal below.
        tempfile.close
        tempfile.unlink

        # Use curl as a magical subprocess weapon which escapes this Ruby sandbox and is not
        # influenced by any HTTP middleware/restrictions. This helps us avoid causing lots of
        # problems for people using gems like VCR/WebMock. We also disable certificate checking
        # because, as odd as that is, it's the default state for Selenium Firefox and others.
        output = `curl --insecure -v -o #{temppath} "#{url.shellescape}" 2>&1`
        content_type = output.match(/< Content-Type:(.*)/i)
        content_type = content_type[1].strip if content_type

        if File.exist?(temppath)
          response = Percy::Capybara::HttpFetcher::Response.new(File.read(temppath), content_type)
          # We've broken the tempfile so it won't get deleted when garbage collected. Delete!
          File.delete(temppath)
          return if response.body == ''
          response
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
percy-capybara-2.4.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.3.6 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.3.5 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.3.4 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.3.3 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.3.2 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.3.1 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.3.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.3.0.pre.beta lib/percy/capybara/httpfetcher.rb
percy-capybara-2.2.1 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.2.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.1.1 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.1.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.0.1 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.0.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-1.2.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-1.1.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-1.0.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-0.7.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-0.6.1 lib/percy/capybara/httpfetcher.rb