コード例 #1
0
 def setup_method(self, method):
     from browsermobproxy.client import Client
     self.client = Client("localhost:9090")
     chrome_binary = environ.get("CHROME_BIN", None)
     self.desired_caps = selenium.webdriver.common.desired_capabilities.DesiredCapabilities.CHROME
     if chrome_binary is not None:
         self.desired_caps.update({
             "chromeOptions": {
                 "binary": chrome_binary,
                 "args": ['no-sandbox']
             }
         })
     self.driver = webdriver.Remote(desired_capabilities=self.desired_caps,
                                    proxy=self.client)
コード例 #2
0
ファイル: test_remote.py プロジェクト: wenshiqi0/gecko-dev
class TestRemote(object):
    def setup_method(self, method):
        from browsermobproxy.client import Client
        self.client = Client("localhost:9090")

    def teardown_method(self, method):
        self.client.close()

    @pytest.mark.human
    def test_i_want_my_remote(self):
        driver = webdriver.Remote(
            desired_capabilities=selenium.webdriver.common.
            desired_capabilities.DesiredCapabilities.FIREFOX,
            proxy=self.client)

        self.client.new_har("mtv")
        targetURL = "http://www.mtv.com"
        self.client.rewrite_url(
            ".*american_flag-384x450\\.jpg",
            "http://www.foodsubs.com/Photos/englishmuffin.jpg")

        driver.get(targetURL)
        time.sleep(5)

        driver.quit()
コード例 #3
0
class TestRemote(object):
    def setup_method(self, method):
        from browsermobproxy.client import Client
        self.client = Client("localhost:9090")
        chrome_binary = environ.get("CHROME_BIN", None)
        self.desired_caps = selenium.webdriver.common.desired_capabilities.DesiredCapabilities.CHROME
        if chrome_binary is not None:
            self.desired_caps.update({
                "chromeOptions": {
                    "binary": chrome_binary,
                    "args": ['no-sandbox']
                }
            })
        self.driver = webdriver.Remote(desired_capabilities=self.desired_caps,
                                       proxy=self.client)

    def teardown_method(self, method):
        self.client.close()
        self.driver.quit()

    @pytest.mark.human
    def test_set_clear_url_rewrite_rule(self):
        targetURL = "https://www.saucelabs.com/versions.js"
        assert 200 == self.client.rewrite_url(
            "https://www.saucelabs.com/versions.+",
            "https://www.saucelabs.com/versions.json")
        self.driver.get(targetURL)
        assert "Sauce Connect" in self.driver.page_source
        assert self.client.clear_all_rewrite_url_rules() == 200
        self.driver.get(targetURL)
        assert "Sauce Connect" not in self.driver.page_source

    @pytest.mark.human
    def test_response_interceptor(self):
        content = "Response successfully intercepted"
        targetURL = "https://saucelabs.com/versions.json?hello"
        self.client.response_interceptor(
            """if(messageInfo.getOriginalUrl().contains('?hello')){contents.setTextContents("%s");}"""
            % content)
        self.driver.get(targetURL)
        assert content in self.driver.page_source
コード例 #4
0
ファイル: test_remote.py プロジェクト: luke-chang/gecko-1
class TestRemote(object):
    def setup_method(self, method):
        from browsermobproxy.client import Client
        self.client = Client("localhost:9090")

    def teardown_method(self, method):
        self.client.close()

    @pytest.mark.human
    def test_i_want_my_remote(self):
        driver = webdriver.Remote(desired_capabilities=selenium.webdriver.common.desired_capabilities.DesiredCapabilities.FIREFOX,
                                  proxy=self.client)

        self.client.new_har("mtv")
        targetURL = "http://www.mtv.com"
        self.client.rewrite_url(".*american_flag-384x450\\.jpg", "http://www.foodsubs.com/Photos/englishmuffin.jpg")

        driver.get(targetURL)
        time.sleep(5)

        driver.quit()
コード例 #5
0
class TestWebDriver(object):
    def setup_method(self, method):
        from browsermobproxy.client import Client
        self.client = Client("localhost:9090")

    def teardown_method(self, method):
        self.client.close()

    def test_i_want_my(self):
        driver = webdriver.Firefox(proxy=self.client)

        self.client.new_har("mtv")
        targetURL = "http://www.mtv.com"
        self.client.rewrite_url(".*american_flag-384x450\\.jpg", "http://www.foodsubs.com/Photos/englishmuffin.jpg")

        driver.get(targetURL)

        import time
        time.sleep(10)

        driver.quit()
コード例 #6
0
class TestWebDriver(object):
    def setup_method(self, method):
        from browsermobproxy.client import Client
        self.client = Client("localhost:9090")

    def teardown_method(self, method):
        self.client.close()

    def test_i_want_my(self):
        driver = webdriver.Firefox(proxy=self.client)

        self.client.new_har("mtv")
        targetURL = "http://www.mtv.com"
        self.client.rewrite_url(
            ".*american_flag-384x450\\.jpg",
            "http://www.foodsubs.com/Photos/englishmuffin.jpg")

        driver.get(targetURL)

        import time
        time.sleep(10)

        driver.quit()
コード例 #7
0
import time

# import the Selenium library to automate the web browser
from selenium import webdriver

# import the Selene library to help simplify browser automation
# Selene uses the Selenium library
from selene.api import browser, s, ss, be, have, by

# load libraries to communicate with the browsermob proxy
from browsermobproxy.client import Client

# set the url of the server under test
url = 'https://espn.com'

bmp_client = Client("bmp:18080")

# setup the proxy limits
bmp_client.limits({
    "downstream_kbps": 0,
    "upstream_kbps": 0,
    "latency": 0,
})

# start capturing HAR data
bmp_client.new_har(options={"captureHeaders": True, "captureContent": True})
bmp_client.new_page(ref="slow_internet")

# update the browser settings
capabilities = webdriver.DesiredCapabilities.FIREFOX.copy()
capabilities["acceptInsecureCerts"] = True
コード例 #8
0
 def setup_method(self, method):
     from browsermobproxy.client import Client
     self.client = Client("localhost:9090")
     self.driver = None
コード例 #9
0
class TestWebDriver(object):
    def setup_method(self, method):
        from browsermobproxy.client import Client
        self.client = Client("localhost:9090")
        self.driver = None

    def teardown_method(self, method):
        self.client.close()
        if self.driver is not None:
            self.driver.quit()

    @pytest.mark.human
    def test_i_want_my_by_capability(self):
        capabilities = selenium.webdriver.common.desired_capabilities.DesiredCapabilities.CHROME
        self.client.add_to_capabilities(capabilities)
        # sets self.driver for proper clean up
        self._create_webdriver(capabilites=capabilities)

        self._run_url_rewrite_test()

    @pytest.mark.human
    def test_i_want_my_by_proxy_object(self):
        self._create_webdriver(capabilites=selenium.webdriver.common.desired_capabilities.DesiredCapabilities.CHROME,
                               proxy=self.client)

        self._run_url_rewrite_test()

    def test_what_things_look_like(self):
        bmp_capabilities = copy.deepcopy(selenium.webdriver.common.desired_capabilities.DesiredCapabilities.FIREFOX)
        self.client.add_to_capabilities(bmp_capabilities)

        proxy_capabilities = copy.deepcopy(selenium.webdriver.common.desired_capabilities.DesiredCapabilities.FIREFOX)
        proxy_addr = 'localhost:%d' % self.client.port
        proxy = Proxy({'httpProxy': proxy_addr,'sslProxy': proxy_addr})
        proxy.add_to_capabilities(proxy_capabilities)

        assert bmp_capabilities == proxy_capabilities

    def _create_webdriver(self, capabilites, proxy=None):
        chrome_binary = environ.get("CHROME_BIN", None)
        if chrome_binary is not None:
            capabilites.update({
                "chromeOptions": {
                    "binary": chrome_binary,
                    "args": ['no-sandbox']
                }
            })
        if proxy is None:
            self.driver = webdriver.Remote(desired_capabilities=capabilites)
        else:
            self.driver = webdriver.Remote(desired_capabilities=capabilites, proxy=proxy)

    def _run_url_rewrite_test(self):
        targetURL = "https://www.saucelabs.com/versions.js"
        assert 200 == self.client.rewrite_url(
            "https://www.saucelabs.com/versions.+", "https://www.saucelabs.com/versions.json"
        )
        self.driver.get(targetURL)
        assert "Sauce Connect" in self.driver.page_source
        assert self.client.clear_all_rewrite_url_rules() == 200
        self.driver.get(targetURL)
        assert "Sauce Connect" not in self.driver.page_source
コード例 #10
0
 def setup_method(self, method):
     from browsermobproxy.client import Client
     self.client = Client("localhost:9090")
コード例 #11
0
class TestWebDriver(object):
    def setup_method(self, method):
        from browsermobproxy.client import Client
        self.client = Client("localhost:9090")

    def teardown_method(self, method):
        self.client.close()

    @pytest.mark.human
    def test_i_want_my_by_capability(self):
        capabilities = selenium.webdriver.common.desired_capabilities.DesiredCapabilities.FIREFOX
        self.client.add_to_capabilities(capabilities)
        driver = webdriver.Firefox(capabilities=capabilities)

        self.client.new_har("mtv")
        targetURL = "http://www.mtv.com"
        self.client.rewrite_url(
            ".*american_flag-384x450\\.jpg",
            "http://www.foodsubs.com/Photos/englishmuffin.jpg")

        driver.get(targetURL)

        time.sleep(5)

        driver.quit()

    @pytest.mark.human
    def test_i_want_my_by_proxy_object(self):
        driver = webdriver.Firefox(proxy=self.client)

        self.client.new_har("mtv")
        targetURL = "http://www.mtv.com"
        self.client.rewrite_url(
            ".*american_flag-384x450\\.jpg",
            "http://www.foodsubs.com/Photos/englishmuffin.jpg")

        driver.get(targetURL)

        time.sleep(5)

        driver.quit()

    def test_what_things_look_like(self):
        bmp_capabilities = copy.deepcopy(
            selenium.webdriver.common.desired_capabilities.DesiredCapabilities.
            FIREFOX)
        self.client.add_to_capabilities(bmp_capabilities)

        proxy_capabilities = copy.deepcopy(
            selenium.webdriver.common.desired_capabilities.DesiredCapabilities.
            FIREFOX)
        proxy_addr = 'localhost:{}'.format(self.client.port)
        proxy = Proxy({'httpProxy': proxy_addr, 'sslProxy': proxy_addr})
        proxy.add_to_capabilities(proxy_capabilities)

        assert bmp_capabilities == proxy_capabilities
コード例 #12
0
class TestWebDriver(object):
    def setup_method(self, method):
        from browsermobproxy.client import Client
        self.client = Client("localhost:9090")

    def teardown_method(self, method):
        self.client.close()

    @pytest.mark.human
    def test_i_want_my_by_capability(self):
        capabilities = selenium.webdriver.common.desired_capabilities.DesiredCapabilities.FIREFOX
        self.client.add_to_capabilities(capabilities)
        driver = webdriver.Firefox(capabilities=capabilities)

        self.client.new_har("mtv")
        targetURL = "http://www.mtv.com"
        self.client.rewrite_url(".*american_flag-384x450\\.jpg", "http://www.foodsubs.com/Photos/englishmuffin.jpg")

        driver.get(targetURL)

        time.sleep(5)

        driver.quit()

    @pytest.mark.human
    def test_i_want_my_by_proxy_object(self):
        driver = webdriver.Firefox(proxy=self.client)

        self.client.new_har("mtv")
        targetURL = "http://www.mtv.com"
        self.client.rewrite_url(".*american_flag-384x450\\.jpg", "http://www.foodsubs.com/Photos/englishmuffin.jpg")

        driver.get(targetURL)

        time.sleep(5)

        driver.quit()

    def test_what_things_look_like(self):
        bmp_capabilities = copy.deepcopy(selenium.webdriver.common.desired_capabilities.DesiredCapabilities.FIREFOX)
        self.client.add_to_capabilities(bmp_capabilities)

        proxy_capabilities = copy.deepcopy(selenium.webdriver.common.desired_capabilities.DesiredCapabilities.FIREFOX)
        proxy_addr = 'localhost:%d' % self.client.port
        proxy = Proxy({'httpProxy': proxy_addr,'sslProxy': proxy_addr})
        proxy.add_to_capabilities(proxy_capabilities)

        assert bmp_capabilities == proxy_capabilities
コード例 #13
0
class TestWebDriver(object):
    def setup_method(self, method):
        from browsermobproxy.client import Client
        self.client = Client("localhost:9090")
        self.driver = None

    def teardown_method(self, method):
        self.client.close()
        if self.driver is not None:
            self.driver.quit()

    @pytest.mark.human
    def test_i_want_my_by_capability(self):
        capabilities = selenium.webdriver.common.desired_capabilities.DesiredCapabilities.CHROME
        self.client.add_to_capabilities(capabilities)
        # sets self.driver for proper clean up
        self._create_webdriver(capabilites=capabilities)

        self._run_url_rewrite_test()

    @pytest.mark.human
    def test_i_want_my_by_proxy_object(self):
        self._create_webdriver(capabilites=selenium.webdriver.common.
                               desired_capabilities.DesiredCapabilities.CHROME,
                               proxy=self.client)

        self._run_url_rewrite_test()

    def test_what_things_look_like(self):
        bmp_capabilities = copy.deepcopy(
            selenium.webdriver.common.desired_capabilities.DesiredCapabilities.
            FIREFOX)
        self.client.add_to_capabilities(bmp_capabilities)

        proxy_capabilities = copy.deepcopy(
            selenium.webdriver.common.desired_capabilities.DesiredCapabilities.
            FIREFOX)
        proxy_addr = 'localhost:%d' % self.client.port
        proxy = Proxy({'httpProxy': proxy_addr, 'sslProxy': proxy_addr})
        proxy.add_to_capabilities(proxy_capabilities)

        assert bmp_capabilities == proxy_capabilities

    def _create_webdriver(self, capabilites, proxy=None):
        chrome_binary = environ.get("CHROME_BIN", None)
        if chrome_binary is not None:
            capabilites.update({
                "chromeOptions": {
                    "binary": chrome_binary,
                    "args": ['no-sandbox']
                }
            })
        if proxy is None:
            self.driver = webdriver.Remote(desired_capabilities=capabilites)
        else:
            self.driver = webdriver.Remote(desired_capabilities=capabilites,
                                           proxy=proxy)

    def _run_url_rewrite_test(self):
        targetURL = "https://www.saucelabs.com/versions.js"
        assert 200 == self.client.rewrite_url(
            "https://www.saucelabs.com/versions.+",
            "https://www.saucelabs.com/versions.json")
        self.driver.get(targetURL)
        assert "Sauce Connect" in self.driver.page_source
        assert self.client.clear_all_rewrite_url_rules() == 200
        self.driver.get(targetURL)
        assert "Sauce Connect" not in self.driver.page_source
コード例 #14
0
 def setup_method(self, method):
     from browsermobproxy.client import Client
     self.client = Client("http://localhost:9090")
コード例 #15
0
class TestClient(object):
    def setup_method(self, method):
        from browsermobproxy.client import Client
        self.client = Client("localhost:9090")

    def teardown_method(self, method):
        self.client.close()

    def test_headers_type(self):
        """
        /proxy/:port/headers needs to take a dictionary
        """
        with pytest.raises(TypeError):
            self.client.headers(['foo'])

    def test_headers_content(self):
        """
        /proxy/:port/headers needs to take a dictionary
        and returns 200 when its successful
        """
        s = self.client.headers(
            {'User-Agent': 'rubber ducks floating in a row'})
        assert (s == 200)

    def test_new_har(self):
        """
        /proxy/:port/har
        and returns 204 when creating a har with a particular name the first time
        and returns 200 and the previous har when creating one with the same name
        """
        status_code, har = self.client.new_har()
        assert (status_code == 204)
        assert (har is None)
        status_code, har = self.client.new_har()
        assert (status_code == 200)
        assert ('log' in har)

    def test_new_har(self):
        """
        /proxy/:port/har
        and returns 204 when creating a har with a particular name the first time
        and returns 200 and the previous har when creating one with the same name
        """
        status_code, har = self.client.new_har("elephants")
        assert (status_code == 204)
        assert (har is None)
        status_code, har = self.client.new_har("elephants")
        assert (status_code == 200)
        assert ('elephants' == har["log"]["pages"][0]['id'])

    def test_new_page_defaults(self):
        """
        /proxy/:port/pageRef
        adds a new page of 'Page N' when no page name is given
        """
        self.client.new_har()
        self.client.new_page()
        har = self.client.har
        assert (len(har["log"]["pages"]) == 2)
        assert (har["log"]["pages"][1]["id"] == "Page 2")

    def test_new_named_page(self):
        """
        /proxy/:port/pageRef
        adds a new page of 'buttress'
        """
        self.client.new_har()
        self.client.new_page('buttress')
        har = self.client.har
        assert (len(har["log"]["pages"]) == 2)
        assert (har["log"]["pages"][1]["id"] == "buttress")

    def test_single_whitelist(self):
        """
        /proxy/:port/whitelist
        adds a whitelist
        """
        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
        adds a blacklist
        """
        status_code = self.client.blacklist("http://www\\.facebook\\.com/.*",
                                            200)
        assert (status_code == 200)

    def test_basic_authentication(self):
        """
        /proxy/:port/auth/basic
        adds automatic basic authentication
        """
        status_code = self.client.basic_authentication("www.example.com",
                                                       "myUsername",
                                                       "myPassword")
        assert (status_code == 200)

    def test_limits_invalid_key(self):
        """
        /proxy/:port/limits
        pre-sending checking that the parameter is correct
        """
        with pytest.raises(KeyError):
            self.client.limits({"hurray": "explosions"})

    def test_limits_key_no_value(self):
        """
        /proxy/:port/limits
        pre-sending checking that a parameter exists
        """
        with pytest.raises(KeyError):
            self.client.limits({})

    def test_limits_all_key_values(self):
        """
        /proxy/:port/limits
        can send all 3 at once based on the proxy implementation
        """
        limits = {"upstream_kbps": 320, "downstream_kbps": 560, "latency": 30}
        status_code = self.client.limits(limits)
        assert (status_code == 200)

    def test_rewrite(self):
        """
        /proxy/:port/rewrite

        """
        match = "/foo"
        replace = "/bar"
        status_code = self.client.rewrite_url(match, replace)
        assert (status_code == 200)

    def test_close(self):
        """
        /proxy/:port
        close the proxy port
        """
        status_code = self.client.close()
        assert (status_code == 200)
        status_code = self.client.close()
        assert (status_code == 404)

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

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

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

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

    def test_timeouts_invalid_timeouts(self):
        """
        /proxy/:port/timeout
        pre-sending checking that the parameter is correct
        """
        with pytest.raises(KeyError):
            self.client.timeouts({"hurray": "explosions"})

    def test_timeouts_key_no_value(self):
        """
        /proxy/:port/timeout
        pre-sending checking that a parameter exists
        """
        with pytest.raises(KeyError):
            self.client.timeouts({})

    def test_timeouts_all_key_values(self):
        """
        /proxy/:port/timeout
        can send all 3 at once based on the proxy implementation
        """
        timeouts = {"request": 2, "read": 2, "connection": 2, "dns": 3}
        status_code = self.client.timeouts(timeouts)
        assert (status_code == 200)

    def test_remap_hosts(self):
        """
        /proxy/:port/hosts
        """
        status_code = self.client.remap_hosts("example.com", "1.2.3.4")
        assert (status_code == 200)

    def test_wait_for_traffic_to_stop(self):
        """
        /proxy/:port/wait
        """
        status_code = self.client.wait_for_traffic_to_stop(2000, 10000)
        assert (status_code == 200)

    def test_clear_dns_cache(self):
        """
        /proxy/:port/dns/cache
        """
        status_code = self.client.clear_dns_cache()
        assert (status_code == 200)

    def test_rewrite_url(self):
        """
        /proxy/:port/rewrite
        """
        status_code = self.client.rewrite_url('http://www.facebook\.com',
                                              'http://microsoft.com')
        assert (status_code == 200)

    def test_retry(self):
        """
        /proxy/:port/retry
        """
        status_code = self.client.retry(4)
        assert (status_code == 200)
コード例 #16
0
class TestClient(object):
    def setup_method(self, method):
        from browsermobproxy.client import Client
        self.client = Client("localhost:9090")

    def teardown_method(self, method):
        self.client.close()

    def test_headers_type(self):
        """
        /proxy/:port/headers needs to take a dictionary
        """
        with pytest.raises(TypeError):
            self.client.headers(['foo'])

    def test_headers_content(self):
        """
        /proxy/:port/headers needs to take a dictionary
        and returns 200 when its successful
        """
        s = self.client.headers({'User-Agent': 'rubber ducks floating in a row'})
        assert(s == 200)

    def test_new_har(self):
        """
        /proxy/:port/har
        and returns 204 when creating a har with a particular name the first time
        and returns 200 and the previous har when creating one with the same name
        """
        status_code, har = self.client.new_har()
        assert(status_code == 204)
        assert(har is None)
        status_code, har = self.client.new_har()
        assert(status_code == 200)
        assert('log' in har)

    def test_new_har(self):
        """
        /proxy/:port/har
        and returns 204 when creating a har with a particular name the first time
        and returns 200 and the previous har when creating one with the same name
        """
        status_code, har = self.client.new_har("elephants")
        assert(status_code == 204)
        assert(har is None)
        status_code, har = self.client.new_har("elephants")
        assert(status_code == 200)
        assert('elephants' == har["log"]["pages"][0]['id'])

    def test_new_har(self):
        """
        /proxy/:port/har
        and returns 204 when creating a har with a particular name the first time
        and returns 200 and the previous har when creating one with the same name
        """
        status_code, har = self.client.new_har("monkeys", True)
        assert(status_code == 204)
        assert(har is None)
        status_code, har = self.client.new_har("monkeys", True)
        assert(status_code == 200)
        assert('monkeys' == har["log"]["pages"][0]['id'])

    def test_new_page_defaults(self):
        """
        /proxy/:port/pageRef
        adds a new page of 'Page N' when no page name is given
        """
        self.client.new_har()
        self.client.new_page()
        har = self.client.har
        assert(len(har["log"]["pages"]) == 2)
        assert(har["log"]["pages"][1]["id"] == "Page 2")

    def test_new_named_page(self):
        """
        /proxy/:port/pageRef
        adds a new page of 'buttress'
        """
        self.client.new_har()
        self.client.new_page('buttress')
        har = self.client.har
        assert(len(har["log"]["pages"]) == 2)
        assert(har["log"]["pages"][1]["id"] == "buttress")

    def test_single_whitelist(self):
        """
        /proxy/:port/whitelist
        adds a whitelist
        """
        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
        adds a blacklist
        """
        status_code = self.client.blacklist("http://www\\.facebook\\.com/.*", 200)
        assert(status_code == 200)

    def test_basic_authentication(self):
        """
        /proxy/:port/auth/basic
        adds automatic basic authentication
        """
        status_code = self.client.basic_authentication("www.example.com", "myUsername", "myPassword")
        assert(status_code == 200)

    def test_limits_invalid_key(self):
        """
        /proxy/:port/limits
        pre-sending checking that the parameter is correct
        """
        with pytest.raises(KeyError):
            self.client.limits({"hurray": "explosions"})

    def test_limits_key_no_value(self):
        """
        /proxy/:port/limits
        pre-sending checking that a parameter exists
        """
        with pytest.raises(KeyError):
            self.client.limits({})

    def test_limits_all_key_values(self):
        """
        /proxy/:port/limits
        can send all 3 at once based on the proxy implementation
        """
        limits = {"upstream_kbps": 320, "downstream_kbps": 560, "latency": 30}
        status_code = self.client.limits(limits)
        assert(status_code == 200)

    def test_rewrite(self):
        """
        /proxy/:port/rewrite

        """
        match = "/foo"
        replace = "/bar"
        status_code = self.client.rewrite_url(match, replace)
        assert(status_code == 200)

    def test_close(self):
        """
        /proxy/:port
        close the proxy port
        """
        status_code = self.client.close()
        assert(status_code == 200)
        status_code = self.client.close()
        assert(status_code == 500)

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

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

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

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

    def test_timeouts_invalid_timeouts(self):
        """
        /proxy/:port/timeout
        pre-sending checking that the parameter is correct
        """
        with pytest.raises(KeyError):
            self.client.timeouts({"hurray": "explosions"})

    def test_timeouts_key_no_value(self):
        """
        /proxy/:port/timeout
        pre-sending checking that a parameter exists
        """
        with pytest.raises(KeyError):
            self.client.timeouts({})

    def test_timeouts_all_key_values(self):
        """
        /proxy/:port/timeout
        can send all 3 at once based on the proxy implementation
        """
        timeouts = {"request": 2, "read": 2, "connection": 2, "dns": 3}
        status_code = self.client.timeouts(timeouts)
        assert(status_code == 200)

    def test_remap_hosts(self):
        """
        /proxy/:port/hosts
        """
        status_code = self.client.remap_hosts("example.com", "1.2.3.4")
        assert(status_code == 200)

    def test_wait_for_traffic_to_stop(self):
        """
        /proxy/:port/wait
        """
        status_code = self.client.wait_for_traffic_to_stop(2000, 10000)
        assert(status_code == 200)

    def test_clear_dns_cache(self):
        """
        /proxy/:port/dns/cache
        """
        status_code = self.client.clear_dns_cache()
        assert(status_code == 200)

    def test_rewrite_url(self):
        """
        /proxy/:port/rewrite
        """
        status_code = self.client.rewrite_url('http://www.facebook\.com', 'http://microsoft.com')
        assert(status_code == 200)

    def test_retry(self):
        """
        /proxy/:port/retry
        """
        status_code = self.client.retry(4)
        assert(status_code == 200)
コード例 #17
0
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
        """
        with pytest.raises(TypeError):
            self.client.headers(['foo'])

    def test_headers_content(self):
        """
        /proxy/:port/headers needs to take a dictionary
        and returns 200 when its successful
        """
        s = self.client.headers({'User-Agent': 'rubber ducks floating in a row'})
        assert(s == 200)

    def test_new_har(self):
        """
        /proxy/:port/har
        and returns 204 when creating a har with a particular name the first time
        and returns 200 and the previous har when creating one with the same name
        """
        status_code, har = self.client.new_har()
        assert(status_code == 204)
        assert(har is None)
        status_code, har = self.client.new_har()
        assert(status_code == 200)
        assert('log' in har)

    def test_new_har(self):
        """
        /proxy/:port/har
        and returns 204 when creating a har with a particular name the first time
        and returns 200 and the previous har when creating one with the same name
        """
        status_code, har = self.client.new_har("elephants")
        assert(status_code == 204)
        assert(har is None)
        status_code, har = self.client.new_har("elephants")
        assert(status_code == 200)
        assert('elephants' == har["log"]["pages"][0]['id'])

    def test_new_page_defaults(self):
        """
        /proxy/:port/pageRef
        adds a new page of 'Page N' when no page name is given
        """
        self.client.new_har()
        self.client.new_page()
        har = self.client.har
        assert(len(har["log"]["pages"]) == 2)
        assert(har["log"]["pages"][1]["id"] == "Page 2")

    def test_new_named_page(self):
        """
        /proxy/:port/pageRef
        adds a new page of 'buttress'
        """
        self.client.new_har()
        self.client.new_page('buttress')
        har = self.client.har
        assert(len(har["log"]["pages"]) == 2)
        assert(har["log"]["pages"][1]["id"] == "buttress")

    def test_single_whitelist(self):
        """
        /proxy/:port/whitelist
        adds a whitelist
        """
        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
        adds a blacklist
        """
        status_code = self.client.blacklist("http://www\\.facebook\\.com/.*", 200)
        assert(status_code == 200)

    def test_limits_invalid_key(self):
        """
        /proxy/:port/limits
        pre-sending checking that the parameter is correct
        """
        with pytest.raises(KeyError):
            self.client.limits({"hurray": "explosions"})

    def test_limits_key_no_value(self):
        """
        /proxy/:port/limits
        pre-sending checking that a parameter exists
        """
        with pytest.raises(KeyError):
            self.client.limits({})

    def test_limits_key_no_value(self):
        """
        /proxy/:port/limits
        can send all 3 at once based on the proxy implementation
        """
        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
        close the proxy port
        """
        status_code = self.client.close()
        assert(status_code == 200)
        status_code = self.client.close()
        assert(status_code == 500)