Sha256: 752f03de4b870a673c44f418f4e3b6e0e3abfac70ee878b785b88985bd4ba9d1

Contents?: true

Size: 1.36 KB

Versions: 7

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 --compressed --insecure -v -o #{temppath} "#{url.shellescape}" 2>&1`
        content_type = output.match(/< Content-Type:(.*)/i)
        content_type = content_type[1].strip if content_type

        return unless 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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
percy-capybara-3.1.1 lib/percy/capybara/httpfetcher.rb
percy-capybara-3.1.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-3.0.2 lib/percy/capybara/httpfetcher.rb
percy-capybara-3.0.1 lib/percy/capybara/httpfetcher.rb
percy-capybara-3.0.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.6.0 lib/percy/capybara/httpfetcher.rb
percy-capybara-2.5.1 lib/percy/capybara/httpfetcher.rb