diff options
author | Senthil Kumaran <senthil@uthcode.com> | 2013-05-24 16:14:12 (GMT) |
---|---|---|
committer | Senthil Kumaran <senthil@uthcode.com> | 2013-05-24 16:14:12 (GMT) |
commit | 8307075ce870375f1d1f7344972f78f9c42b39e8 (patch) | |
tree | 19fd346ea71cf5fdfed95c5b99a96ffc4cce973a /Lib | |
parent | dc3e6cc452a2a4409a4d12804fab4e474abbf9ff (diff) | |
download | cpython-8307075ce870375f1d1f7344972f78f9c42b39e8.zip cpython-8307075ce870375f1d1f7344972f78f9c42b39e8.tar.gz cpython-8307075ce870375f1d1f7344972f78f9c42b39e8.tar.bz2 |
Fix #17272 - Make Request.full_url and Request.get_full_url return same result under all circumstances.
Document the change of Request.full_url to a property.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_urllib2.py | 15 | ||||
-rw-r--r-- | Lib/test/test_urllib2net.py | 8 | ||||
-rw-r--r-- | Lib/urllib/request.py | 4 |
3 files changed, 25 insertions, 2 deletions
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index b4f940c..b3659f4 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -11,6 +11,7 @@ import urllib.request # The proxy bypass method imported below has logic specific to the OSX # proxy config data structure but is testable on all platforms. from urllib.request import Request, OpenerDirector, _proxy_bypass_macosx_sysconf +from urllib.parse import urlparse import urllib.error # XXX @@ -919,7 +920,13 @@ class HandlerTests(unittest.TestCase): r = Request('http://example.com') for url in urls: r.full_url = url + parsed = urlparse(url) + self.assertEqual(r.get_full_url(), url) + # full_url setter uses splittag to split into components. + # splittag sets the fragment as None while urlparse sets it to '' + self.assertEqual(r.fragment or '', parsed.fragment) + self.assertEqual(urlparse(r.get_full_url()).query, parsed.query) def test_full_url_deleter(self): r = Request('http://www.example.com') @@ -1537,6 +1544,14 @@ class RequestTests(unittest.TestCase): req = Request(url) self.assertEqual(req.get_full_url(), url) + def test_url_fullurl_get_full_url(self): + urls = ['http://docs.python.org', + 'http://docs.python.org/library/urllib2.html#OK', + 'http://www.python.org/?qs=query#fragment=true' ] + for url in urls: + req = Request(url) + self.assertEqual(req.get_full_url(), req.full_url) + def test_main(verbose=None): from test import test_urllib2 support.run_doctest(test_urllib2, verbose) diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py index e276d2e..b674be0 100644 --- a/Lib/test/test_urllib2net.py +++ b/Lib/test/test_urllib2net.py @@ -164,6 +164,14 @@ class OtherNetworkTests(unittest.TestCase): self.assertEqual(res.geturl(), "http://docs.python.org/2/glossary.html#glossary") + def test_redirect_url_withfrag(self): + redirect_url_with_frag = "http://bitly.com/urllibredirecttest" + with support.transient_internet(redirect_url_with_frag): + req = urllib.request.Request(redirect_url_with_frag) + res = urllib.request.urlopen(req) + self.assertEqual(res.geturl(), + "http://docs.python.org/3.4/glossary.html#term-global-interpreter-lock") + def test_custom_headers(self): url = "http://www.example.com" with support.transient_internet(url): diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 3258102..fdb1ec8 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -275,6 +275,8 @@ class Request: @property def full_url(self): + if self.fragment: + return '{}#{}'.format(self._full_url, self.fragment) return self._full_url @full_url.setter @@ -326,8 +328,6 @@ class Request: return "GET" def get_full_url(self): - if self.fragment: - return '{}#{}'.format(self.full_url, self.fragment) return self.full_url def set_proxy(self, host, type): |