[2.7] bpo-35647: Fix path check in cookiejar by tirkarthi · Pull Request #13427 · python/cpython
Expand Up
@@ -646,6 +646,35 @@ def test_request_path(self):
req = Request("http://www.example.com")
self.assertEqual(request_path(req), "/")
def test_path_prefix_match(self): from cookielib import CookieJar, DefaultCookiePolicy from urllib2 import Request
pol = DefaultCookiePolicy() strict_ns_path_pol = DefaultCookiePolicy(strict_ns_set_path=True)
c = CookieJar(pol) base_url = "http://bar.com" interact_netscape(c, base_url, 'spam=eggs; Path=/foo') cookie = c._cookies['bar.com']['/foo']['spam']
for path, ok in [('/foo', True), ('/foo/', True), ('/foo/bar', True), ('/', False), ('/foobad/foo', False)]: url = '{0}{1}'.format(base_url, path) req = Request(url) h = interact_netscape(c, url) if ok: self.assertIn('spam=eggs', h, "cookie not set for {0}".format(path)) self.assertTrue(strict_ns_path_pol.set_ok_path(cookie, req)) else: self.assertNotIn('spam=eggs', h, "cookie set for {0}".format(path)) self.assertFalse(strict_ns_path_pol.set_ok_path(cookie, req))
def test_request_port(self): from urllib2 import Request from cookielib import request_port, DEFAULT_HTTP_PORT Expand Down
def test_path_prefix_match(self): from cookielib import CookieJar, DefaultCookiePolicy from urllib2 import Request
pol = DefaultCookiePolicy() strict_ns_path_pol = DefaultCookiePolicy(strict_ns_set_path=True)
c = CookieJar(pol) base_url = "http://bar.com" interact_netscape(c, base_url, 'spam=eggs; Path=/foo') cookie = c._cookies['bar.com']['/foo']['spam']
for path, ok in [('/foo', True), ('/foo/', True), ('/foo/bar', True), ('/', False), ('/foobad/foo', False)]: url = '{0}{1}'.format(base_url, path) req = Request(url) h = interact_netscape(c, url) if ok: self.assertIn('spam=eggs', h, "cookie not set for {0}".format(path)) self.assertTrue(strict_ns_path_pol.set_ok_path(cookie, req)) else: self.assertNotIn('spam=eggs', h, "cookie set for {0}".format(path)) self.assertFalse(strict_ns_path_pol.set_ok_path(cookie, req))
def test_request_port(self): from urllib2 import Request from cookielib import request_port, DEFAULT_HTTP_PORT Expand Down