<commit_before>require 'mechanize'

class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
  extend Forwardable
  
  def_delegator :agent, :scheme_handlers
  def_delegator :agent, :scheme_handlers=
  
  def initialize(*args)
    super
    @agent = ::Mechanize.new
    @agent.redirect_ok = false
  end
  
  def cleanup!
    @agent.cookie_jar.clear!
    super
  end
  
  def current_url
    last_request_remote? ? remote_response.current_url : super
  end
  
  def response
    last_request_remote? ? remote_response : super
  end
  
  # TODO see how this can be cleaned up
  def follow_redirect!
    unless response.redirect?
      raise "Last response was not a redirect. Cannot follow_redirect!"
    end
    
    location = if last_request_remote?
        remote_response.page.response['Location'] 
      else
        response['Location']
      end
    
    get(location)
  end
  
  def get(url, params = {}, headers = {})
    if remote?(url)
      process_remote_request(:get, url)
    else 
      super
    end
  end

  def post(url, params = {}, headers = {})
    if remote?(url)
      process_remote_request(:post, url, params, headers)
    else 
      super
    end
  end

  def remote?(url)
    if !Capybara.app_host.nil? 
      true
    elsif Capybara.default_host.nil?
      false
    else
      host = URI.parse(url).host
      !(host.nil? || host.include?(Capybara.default_host))
    end
  end

  attr_reader :agent

  private
  
  def last_request_remote?
    !!@last_request_remote
  end

  def process_remote_request(method, url, *options)
    if remote?(url)
      url = File.join((Capybara.app_host || Capybara.default_host), url) if URI.parse(url).host.nil?<commit_msg>Added put and delete for remote session and make one extra spec pass<commit_after>require 'mechanize'

class Capybara::Driver::Mechanize < Capybara::Driver::RackTest
  extend Forwardable
  
  def_delegator :agent, :scheme_handlers
  def_delegator :agent, :scheme_handlers=
  
  def initialize(*args)
    super
    @agent = ::Mechanize.new
    @agent.redirect_ok = false
  end
  
  def cleanup!
    @agent.cookie_jar.clear!
    super
  end
  
  def current_url
    last_request_remote? ? remote_response.current_url : super
  end
  
  def response
    last_request_remote? ? remote_response : super
  end
  
  # TODO see how this can be cleaned up
  def follow_redirect!
    unless response.redirect?
      raise "Last response was not a redirect. Cannot follow_redirect!"
    end
    
    location = if last_request_remote?
        remote_response.page.response['Location'] 
      else
        response['Location']
      end
    
    get(location)
  end
  
  def get(url, params = {}, headers = {})
    if remote?(url)
      process_remote_request(:get, url)
    else 
      super
    end
  end

  def post(url, params = {}, headers = {})
    if remote?(url)
      process_remote_request(:post, url, params, headers)
    else 
      super
    end
  end
  
  def put(url, params = {}, headers = {})
    if remote?(url)
      process_remote_request(:put, url)
    else 
      super
    end
  end

  def delete(url, params = {}, headers = {})
    if remote?(url)
      process_remote_request(:delete, url, params, headers)
    else 
      super
    end
  end

  def remote?(url)
    if !Capybara.app_host.nil? 
      true
    elsif Capybara.default_host.nil?
      false
    else
      host = URI.parse(url).host
      !(host.nil? || host.include?(Capybara.default_host))
    end
  end

  attr_reader :agent

  private
  
  def last_request_remote?
    !!@last_request_remote
  end

  def process_remote_request(method, url, *options)
    if remote?(url)
      url = File.join((Capybara.app_host || Capybara.default_host), url) if URI.parse(url).host.nil?
{'fraction_non_alphanumeric': 0.09450080085424453, 'fraction_numerical': 0.0, 'mean_word_length': 2.7357926221335993, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5194585', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1220, 'n_words': 297}