Skip to content

Some fixes and additional options. #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
32 changes: 26 additions & 6 deletions browsermobproxy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@


class Client(object):
def __init__(self, url, params=None, options=None):
def __init__(self, url, data=None, params=None, options=None):
"""
Initialises a new Client object


:param url: This is where the BrowserMob Proxy lives
:param data: Data to be POST'ed (for example bindAddress, useEcc and trustAllServers)
:param params: URL query (for example httpProxy and httpsProxy vars)
:param options: Dictionary that can contain the port of an existing
proxy to use (for example 'existing_proxy_port_to_use')
"""
data = data if data is not None else {}
params = params if params is not None else {}
options = options if options is not None else {}
self.host = "http://" + url
Expand All @@ -28,7 +30,7 @@ def __init__(self, url, params=None, options=None):
if 'existing_proxy_port_to_use' in options:
self.port = options['existing_proxy_port_to_use']
else:
resp = requests.post('%s/proxy' % self.host + urlparams)
resp = requests.post('%s/proxy' % self.host + urlparams, data=data)
jcontent = json.loads(resp.content.decode('utf-8'))
self.port = jcontent['port']
url_parts = self.host.split(":")
Expand Down Expand Up @@ -196,22 +198,40 @@ def response_interceptor(self, js):
"""
Executes the javascript against each response

:param str js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/interceptor/response' % (self.host, self.port),
data=js)
return r.status_code

def response_filter(self, js):
"""
Executes the javascript against each response

:param str js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/filter/response' % (self.host, self.port),
data=js,
headers={'content-type': 'application/json'})
data=js)
return r.status_code

def request_interceptor(self, js):
"""
Executes the javascript against each request

:param str js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/interceptor/request' % (self.host, self.port),
data=js)
return r.status_code

def request_filter(self, js):
Copy link

@scythargon scythargon Feb 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Could this method help me to prevent interception of all websocket connections according to this issue? lightbody/browsermob-proxy#408
If so - what should I write in js function?
Thanks in advance!

"""
Executes the javascript against each request

:param str js: the javascript to execute
"""
r = requests.post(url='%s/proxy/%s/filter/request' % (self.host, self.port),
data=js,
headers={'content-type': 'application/json'})
data=js)
return r.status_code

LIMITS = {
Expand Down
9 changes: 5 additions & 4 deletions browsermobproxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ def url(self):
"""
return "http://%s:%d" % (self.host, self.port)

def create_proxy(self, params=None):
def create_proxy(self, data=None, params=None):
"""
Gets a client class that allow to set all the proxy details that you
may need to.

:param dict params: Dictionary where you can specify params
like httpProxy and httpsProxy
"""
data = data if data is not None else {}
params = params if params is not None else {}
client = Client(self.url[7:], params)
client = Client(self.url[7:], data, params)
return client

def _is_listening(self):
Expand Down Expand Up @@ -80,7 +81,7 @@ def __init__(self, path='browsermob-proxy', options=None):
" provided: %s" % path)

self.path = path
self.host = 'localhost'
self.host = options.get('host', 'localhost')
self.port = options.get('port', 8080)
self.process = None

Expand All @@ -105,7 +106,7 @@ def start(self, options=None):
}
log_path = options.get('log_path')
log_file = options.get('log_file')
log_path_name = os.path.join(log_path, os.path.sep, log_file)
log_path_name = os.path.join(log_path, log_file)
self.log_file = open(log_path_name, 'w')

self.process = subprocess.Popen(self.command,
Expand Down
16 changes: 8 additions & 8 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,34 +166,34 @@ def test_close(self):

def test_response_interceptor_with_parsing_js(self):
"""
/proxy/:port/interceptor/response
/proxy/:port/filter/response
"""
js = 'alert("foo")'
status_code = self.client.response_interceptor(js)
status_code = self.client.response_filter(js)
assert(status_code == 200)

def test_response_interceptor_with_invalid_js(self):
"""
/proxy/:port/interceptor/response
/proxy/:port/filter/response
"""
js = 'alert("foo"'
status_code = self.client.response_interceptor(js)
status_code = self.client.response_filter(js)
assert(status_code == 500)

def test_request_interceptor_with_parsing_js(self):
"""
/proxy/:port/interceptor/request
/proxy/:port/filter/request
"""
js = 'alert("foo")'
status_code = self.client.request_interceptor(js)
status_code = self.client.request_filter(js)
assert(status_code == 200)

def test_request_interceptor_with_invalid_js(self):
"""
/proxy/:port/interceptor/request
/proxy/:port/filter/request
"""
js = 'alert("foo"'
status_code = self.client.request_interceptor(js)
status_code = self.client.request_filter(js)
assert(status_code == 500)

def test_timeouts_invalid_timeouts(self):
Expand Down