Sha256: 9333cdd86d187c2a5d6e7390d29ac861173ac36918eed5bf8d24ad4c953e6929

Contents?: true

Size: 1.97 KB

Versions: 11

Compression:

Stored size: 1.97 KB

Contents

require 'capybara/mechanize/cucumber' 
require 'uri'
class Capybara::Mechanize::Browser
  #patch to remove catching all Mechanize exceptions (which are nice and specific) and throwing a useless RuntimeError
  def process_remote_request(method, url, attributes, headers)
    if remote?(url)
      uri = URI.parse(url)
      uri = resolve_relative_url(url) if uri.host.nil?
      @last_remote_uri = uri
      url = uri.to_s

      reset_cache!
      args = []
      args << attributes unless attributes.empty?
      args << headers unless headers.empty?
      @agent.send(method, url, *args)
      @last_request_remote = true
    end
  end
end

class Capybara::Mechanize::Driver
  #Patch for friendly cookie handling api
  def cookies
    cookies = []

    browser.agent.cookie_jar.jar.each do |domain|
      domain[1].each do |path|
        path[1].each do |cookie|
          cookies.push({
            :name => cookie[1].name,
            :value => cookie[1].value,
            :domain => cookie[1].domain,
            :secure => cookie[1].secure,
            :expires => cookie[1].expires,
            :path => cookie[1].path
          })
        end
      end
    end
    cookies
  end

  def cookie_named(name)
    cookies.find { |c| c[:name] == name }
  end

  def delete_cookie(cookie_name)
    browser.agent.cookie_jar.jar.each do |domain|
      domain[1].each do |path|
        path[1].each do |cookie|
          if cookie[0] == cookie_name
            browser.agent.cookie_jar.jar[domain[0]][path[0]].delete(cookie[0])
          end
        end
      end
    end
  end

  def delete_all_cookies
    browser.agent.cookie_jar.clear!
  end

  FakeURI = Struct.new(:host)
  def add_cookie(attribs)
    c = Mechanize::Cookie.new(attribs[:name],attribs[:value])
    # remember: mechanize always removes leading '.' from domains
    c.domain = attribs[:domain]
    c.path = '/'
    c.expires = attribs[:expires]
    c.secure = attribs[:secure]
    browser.agent.cookie_jar.add(FakeURI.new(c.domain),c)
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
frameworks-capybara-0.2.6 lib/monkey-patches/capybara-mechanize-patches.rb
frameworks-capybara-0.2.5 lib/monkey-patches/capybara-mechanize-patches.rb
frameworks-capybara-0.2.4 lib/monkey-patches/capybara-mechanize-patches.rb
frameworks-capybara-0.2.3 lib/monkey-patches/capybara-mechanize-patches.rb
frameworks-capybara-0.2.2 lib/monkey-patches/capybara-mechanize-patches.rb
frameworks-capybara-0.2.1 lib/monkey-patches/capybara-mechanize-patches.rb
frameworks-capybara-0.3.0.rc1 lib/monkey-patches/capybara-mechanize-patches.rb
frameworks-capybara-0.2.0 lib/monkey-patches/capybara-mechanize-patches.rb
frameworks-capybara-0.2.0.rc7 lib/monkey-patches/capybara-mechanize-patches.rb
frameworks-capybara-0.2.0.rc6 lib/monkey-patches/capybara-mechanize-patches.rb
frameworks-capybara-0.2.0.rc5 lib/monkey-patches/capybara-mechanize-patches.rb