How to use the mechanize.DefaultCookiePolicy 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_cookies.py View on Github external
def test_url_encoding(self):
        # Try some URL encodings of the PATHs.
        # (the behaviour here has changed from libwww-perl)
        from mechanize import CookieJar, DefaultCookiePolicy
        from mechanize.polyglot import is_py2

        c = CookieJar(DefaultCookiePolicy(rfc2965=True))
        url = "http://www.acme.com/foo%2f%25/%3c%3c%0Anew%C3%A5/%E5"
        if is_py2:
            # in python2 latin1 encoding is used
            url = url.replace('%C3%A5', '%E5')

        interact_2965(c, url,
                      "foo  =   bar; version    =   1")

        cookie = interact_2965(
            c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345",
            'bar=baz; path="/foo/"; version=1')
        version_re = re.compile(r'^\$version=\"?1\"?', re.I)
        self.assertIn('foo=bar', cookie)
        self.assertIsNotNone(version_re.search(cookie))

        cookie = interact_2965(
github python-mechanize / mechanize / test / test_cookies.py View on Github external
def test_intranet_domains_ns(self):
        from mechanize import CookieJar, DefaultCookiePolicy

        c = CookieJar(DefaultCookiePolicy(rfc2965=False))
        interact_netscape(c, "http://example/", "foo1=bar")
        cookie = interact_netscape(c, "http://example/",
                                   'foo2=bar; domain=.local')
        assert len(c) == 2
        assert cookie.find("foo1=bar") >= 0

        cookie = interact_netscape(c, "http://example/")
        assert cookie.find("foo2=bar") >= 0 and len(c) == 2
github python-mechanize / mechanize / test / test_cookies.py View on Github external
def test_ietf_example_2(self):
        from mechanize import CookieJar, DefaultCookiePolicy

        # 5.2  Example 2
        #
        # This example illustrates the effect of the Path attribute.  All detail
        # of request and response headers has been omitted.  Assume the user agent
        # has no stored cookies.

        c = CookieJar(DefaultCookiePolicy(rfc2965=True))

        # Imagine the user agent has received, in response to earlier requests,
        # the response headers
        #
        # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
        #         Path="/acme"
        #
        # and
        #
        # Set-Cookie2: Part_Number="Riding_Rocket_0023"; Version="1";
        #         Path="/acme/ammo"

        interact_2965(
            c, "http://www.acme.com/acme/ammo/specific",
            'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"',
            'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"')
github python-mechanize / mechanize / test / test_cookies.py View on Github external
# .bar.com, bar.com, no domain should
        #  all get accepted, as should .acme.com, acme.com and no domain for
        #  2-component domains like acme.com.
        from mechanize import CookieJar, DefaultCookiePolicy

        c = CookieJar()

        # two-component V0 domain is OK
        interact_netscape(c, "http://foo.net/", 'ns=bar')
        assert len(c) == 1
        assert c._cookies["foo.net"]["/"]["ns"].value == "bar"
        assert interact_netscape(c, "http://foo.net/") == "ns=bar"
        # *will* be returned to any other domain (unlike RFC 2965)...
        assert interact_netscape(c, "http://www.foo.net/") == "ns=bar"
        # ...unless requested otherwise
        pol = DefaultCookiePolicy(
            strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain)
        c.set_policy(pol)
        assert interact_netscape(c, "http://www.foo.net/") == ""

        # unlike RFC 2965, even explicit two-component domain is OK,
        # because .foo.net matches foo.net
        interact_netscape(c, "http://foo.net/foo/",
                          'spam1=eggs; domain=foo.net')
        # even if starts with a dot -- in NS rules, .foo.net matches foo.net!
        interact_netscape(c, "http://foo.net/foo/bar/",
                          'spam2=eggs; domain=.foo.net')
        assert len(c) == 3
        assert c._cookies[".foo.net"]["/foo"]["spam1"].value == "eggs"
        assert c._cookies[".foo.net"]["/foo/bar"]["spam2"].value == "eggs"
        assert interact_netscape(c, "http://foo.net/foo/bar/") == \
            "spam2=eggs; spam1=eggs; ns=bar"
github python-mechanize / mechanize / test / test_cookies.py View on Github external
def test_firefox3_cookiejar_iteration(self):
        try:
            from mechanize import Firefox3CookieJar
        except ImportError:
            pass
        else:
            from mechanize import DefaultCookiePolicy
            filename = self.mktemp()
            hide_experimental_warnings()
            try:
                cj = Firefox3CookieJar(
                    filename, policy=DefaultCookiePolicy(rfc2965=True))
            finally:
                reset_experimental_warnings()
            cj.connect()
            self._interact(cj)
            summary = "\n".join([str(cookie) for cookie in cj])
            self.assertEqual(
                summary, """\
github python-mechanize / mechanize / test / test_cookies.py View on Github external
def test_rfc2109_handling(self):
        # 2109 cookies have rfc2109 attr set correctly, and are handled
        # as 2965 or Netscape cookies depending on policy settings
        from mechanize import CookieJar, DefaultCookiePolicy

        for policy, version in [
            (DefaultCookiePolicy(), 0),
            (DefaultCookiePolicy(rfc2965=True), 1),
            (DefaultCookiePolicy(rfc2109_as_netscape=True), 0),
            (DefaultCookiePolicy(rfc2965=True, rfc2109_as_netscape=True), 0),
        ]:
            c = CookieJar(policy)
            interact_netscape(c, "http://www.example.com/", "ni=ni; Version=1")
            cookie = c._cookies["www.example.com"]["/"]["ni"]
            self.assertTrue(cookie.rfc2109)
            self.assertEqual(cookie.version, version)
github python-mechanize / mechanize / test / test_cookies.py View on Github external
def test_rejection(self):
        # Test rejection of Set-Cookie2 responses based on domain, path, port.
        from mechanize import LWPCookieJar, DefaultCookiePolicy

        pol = DefaultCookiePolicy(rfc2965=True)

        c = LWPCookieJar(policy=pol)

        # illegal domain (no embedded dots)
        cookie = interact_2965(c, "http://www.acme.com",
                               'foo=bar; domain=".com"; version=1')
        assert not c

        # legal domain
        cookie = interact_2965(c, "http://www.acme.com",
                               'ping=pong; domain="acme.com"; version=1')
        assert len(c) == 1

        # illegal domain (host prefix "www.a" contains a dot)
        cookie = interact_2965(c, "http://www.a.acme.com",
                               'whiz=bang; domain="acme.com"; version=1')
github python-mechanize / mechanize / test / test_cookies.py View on Github external
def test_intranet_domains_2965(self):
        # Test handling of local intranet hostnames without a dot.
        from mechanize import CookieJar, DefaultCookiePolicy

        c = CookieJar(DefaultCookiePolicy(rfc2965=True))
        interact_2965(c, "http://example/",
                      "foo1=bar; PORT; Discard; Version=1;")
        cookie = interact_2965(c, "http://example/",
                               'foo2=bar; domain=".local"; Version=1')
        assert cookie.find("foo1=bar") >= 0

        interact_2965(c, "http://example/", 'foo3=bar; Version=1')
        cookie = interact_2965(c, "http://example/")
        assert cookie.find("foo2=bar") >= 0 and len(c) == 3
github python-mechanize / mechanize / test / test_cookies.py View on Github external
def test_domain_return_ok(self):
        # test optimization: .domain_return_ok() should filter out most
        # domains in the CookieJar before we try to access them (because that
        # may require disk access -- in particular, with MSIECookieJar)
        # This is only a rough check for performance reasons, so it's not too
        # critical as long as it's sufficiently liberal.
        import mechanize
        pol = mechanize.DefaultCookiePolicy()
        for url, domain, ok in [
            ("http://foo.bar.com/", "blah.com", False),
            ("http://foo.bar.com/", "rhubarb.blah.com", False),
            ("http://foo.bar.com/", "rhubarb.foo.bar.com", False),
            ("http://foo.bar.com/", ".foo.bar.com", True),
            ("http://foo.bar.com/", "foo.bar.com", True),
            ("http://foo.bar.com/", ".bar.com", True),
            ("http://foo.bar.com/", "com", True),
            ("http://foo.com/", "rhubarb.foo.com", False),
            ("http://foo.com/", ".foo.com", True),
            ("http://foo.com/", "foo.com", True),
            ("http://foo.com/", "com", True),
            ("http://foo/", "rhubarb.foo", False),
            ("http://foo/", ".foo", True),
            ("http://foo/", "foo", True),
            ("http://foo/", "foo.local", True),
github python-mechanize / mechanize / test / test_cookies.py View on Github external
def test_domain_block(self):
        from mechanize import CookieJar, DefaultCookiePolicy

        # import logging
        # logging.getLogger("mechanize").setLevel(logging.DEBUG)

        pol = DefaultCookiePolicy(rfc2965=True, blocked_domains=[".acme.com"])
        c = CookieJar(policy=pol)
        headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]

        req = Request("http://www.acme.com/")
        res = FakeResponse(headers, "http://www.acme.com/")
        c.extract_cookies(res, req)
        assert len(c) == 0

        pol.set_blocked_domains(["acme.com"])
        c.extract_cookies(res, req)
        assert len(c) == 1

        c.clear()
        req = Request("http://www.roadrunner.net/")
        res = FakeResponse(headers, "http://www.roadrunner.net/")
        c.extract_cookies(res, req)