Class: Capybara::Mechanize::Browser

Inherits:
RackTest::Browser
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/capybara/mechanize/browser.rb

Defined Under Namespace

Classes: ResponseProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver) ⇒ Browser

Returns a new instance of Browser.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/capybara/mechanize/browser.rb', line 10

def initialize(driver)
  @agent = ::Mechanize.new
  @agent.redirect_ok = false
  @agent.user_agent = default_user_agent
  #@agent.agent.max_history = 0
  if !driver.options.empty?
    if driver.options[:certificate]
      if !driver.options[:certificate].is_a? Hash
        @agent.cert = driver.options[:certificate].certificate
        @agent.key = driver.options[:certificate].key
      elsif driver.options[:certificate][:key] && driver.options[:certificate][:cer]
        @agent.cert = driver.options[:certificate][:cer]
        @agent.key = driver.options[:certificate][:key]
      end
      if driver.options[:ca]
        @agent.ca_file = driver.options[:ca]
      end
    end
    if driver.options[:user_agent]
      @agent.user_agent = driver.options[:user_agent]
    end
    if driver.options[:username] and driver.options[:password]
      @agent.basic_auth(driver.options[:username], driver.options[:password])
    end
    if driver.options[:proxy] && !driver.options[:proxy].empty?
      proxy = nil
      begin
        proxy = URI.parse(driver.options[:proxy])
      rescue URI::InvalidURIError => e
        raise "You have entered an invalid proxy address #{driver.options[:proxy]}. Check proxy settings."
      end
      if proxy && proxy.instance_of?(URI::HTTP) 
        @agent.set_proxy(proxy.host, proxy.port)
      else
        raise "ProxyError: You have entered an invalid proxy address #{driver.options[:proxy]}. e.g. (http|https)://proxy.com(:port)"
      end
    end
  end
  super
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



180
181
182
# File 'lib/capybara/mechanize/browser.rb', line 180

def agent
  @agent
end

Instance Method Details

#current_urlObject



57
58
59
# File 'lib/capybara/mechanize/browser.rb', line 57

def current_url
  last_request_remote? ? remote_response.current_url : super
end

#delete(path, attributes = {}, headers = {}) ⇒ Object



144
145
146
# File 'lib/capybara/mechanize/browser.rb', line 144

def delete(path, attributes = {}, headers = {})
  process_without_redirect(:delete, path, attributes, headers)
end

#determine_path(path) ⇒ Object

TODO path Capybara to move this into its own method



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/capybara/mechanize/browser.rb', line 104

def determine_path(path)
  new_uri = URI.parse(path)
  current_uri = URI.parse(current_url)

  if new_uri.host
    @current_host = new_uri.scheme + '://' + new_uri.host
  end

  if new_uri.relative?
    path = request_path + path if path.start_with?('?')

    unless path.start_with?('/')
      folders = request_path.split('/')
      if folders.empty?
        path = '/' + path
      else
        path = (folders[0, folders.size - 1] << path).join('/')
      end
    end
    path = current_host + path
  end
  path
end

#follow_redirect!Object



72
73
74
75
76
77
78
# File 'lib/capybara/mechanize/browser.rb', line 72

def follow_redirect!
  unless last_response.redirect?
    raise "Last response was not a redirect. Cannot follow_redirect!"
  end

  get(last_location_header)
end

#follow_redirects!Object

Raises:

  • (Capybara::InfiniteRedirectError)


65
66
67
68
69
70
# File 'lib/capybara/mechanize/browser.rb', line 65

def follow_redirects!
  5.times do
    follow_redirect! if last_response.redirect?
  end
  raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if last_response.redirect?
end

#get(path, attributes = {}, headers = {}) ⇒ Object



129
130
131
# File 'lib/capybara/mechanize/browser.rb', line 129

def get(path, attributes = {}, headers = {})
  process_without_redirect(:get, path, attributes, headers)
end

#last_responseObject



61
62
63
# File 'lib/capybara/mechanize/browser.rb', line 61

def last_response
  last_request_remote? ? remote_response : super
end

#post(path, attributes = {}, headers = {}) ⇒ Object



134
135
136
# File 'lib/capybara/mechanize/browser.rb', line 134

def post(path, attributes = {}, headers = {})
  process_without_redirect(:post, path, post_data(attributes), headers)
end

#post_data(params) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/capybara/mechanize/browser.rb', line 148

def post_data(params)
  params.inject({}) do |memo, param|
    case param
    when Hash
      param.each {|attribute, value| memo[attribute] = value }
      memo
    when Array
      case param.last
      when Hash
        param.last.each {|attribute, value| memo["#{param.first}[#{attribute}]"] = value }
      else
        memo[param.first] = param.last
      end
      memo
    end
  end
end

#process(method, path, *options) ⇒ Object



80
81
82
83
84
# File 'lib/capybara/mechanize/browser.rb', line 80

def process(method, path, *options)
  reset_cache!
  send(method, path, *options)
  follow_redirects!
end

#process_without_redirect(method, path, attributes, headers) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/capybara/mechanize/browser.rb', line 86

def process_without_redirect(method, path, attributes, headers)
  path = @last_path if path.nil? || path.empty?

  if remote?(path)
    process_remote_request(method, path, attributes, headers)
  else
    register_local_request

    path = determine_path(path)

    reset_cache!
    send("racktest_#{method}", path, attributes, env.merge(headers))
  end

  @last_path = path
end

#put(path, attributes = {}, headers = {}) ⇒ Object



139
140
141
# File 'lib/capybara/mechanize/browser.rb', line 139

def put(path, attributes = {}, headers = {})
  process_without_redirect(:put, path, attributes, headers)
end

#racktest_deleteObject



143
# File 'lib/capybara/mechanize/browser.rb', line 143

alias :racktest_delete :delete

#racktest_getObject



128
# File 'lib/capybara/mechanize/browser.rb', line 128

alias :racktest_get :get

#racktest_postObject



133
# File 'lib/capybara/mechanize/browser.rb', line 133

alias :racktest_post :post

#racktest_putObject



138
# File 'lib/capybara/mechanize/browser.rb', line 138

alias :racktest_put :put

#remote?(url) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/capybara/mechanize/browser.rb', line 166

def remote?(url)
  if Capybara.app_host
    true
  else
    host = URI.parse(url).host

    if host.nil?
      last_request_remote?
    else
      !Capybara::Mechanize.local_hosts.include?(host)
    end
  end
end

#reset_host!Object



51
52
53
54
55
# File 'lib/capybara/mechanize/browser.rb', line 51

def reset_host!
  @last_remote_uri = nil
  @last_request_remote = nil
  super
end