Skip to content
Browse files

pep8 --ignore=E501

  • Loading branch information...
1 parent a6c95df commit 124c883e22504c9e2d7e8be438032eb08114ba54 @adamgoucher adamgoucher committed Jun 15, 2012
Showing with 37 additions and 37 deletions.
  1. +28 −30 browsermobproxy/client.py
  2. +9 −7 test/test_client.py
View
58 browsermobproxy/client.py
@@ -1,22 +1,21 @@
-#from httplib2 import Http
import requests
from urllib import urlencode
import json
+
class Client(object):
-
def __init__(self, url):
"""
Initialises a new Client object
:Args:
- url: This is where the BrowserMob Proxy lives
"""
- self.host = url
+ self.host = url
resp = requests.post('%s/proxy' % self.host, urlencode(''))
jcontent = json.loads(resp.content)
self.port = jcontent['port']
url_parts = self.host.split(":")
- self.proxy = url_parts[0] + ":" + url_parts[1] + ":" + str(self.port)
+ self.proxy = url_parts[0] + ":" + url_parts[1] + ":" + str(self.port)
def headers(self, headers):
"""
@@ -29,7 +28,7 @@ def headers(self, headers):
r = requests.post(url='%s/proxy/%s/headers' % (self.host, self.port),
data=json.dumps(headers),
- headers = {'content-type': 'application/json'})
+ headers={'content-type': 'application/json'})
return r.status_code
def new_har(self, ref=None):
@@ -55,24 +54,25 @@ def new_page(self, ref=None):
payload = {"pageRef": ref}
else:
payload = {}
- r = requests.put('%s/proxy/%s/har/pageRef' % (self.host, self.port), payload)
+ r = requests.put('%s/proxy/%s/har/pageRef' % (self.host, self.port),
+ payload)
return r.status_code
-
+
@property
def har(self):
"""
Gets the HAR that has been recorded
"""
r = requests.get('%s/proxy/%s/har' % (self.host, self.port))
-
+
return r.json
def selenium_proxy(self):
"""
Returns a Selenium WebDriver Proxy class with details of the HTTP Proxy
"""
from selenium import webdriver
- return webdriver.Proxy({"httpProxy":self.proxy})
+ return webdriver.Proxy({"httpProxy": self.proxy})
def webdriver_proxy(self):
"""
@@ -82,7 +82,8 @@ def webdriver_proxy(self):
def add_to_webdriver_capabilities(self, capabilities):
"""
- Adds an 'proxy' entry to a desired capabilities dictionary with the BrowserMob proxy information
+ Adds an 'proxy' entry to a desired capabilities dictionary with the
+ BrowserMob proxy information
"""
capabilities['proxy'] = {'proxyType': 'manual',
'httpProxy': self.proxy}
@@ -92,34 +93,31 @@ def whitelist(self, regexp, status_code):
Sets a list of URL patterns to whitelist
:Args:
- regex: a comma separated list of regular expressions
- - status_code: the HTTP status code to return for URLs that do not match the whitelist
-
+ - status_code: the HTTP status code to return for URLs that do not
+ match the whitelist
"""
- r = requests.put('%s/proxy/%s/whitelist' % (self.host, self.port),
- urlencode({ 'regex': regexp, 'status': status_code
- }))
+ r = requests.put('%s/proxy/%s/whitelist' % (self.host, self.port),
+ urlencode({'regex': regexp, 'status': status_code}))
return r.status_code
-
def blacklist(self, regexp, status_code):
"""
- Sets a list of URL patterns to blacklist
+ Sets a list of URL patterns to blacklist
:Args:
- regex: a comma separated list of regular expressions
- - status_code: the HTTP status code to return for URLs that do not match the blacklist
+ - status_code: the HTTP status code to return for URLs that do not
+ match the blacklist
"""
- r = requests.put('%s/proxy/%s/blacklist' % (self.host, self.port),
- urlencode({ 'regex': regexp, 'status': status_code
- }))
+ r = requests.put('%s/proxy/%s/blacklist' % (self.host, self.port),
+ urlencode({'regex': regexp, 'status': status_code}))
return r.status_code
LIMITS = {
- 'upstream_kbps' : 'upstreamKbps',
- 'downstream_kbps' : 'downstreamKbps',
- 'latency' : 'latency'
- }
-
+ 'upstream_kbps': 'upstreamKbps',
+ 'downstream_kbps': 'downstreamKbps',
+ 'latency': 'latency'
+ }
def limits(self, options):
"""
@@ -133,16 +131,16 @@ def limits(self, options):
params = {}
for (k, v) in options.items():
- if not self.LIMITS.has_key(k):
+ if k not in self.LIMITS:
raise KeyError('invalid key: %s' % k)
params[self.LIMITS[k]] = int(v)
if len(params.items()) == 0:
raise KeyError("You need to specify one of the valid Keys")
-
- r = requests.put('%s/proxy/%s/limit' % (self.host, self.port),
- urlencode(params))
+
+ r = requests.put('%s/proxy/%s/limit' % (self.host, self.port),
+ urlencode(params))
return r.status_code
def close(self):
View
16 test/test_client.py
@@ -2,17 +2,19 @@
import pytest
import sys
+
def setup_module(module):
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
+
class TestClient(object):
def setup_method(self, method):
from browsermobproxy.client import Client
self.client = Client("http://localhost:9090")
-
+
def teardown_method(self, method):
self.client.close()
-
+
def test_headers_type(self):
"""
/proxy/:port/headers needs to take a dictionary
@@ -36,7 +38,7 @@ def test_new_har(self):
"""
status_code, har = self.client.new_har()
assert(status_code == 204)
- assert(har == None)
+ assert(har is None)
status_code, har = self.client.new_har()
assert(status_code == 200)
assert('log' in har)
@@ -49,7 +51,7 @@ def test_new_har(self):
"""
status_code, har = self.client.new_har("elephants")
assert(status_code == 204)
- assert(har == None)
+ assert(har is None)
status_code, har = self.client.new_har("elephants")
assert(status_code == 200)
assert('elephants' == har["log"]["pages"][0]['id'])
@@ -83,15 +85,15 @@ def test_single_whitelist(self):
"""
status_code = self.client.whitelist("http://www\\.facebook\\.com/.*", 200)
assert(status_code == 200)
-
+
def test_multiple_whitelists(self):
"""
/proxy/:port/whitelist
adds a whitelist
"""
status_code = self.client.whitelist("http://www\\.facebook\\.com/.*,http://cdn\\.twitter\\.com", 200)
assert(status_code == 200)
-
+
def test_blacklist(self):
"""
/proxy/:port/blacklist
@@ -124,7 +126,7 @@ def test_limits_key_no_value(self):
limits = {"upstream_kbps": 320, "downstream_kbps": 560, "latency": 30}
status_code = self.client.limits(limits)
assert(status_code == 200)
-
+
def test_close(self):
"""
/proxy/:port

0 comments on commit 124c883

Please sign in to comment.
Something went wrong with that request. Please try again.