How to use the mechanize.OpenerDirector function in mechanize

To help you get started, we’ve selected a few mechanize examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def test_add_non_handler(self):
        class NonHandler(object):
            pass

        self.assertRaises(TypeError, OpenerDirector().add_handler,
                          NonHandler())
github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def test_handler_order(self):
        o = OpenerDirector()
        handlers = []
        for meths, handler_order in [
            ([("http_open", "return self")], 500),
            (["http_open"], 0),
        ]:

            class MockHandlerSubclass(MockHandler):
                pass

            h = MockHandlerSubclass(meths)
            h.handler_order = handler_order
            handlers.append(h)
            o.add_handler(h)

        o.open("http://example.com/")
        # handlers called in reverse order, thanks to their sort order
github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def test_proxy_no_proxy(self):
        self.monkey_patch_environ("no_proxy", "python.org")
        o = OpenerDirector()
        ph = mechanize.ProxyHandler(dict(http="proxy.example.com"))
        o.add_handler(ph)
        req = Request("http://www.perl.org/")
        self.assertEqual(req.get_host(), "www.perl.org")
        o.open(req)
        self.assertEqual(req.get_host(), "proxy.example.com")
        req = Request("http://www.python.org")
        self.assertEqual(req.get_host(), "www.python.org")
        o.open(req)
        if sys.version_info >= (2, 6):
            # no_proxy environment variable not supported in python 2.5
            self.assertEqual(req.get_host(), "www.python.org")
github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def build_test_opener(*handler_instances):
    opener = OpenerDirector()
    for h in handler_instances:
        opener.add_handler(h)
    return opener
github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def test_http_error(self):
        # XXX http_error_default
        # http errors are a special case
        o = OpenerDirector()
        meth_spec = [
            [("http_open", "error 302")],
            [("http_error_400", "raise"), "http_open"],
            [("http_error_302", "return response"), "http_error_303",
             "http_error"],
            [("http_error_302")],
        ]
        handlers = add_ordered_mock_handlers(o, meth_spec)

        req = Request("http://example.com/")
        o.open(req)
        assert len(o.calls) == 2
        ignore = object()
        calls = [(handlers[0], "http_open", (req, )), (
            handlers[2], "http_error_302", (req, ignore, 302, "", {}))]
        for expected, got in zip(calls, o.calls):
github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def test_badly_named_methods(self):
        # test work-around for three methods that accidentally follow the
        # naming conventions for handler methods
        # (*_open() / *_request() / *_response())

        # These used to call the accidentally-named methods, causing a
        # TypeError in real code; here, returning self from these mock
        # methods would either cause no exception, or AttributeError.

        from mechanize import URLError

        o = OpenerDirector()
        meth_spec = [
            [("do_open", "return self"), ("proxy_open", "return self")],
            [("redirect_request", "return self")],
        ]
        add_ordered_mock_handlers(o, meth_spec)
        o.add_handler(mechanize.UnknownHandler())
        for scheme in "do", "proxy", "redirect":
            self.assertRaises(URLError, o.open, scheme + "://example.com/")
github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def test_basic_auth(self, quote_char='"'):
        opener = OpenerDirector()
        password_manager = MockPasswordManager()
        auth_handler = mechanize.HTTPBasicAuthHandler(password_manager)
        realm = "ACME Widget Store"
        http_handler = MockHTTPHandler(
            401, 'WWW-Authenticate: Basic realm=%s%s%s\r\n\r\n' %
            (quote_char, realm, quote_char))
        opener.add_handler(auth_handler)
        opener.add_handler(http_handler)
        self._test_basic_auth(
            opener,
            auth_handler,
            "Authorization",
            realm,
            http_handler,
            password_manager,
            "http://acme.example.com/protected",
github python-mechanize / mechanize / test / test_opener.py View on Github external
def __init__(self, content_length=None):
                mechanize.OpenerDirector.__init__(self)
                self.calls = []
                self.block_size = mechanize.OpenerDirector.BLOCK_SIZE
                self.nr_blocks = 2.5
                self.data = int((self.block_size / 8) *
                                self.nr_blocks) * b"01234567"
                self.total_size = len(self.data)
                self._content_length = content_length
github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def test_proxy_custom_proxy_bypass(self):
        self.monkey_patch_environ("no_proxy",
                                  mechanize._testcase.MonkeyPatcher.Unset)

        def proxy_bypass(hostname):
            return hostname == "noproxy.com"

        o = OpenerDirector()
        ph = mechanize.ProxyHandler(
            dict(http="proxy.example.com"), proxy_bypass=proxy_bypass)

        def is_proxied(url):
            o.add_handler(ph)
            req = Request(url)
            o.open(req)
            return req.has_proxy()

        self.assertTrue(is_proxied("http://example.com"))
        self.assertFalse(is_proxied("http://noproxy.com"))
github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def test_proxy_https_proxy_authorization(self):
        o = OpenerDirector()
        ph = mechanize.ProxyHandler(dict(https='proxy.example.com:3128'))
        o.add_handler(ph)
        https_handler = MockHTTPSHandler()
        o.add_handler(https_handler)
        req = Request("https://www.example.com/")
        req.add_header("Proxy-Authorization", "FooBar")
        req.add_header("User-Agent", "Grail")
        self.assertEqual(req.get_host(), "www.example.com")
        self.assertIsNone(req._tunnel_host)
        o.open(req)
        # Verify Proxy-Authorization gets tunneled to request.
        # httpsconn req_headers do not have the Proxy-Authorization header but
        # the req will have.
        self.assertNotIn(
            ("Proxy-Authorization", "FooBar"),
            https_handler.httpconn.req_headers)