Intercept responses from standalone script


#1

Hi guys,
I’m creating automated website and REST API tests using unittest in conjunction with webdriver and I want to achieve the following:

  1. Setup webdriver to use proxy server
  2. Using webdriver, open a web browser and navigate through pages performing various actions
  3. Intercept received responses in JSON format and validate them (JSON schema, keys and values, data types)

Is mitmproxy suitable for achieving my goal?

What I tried is to (based on the flowbasic example)

def setUpClass(cls):

    # start proxy server
    cls.proxy_options = options.Options(cadir="~/.mitmproxy/")
    cls.proxy_config = ProxyConfig(cls.proxy_options)
    cls.proxy_server = ProxyServer(cls.proxy_config)
    cls.proxy = proxy.Proxy(cls.proxy_options, cls.proxy_server)
    cls.proxy.run()

    # configure proxy for webdriver
    cls.browser_profile = webdriver.FirefoxProfile()
    cls.browser_profile.set_preference("network.proxy.type", 1)
    cls.browser_profile.set_preference("network.proxy.http", "127.0.0.1")
    cls.browser_profile.set_preference("network.proxy.http_port", 8080)
    cls.browser_profile.update_preferences()

However, this approach has failed as proxy.run() blocks the thread.

How to implement it properly? Maybe it’s possible to connect to already running instance of mitmproxy and intercept responses not using inline scripts?

Any help would be highly appreciated