Class: BrowserMob::Proxy::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/browsermob/proxy/client.rb

Constant Summary collapse

TIMEOUTS =

Specify timeouts that will be used by a proxy (see README of browsermob-proxy itself for more info about what they mean)

{
  request: :requestTimeout,
  read: :readTimeout,
  connection: :connectionTimeout,
  dns_cache: :dnsCacheTimeout
}
LIMITS =
{
  :upstream_kbps   => 'upstreamKbps',
  :downstream_kbps => 'downstreamKbps',
  :latency         => 'latency'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, host, port) ⇒ Client



22
23
24
25
26
# File 'lib/browsermob/proxy/client.rb', line 22

def initialize(resource, host, port)
  @resource = resource
  @host = host
  @port = port
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/browsermob/proxy/client.rb', line 5

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/browsermob/proxy/client.rb', line 5

def port
  @port
end

Class Method Details

.from(server_url) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/browsermob/proxy/client.rb', line 7

def self.from(server_url)
  # ActiveSupport may define Object#load, so we can't use MultiJson.respond_to? here.
  sm = MultiJson.singleton_methods.map { |e| e.to_sym }
  decode_method = sm.include?(:load) ? :load : :decode

  port = MultiJson.send(decode_method,
    RestClient.post(URI.join(server_url, "proxy").to_s, '')
  ).fetch('port')

  uri = URI.parse(File.join(server_url, "proxy", port.to_s))
  resource = RestClient::Resource.new(uri.to_s)

  Client.new resource, uri.host, port
end

Instance Method Details

#basic_authentication(domain, username, password) ⇒ Object



114
115
116
117
# File 'lib/browsermob/proxy/client.rb', line 114

def basic_authentication(domain, username, password)
  data = { username: username, password: password }
  @resource["auth/basic/#{domain}"].post data.to_json, :content_type => "application/json"
end

#blacklist(regexp, status_code) ⇒ Object



100
101
102
103
# File 'lib/browsermob/proxy/client.rb', line 100

def blacklist(regexp, status_code)
  regex = Regexp === regexp ? regexp.source : regexp.to_s
  @resource['blacklist'].put :regex => regex, :status => status_code
end

#clear_blacklistObject



105
106
107
# File 'lib/browsermob/proxy/client.rb', line 105

def clear_blacklist
  @resource['blacklist'].delete
end

#clear_whitelistObject



96
97
98
# File 'lib/browsermob/proxy/client.rb', line 96

def clear_whitelist
  @resource['whitelist'].delete
end

#closeObject



189
190
191
# File 'lib/browsermob/proxy/client.rb', line 189

def close
  @resource.delete
end

#harObject



63
64
65
# File 'lib/browsermob/proxy/client.rb', line 63

def har
  HAR::Archive.from_string @resource["har"].get
end

#header(hash) ⇒ Object Also known as: headers



109
110
111
# File 'lib/browsermob/proxy/client.rb', line 109

def header(hash)
  @resource['headers'].post hash.to_json, :content_type => "application/json"
end

#limit(opts) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/browsermob/proxy/client.rb', line 171

def limit(opts)
  params = {}

  opts.each do |key, value|
    unless LIMITS.member?(key)
      raise ArgumentError, "invalid: #{key.inspect} (valid options: #{LIMITS.keys.inspect})"
    end

    params[LIMITS[key]] = Integer(value)
  end

  if params.empty?
    raise ArgumentError, "must specify one of #{LIMITS.keys.inspect}"
  end

  @resource['limit'].put params
end

#new_har(ref = nil, opts = {}) ⇒ Object

Examples:

client.new_har("page-name")
client.new_har("page-name", :capture_headers => true)
client.new_har(:capture_headers => true)
client.new_har(:capture_content => true)
client.new_har(:capture_binary_content => true)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/browsermob/proxy/client.rb', line 38

def new_har(ref = nil, opts = {})
  if opts.empty? && ref.kind_of?(Hash)
    opts = ref
    ref = nil
  end

  params = {}

  params[:initialPageRef] = ref if ref
  params[:captureHeaders] = true if opts[:capture_headers]
  params[:captureContent] = true if opts[:capture_content]

  if opts[:capture_binary_content]
    params[:captureContent] = true
    params[:captureBinaryContent] = true
  end

  previous = @resource["har"].put params
  HAR::Archive.from_string(previous) unless previous.empty?
end

#new_page(ref) ⇒ Object



59
60
61
# File 'lib/browsermob/proxy/client.rb', line 59

def new_page(ref)
  @resource['har/pageRef'].put :pageRef => ref
end

#remap_dns_hosts(hash) ⇒ Object

Override normal DNS lookups (remap the given hosts with the associated IP address).

Each invocation of the method will add given hosts to existing BrowserMob’s DNS cache instead of overriding it.

Examples:

remap_dns_hosts(‘example.com’ => ‘1.2.3.4’)



161
162
163
# File 'lib/browsermob/proxy/client.rb', line 161

def remap_dns_hosts(hash)
  @resource['hosts'].post hash.to_json, :content_type => 'application/json'
end

#selenium_proxy(*protocols) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/browsermob/proxy/client.rb', line 67

def selenium_proxy(*protocols)
  require 'selenium-webdriver' unless defined?(Selenium)

  protocols += [:http] if protocols.empty?
  unless (protocols - [:http, :ssl, :ftp]).empty?
    raise "Invalid protocol specified.  Must be one of: :http, :ssl, or :ftp."
  end

  proxy_mapping = {}
  protocols.each { |proto| proxy_mapping[proto] = "#{@host}:#{@port}" }
  Selenium::WebDriver::Proxy.new(proxy_mapping)
end

#timeouts(timeouts = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/browsermob/proxy/client.rb', line 137

def timeouts(timeouts = {})
  params = {}

  timeouts.each do |key, value|
    unless TIMEOUTS.member?(key)
      raise ArgumentError, "invalid key: #{key.inspect}, should belong to: #{TIMEOUTS.keys.inspect}"
    end

    params[TIMEOUTS[key]] = (value * 1000).to_i
  end

  @resource['timeout'].put params
end

#whitelist(regexp, status_code) ⇒ Object

Set a list of URL regexes to whitelist

Note that passed regexp/string should match string as a whole (i.e. if /example.com/ is whitelisted “www.example.com” won’t be allowed though if /.+example.com“ is whitelisted ”www.example.com“ will be allowed)



91
92
93
94
# File 'lib/browsermob/proxy/client.rb', line 91

def whitelist(regexp, status_code)
  regex = Array(regexp).map { |rx| Regexp === rx ? rx.source : rx.to_s }.join(',')
  @resource['whitelist'].put :regex => regex, :status => status_code
end