# HG changeset patch # Parent eede126ee89a7ea834f62425232e97bb550ac50b 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 @@ -992,16 +992,33 @@ class URLopener_Tests(unittest.TestCase) self.assertEqual(DummyURLopener().open( 'spam://example/ /'),'//example/%20/') # test the safe characters are not quoted by urlopen self.assertEqual(DummyURLopener().open( "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"), "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/") + +class Request_Tests(unittest.TestCase): + + def test_HEAD(self): + req = urllib.request.Request("http://example.org/", method="HEAD") + self.assertEquals(req.get_method(), "HEAD") + + def test_PUT(self): + req = urllib.request.Request("http://example.org/", {}, method="PUT") + self.assertEquals(req.get_method(), "PUT") + + def test_DELETE(self): + req = urllib.request.Request("http://example.org/", {}, + method="DELETE") + self.assertEquals(req.get_method(), "DELETE") + + # Just commented them out. # Can't really tell why keep failing in windows and sparc. # Everywhere else they work ok, but on those machines, someteimes # fail in one of the tests, sometimes in other. I have a linux, and # the tests go ok. # If anybody has one of the problematic enviroments, please help! # . Facundo # @@ -1071,28 +1088,28 @@ 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() - 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 @@ -154,42 +154,45 @@ def request_host(request): host = request.get_header("Host", "") # remove port, if present host = _cut_port_re.sub("", host, 1) return host.lower() class Request: - def __init__(self, url, data=None, headers={}, - origin_req_host=None, unverifiable=False): + def __init__(self, url, data=None, headers={}, origin_req_host=None, + unverifiable=False, method=None): # unwrap('') --> 'type://host/path' self.full_url = unwrap(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) self.origin_req_host = origin_req_host self.unverifiable = unverifiable + self.method = method self._parse() def _parse(self): self.type, rest = splittype(self.full_url) if self.type is None: raise ValueError("unknown url type: %s" % self.full_url) self.host, self.selector = splithost(rest) if self.host: self.host = unquote(self.host) def get_method(self): - if self.data is not None: + if self.method: + return self.method + elif self.data is not None: return "POST" else: return "GET" # Begin deprecated methods def add_data(self, data): self.data = data