# HG changeset patch # Parent 0a47d0cd92e29731059972f5985cc7d073754adb diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -1071,28 +1071,60 @@ class URLopener_Tests(unittest.TestCase) # # def testTimeoutValue(self): # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [], # timeout=30) # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) # ftp.close() +class Request_Tests(unittest.TestCase): + + def verify_response(self, resp, expected_type, expected_method, + expected_host, expected_selector, expected_data=None): + self.assertEqual(resp.type, expected_type) + self.assertEqual(resp.get_method(), expected_method) + self.assertEqual(resp.host, expected_host) + self.assertEqual(resp.selector, expected_selector) + if expected_data: + self.assertEqual(resp.data, expected_data) + + def test_a_wrapped_url(self): + resp = urllib.request.Request('') + self.verify_response(resp, 'http', 'GET', 'python.org', '') + + def test_a_simple_url(self): + resp = urllib.request.Request('http://python.org') + self.verify_response(resp, 'http', 'GET', 'python.org', '') + + resp = urllib.request.Request('http://python.org/') + self.verify_response(resp, 'http', 'GET', 'python.org', '/') + + resp = urllib.request.Request('http://python.org/dev') + self.verify_response(resp, 'http', 'GET', 'python.org', '/dev') + + def test_a_url_containing_a_fragment(self): + resp = urllib.request.Request('http://python.org/#fun=true') + self.verify_response(resp, 'http', 'GET', 'python.org', '/') + + resp = urllib.request.Request('http://python.org/?qs=true#fun=true') + self.verify_response(resp, 'http', 'GET', 'python.org', '/?qs=true') + def test_main(): support.run_unittest( urlopen_FileTests, urlopen_HttpTests, urlretrieve_FileTests, ProxyTests, QuotingTests, UnquotingTests, urlencode_Tests, Pathname_Tests, Utility_Tests, URLopener_Tests, + Request_Tests, #FTPWrapperTests, ) - if __name__ == '__main__': test_main() diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -94,17 +94,17 @@ import re import socket import sys import time from urllib.error import URLError, HTTPError, ContentTooShortError from urllib.parse import ( urlparse, urlsplit, urljoin, unwrap, quote, unquote, splittype, splithost, splitport, splituser, splitpasswd, - splitattr, splitquery, splitvalue, to_bytes, urlunparse) + splitattr, splitquery, splitvalue, splittag, to_bytes, urlunparse) from urllib.response import addinfourl, addclosehook # check for SSL try: import ssl except: _have_ssl = False else: @@ -158,16 +158,17 @@ def request_host(request): return host.lower() class Request: def __init__(self, url, data=None, headers={}, origin_req_host=None, unverifiable=False): # unwrap('') --> 'type://host/path' self.full_url = unwrap(url) + self.full_url, garbage = splittag(self.full_url) self.data = data self.headers = {} self._tunnel_host = None for key, value in headers.items(): self.add_header(key, value) self.unredirected_hdrs = {} if origin_req_host is None: origin_req_host = request_host(self)