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. pol = 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), ("http://foo/", ".local", True), ]: request = WrappedRequest(Request(url)) r = pol.domain_return_ok(domain, request) if ok: self.assert_(r) else: self.assert_(not r)
def test_domain_block(self): 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 = Response('http://www.acme.com/', headers=headers) c.extract_cookies(res, req) self.assertEquals(len(c), 0) p = pol.set_blocked_domains(["acme.com"]) c.extract_cookies(res, req) self.assertEquals(len(c), 1) c.clear() req = Request("http://www.roadrunner.net/") res = Response("http://www.roadrunner.net/", headers=headers) c.extract_cookies(res, req) self.assertEquals(len(c), 1) req = Request("http://www.roadrunner.net/") c.add_cookie_header(req) assert 'Cookie' in req.headers and 'Cookie2' in req.headers c.clear() pol.set_blocked_domains([".acme.com"]) c.extract_cookies(res, req) self.assertEquals(len(c), 1) # set a cookie with blocked domain... req = Request("http://www.acme.com/") res = Response("http://www.acme.com/", headers=headers) cookies = c.make_cookies(res, req) c.set_cookie(cookies[0]) self.assertEquals(len(c), 2) # ... and check is doesn't get returned c.add_cookie_header(req) assert 'Cookie' not in req.headers