diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 23631f657fe..87030819c5d 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -1237,6 +1237,16 @@ class RequestTests(unittest.TestCase): self.assertEqual("www.python.org", self.get.get_origin_req_host()) self.assertEqual("www.perl.org", self.get.get_host()) + def test_wrapped_url(self): + req = Request("") + self.assertEqual("www.python.org", req.get_host()) + + def test_urlwith_fragment(self): + req = Request("http://www.python.org/?qs=query#fragment=true") + self.assertEqual("/?qs=query", req.get_selector()) + req = Request("http://www.python.org/#fun=true") + self.assertEqual("/", req.get_selector()) + def test_main(verbose=None): from test import test_urllib2 diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py index d2762dcf029..4da38eff24d 100644 --- a/Lib/test/test_urllib2net.py +++ b/Lib/test/test_urllib2net.py @@ -154,6 +154,13 @@ class OtherNetworkTests(unittest.TestCase): ## self._test_urls(urls, self._extra_handlers()+[bauth, dauth]) + def test_urlwithfrag(self): + urlwith_frag = "http://docs.python.org/glossary.html#glossary" + req = urllib2.Request(urlwith_frag) + res = urllib2.urlopen(req) + self.assertEqual(res.geturl(), + "http://docs.python.org/glossary.html") + def _test_urls(self, urls, handlers, retry=True): import time import logging diff --git a/Lib/urllib2.py b/Lib/urllib2.py index f6f7be9092a..d0e81a8084f 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -109,7 +109,7 @@ except ImportError: from StringIO import StringIO from urllib import (unwrap, unquote, splittype, splithost, quote, - addinfourl, splitport, + addinfourl, splitport, splittag, splitattr, ftpwrapper, splituser, splitpasswd, splitvalue) # support for FileHandler, proxies via environment variables @@ -190,6 +190,7 @@ class Request: origin_req_host=None, unverifiable=False): # unwrap('') --> 'type://host/path' self.__original = unwrap(url) + self.__original, fragment = splittag(self.__original) self.type = None # self.__r_type is what's left after doing the splittype self.host = None