diff --git a/browsermobproxy/client.py b/browsermobproxy/client.py index e63d3cd..8280408 100644 --- a/browsermobproxy/client.py +++ b/browsermobproxy/client.py @@ -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 @@ -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(":") @@ -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): + """ + 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 = { @@ -225,8 +245,8 @@ def limits(self, options): Limit the bandwidth through the proxy. :param dict options: A dictionary with all the details you want to set. - downstreamKbps - Sets the downstream kbps - upstreamKbps - Sets the upstream kbps + downstream_kbps - Sets the downstream kbps + upstream_kbps - Sets the upstream kbps latency - Add the given latency to each HTTP request """ params = {} diff --git a/browsermobproxy/server.py b/browsermobproxy/server.py index b5620d5..2f75398 100644 --- a/browsermobproxy/server.py +++ b/browsermobproxy/server.py @@ -27,7 +27,7 @@ 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. @@ -35,8 +35,9 @@ def create_proxy(self, params=None): :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): @@ -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 @@ -99,13 +100,12 @@ def start(self, options=None): of the log file with resp. keys of `log_path` and `log_file` """ if options is None: - options = { - 'log_path': os.getcwd(), - 'log_file': 'server.log', - } - 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) + options = {} + log_path = options.get('log_path', os.getcwd()) + log_file = options.get('log_file', 'server.log') + retry_sleep = options.get('retry_sleep', 0.5) + retry_count = options.get('retry_count', 60) + log_path_name = os.path.join(log_path, log_file) self.log_file = open(log_path_name, 'w') self.process = subprocess.Popen(self.command, @@ -120,9 +120,9 @@ def start(self, options=None): "for a helpful error message.".format(self.log_file)) raise Exception(message) - time.sleep(0.5) + time.sleep(retry_sleep) count += 1 - if count == 60: + if count == retry_count: self.stop() raise Exception("Can't connect to Browsermob-Proxy") diff --git a/test/test_client.py b/test/test_client.py index a260977..aa98ed4 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -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):