test_urllib2.py 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  1. import unittest
  2. from test import support
  3. from test.support import os_helper
  4. from test.support import socket_helper
  5. from test.support import warnings_helper
  6. from test import test_urllib
  7. import os
  8. import io
  9. import socket
  10. import array
  11. import sys
  12. import tempfile
  13. import subprocess
  14. import urllib.request
  15. # The proxy bypass method imported below has logic specific to the OSX
  16. # proxy config data structure but is testable on all platforms.
  17. from urllib.request import (Request, OpenerDirector, HTTPBasicAuthHandler,
  18. HTTPPasswordMgrWithPriorAuth, _parse_proxy,
  19. _proxy_bypass_macosx_sysconf,
  20. AbstractDigestAuthHandler)
  21. from urllib.parse import urlparse
  22. import urllib.error
  23. import http.client
  24. support.requires_working_socket(module=True)
  25. # XXX
  26. # Request
  27. # CacheFTPHandler (hard to write)
  28. # parse_keqv_list, parse_http_list, HTTPDigestAuthHandler
  29. class TrivialTests(unittest.TestCase):
  30. def test___all__(self):
  31. # Verify which names are exposed
  32. for module in 'request', 'response', 'parse', 'error', 'robotparser':
  33. context = {}
  34. exec('from urllib.%s import *' % module, context)
  35. del context['__builtins__']
  36. if module == 'request' and os.name == 'nt':
  37. u, p = context.pop('url2pathname'), context.pop('pathname2url')
  38. self.assertEqual(u.__module__, 'nturl2path')
  39. self.assertEqual(p.__module__, 'nturl2path')
  40. for k, v in context.items():
  41. self.assertEqual(v.__module__, 'urllib.%s' % module,
  42. "%r is exposed in 'urllib.%s' but defined in %r" %
  43. (k, module, v.__module__))
  44. def test_trivial(self):
  45. # A couple trivial tests
  46. # clear _opener global variable
  47. self.addCleanup(urllib.request.urlcleanup)
  48. self.assertRaises(ValueError, urllib.request.urlopen, 'bogus url')
  49. # XXX Name hacking to get this to work on Windows.
  50. fname = os.path.abspath(urllib.request.__file__).replace(os.sep, '/')
  51. if os.name == 'nt':
  52. file_url = "file:///%s" % fname
  53. else:
  54. file_url = "file://%s" % fname
  55. with urllib.request.urlopen(file_url) as f:
  56. f.read()
  57. def test_parse_http_list(self):
  58. tests = [
  59. ('a,b,c', ['a', 'b', 'c']),
  60. ('path"o,l"og"i"cal, example', ['path"o,l"og"i"cal', 'example']),
  61. ('a, b, "c", "d", "e,f", g, h',
  62. ['a', 'b', '"c"', '"d"', '"e,f"', 'g', 'h']),
  63. ('a="b\\"c", d="e\\,f", g="h\\\\i"',
  64. ['a="b"c"', 'd="e,f"', 'g="h\\i"'])]
  65. for string, list in tests:
  66. self.assertEqual(urllib.request.parse_http_list(string), list)
  67. def test_URLError_reasonstr(self):
  68. err = urllib.error.URLError('reason')
  69. self.assertIn(err.reason, str(err))
  70. class RequestHdrsTests(unittest.TestCase):
  71. def test_request_headers_dict(self):
  72. """
  73. The Request.headers dictionary is not a documented interface. It
  74. should stay that way, because the complete set of headers are only
  75. accessible through the .get_header(), .has_header(), .header_items()
  76. interface. However, .headers pre-dates those methods, and so real code
  77. will be using the dictionary.
  78. The introduction in 2.4 of those methods was a mistake for the same
  79. reason: code that previously saw all (urllib2 user)-provided headers in
  80. .headers now sees only a subset.
  81. """
  82. url = "http://example.com"
  83. self.assertEqual(Request(url,
  84. headers={"Spam-eggs": "blah"}
  85. ).headers["Spam-eggs"], "blah")
  86. self.assertEqual(Request(url,
  87. headers={"spam-EggS": "blah"}
  88. ).headers["Spam-eggs"], "blah")
  89. def test_request_headers_methods(self):
  90. """
  91. Note the case normalization of header names here, to
  92. .capitalize()-case. This should be preserved for
  93. backwards-compatibility. (In the HTTP case, normalization to
  94. .title()-case is done by urllib2 before sending headers to
  95. http.client).
  96. Note that e.g. r.has_header("spam-EggS") is currently False, and
  97. r.get_header("spam-EggS") returns None, but that could be changed in
  98. future.
  99. Method r.remove_header should remove items both from r.headers and
  100. r.unredirected_hdrs dictionaries
  101. """
  102. url = "http://example.com"
  103. req = Request(url, headers={"Spam-eggs": "blah"})
  104. self.assertTrue(req.has_header("Spam-eggs"))
  105. self.assertEqual(req.header_items(), [('Spam-eggs', 'blah')])
  106. req.add_header("Foo-Bar", "baz")
  107. self.assertEqual(sorted(req.header_items()),
  108. [('Foo-bar', 'baz'), ('Spam-eggs', 'blah')])
  109. self.assertFalse(req.has_header("Not-there"))
  110. self.assertIsNone(req.get_header("Not-there"))
  111. self.assertEqual(req.get_header("Not-there", "default"), "default")
  112. req.remove_header("Spam-eggs")
  113. self.assertFalse(req.has_header("Spam-eggs"))
  114. req.add_unredirected_header("Unredirected-spam", "Eggs")
  115. self.assertTrue(req.has_header("Unredirected-spam"))
  116. req.remove_header("Unredirected-spam")
  117. self.assertFalse(req.has_header("Unredirected-spam"))
  118. def test_password_manager(self):
  119. mgr = urllib.request.HTTPPasswordMgr()
  120. add = mgr.add_password
  121. find_user_pass = mgr.find_user_password
  122. add("Some Realm", "http://example.com/", "joe", "password")
  123. add("Some Realm", "http://example.com/ni", "ni", "ni")
  124. add("Some Realm", "http://c.example.com:3128", "3", "c")
  125. add("Some Realm", "d.example.com", "4", "d")
  126. add("Some Realm", "e.example.com:3128", "5", "e")
  127. # For the same realm, password set the highest path is the winner.
  128. self.assertEqual(find_user_pass("Some Realm", "example.com"),
  129. ('joe', 'password'))
  130. self.assertEqual(find_user_pass("Some Realm", "http://example.com/ni"),
  131. ('joe', 'password'))
  132. self.assertEqual(find_user_pass("Some Realm", "http://example.com"),
  133. ('joe', 'password'))
  134. self.assertEqual(find_user_pass("Some Realm", "http://example.com/"),
  135. ('joe', 'password'))
  136. self.assertEqual(find_user_pass("Some Realm",
  137. "http://example.com/spam"),
  138. ('joe', 'password'))
  139. self.assertEqual(find_user_pass("Some Realm",
  140. "http://example.com/spam/spam"),
  141. ('joe', 'password'))
  142. # You can have different passwords for different paths.
  143. add("c", "http://example.com/foo", "foo", "ni")
  144. add("c", "http://example.com/bar", "bar", "nini")
  145. add("c", "http://example.com/foo/bar", "foobar", "nibar")
  146. self.assertEqual(find_user_pass("c", "http://example.com/foo"),
  147. ('foo', 'ni'))
  148. self.assertEqual(find_user_pass("c", "http://example.com/bar"),
  149. ('bar', 'nini'))
  150. self.assertEqual(find_user_pass("c", "http://example.com/foo/"),
  151. ('foo', 'ni'))
  152. self.assertEqual(find_user_pass("c", "http://example.com/foo/bar"),
  153. ('foo', 'ni'))
  154. self.assertEqual(find_user_pass("c", "http://example.com/foo/baz"),
  155. ('foo', 'ni'))
  156. self.assertEqual(find_user_pass("c", "http://example.com/foobar"),
  157. (None, None))
  158. add("c", "http://example.com/baz/", "baz", "ninini")
  159. self.assertEqual(find_user_pass("c", "http://example.com/baz"),
  160. (None, None))
  161. self.assertEqual(find_user_pass("c", "http://example.com/baz/"),
  162. ('baz', 'ninini'))
  163. self.assertEqual(find_user_pass("c", "http://example.com/baz/bar"),
  164. ('baz', 'ninini'))
  165. # For the same path, newer password should be considered.
  166. add("b", "http://example.com/", "first", "blah")
  167. add("b", "http://example.com/", "second", "spam")
  168. self.assertEqual(find_user_pass("b", "http://example.com/"),
  169. ('second', 'spam'))
  170. # No special relationship between a.example.com and example.com:
  171. add("a", "http://example.com", "1", "a")
  172. self.assertEqual(find_user_pass("a", "http://example.com/"),
  173. ('1', 'a'))
  174. self.assertEqual(find_user_pass("a", "http://a.example.com/"),
  175. (None, None))
  176. # Ports:
  177. self.assertEqual(find_user_pass("Some Realm", "c.example.com"),
  178. (None, None))
  179. self.assertEqual(find_user_pass("Some Realm", "c.example.com:3128"),
  180. ('3', 'c'))
  181. self.assertEqual(
  182. find_user_pass("Some Realm", "http://c.example.com:3128"),
  183. ('3', 'c'))
  184. self.assertEqual(find_user_pass("Some Realm", "d.example.com"),
  185. ('4', 'd'))
  186. self.assertEqual(find_user_pass("Some Realm", "e.example.com:3128"),
  187. ('5', 'e'))
  188. def test_password_manager_default_port(self):
  189. """
  190. The point to note here is that we can't guess the default port if
  191. there's no scheme. This applies to both add_password and
  192. find_user_password.
  193. """
  194. mgr = urllib.request.HTTPPasswordMgr()
  195. add = mgr.add_password
  196. find_user_pass = mgr.find_user_password
  197. add("f", "http://g.example.com:80", "10", "j")
  198. add("g", "http://h.example.com", "11", "k")
  199. add("h", "i.example.com:80", "12", "l")
  200. add("i", "j.example.com", "13", "m")
  201. self.assertEqual(find_user_pass("f", "g.example.com:100"),
  202. (None, None))
  203. self.assertEqual(find_user_pass("f", "g.example.com:80"),
  204. ('10', 'j'))
  205. self.assertEqual(find_user_pass("f", "g.example.com"),
  206. (None, None))
  207. self.assertEqual(find_user_pass("f", "http://g.example.com:100"),
  208. (None, None))
  209. self.assertEqual(find_user_pass("f", "http://g.example.com:80"),
  210. ('10', 'j'))
  211. self.assertEqual(find_user_pass("f", "http://g.example.com"),
  212. ('10', 'j'))
  213. self.assertEqual(find_user_pass("g", "h.example.com"), ('11', 'k'))
  214. self.assertEqual(find_user_pass("g", "h.example.com:80"), ('11', 'k'))
  215. self.assertEqual(find_user_pass("g", "http://h.example.com:80"),
  216. ('11', 'k'))
  217. self.assertEqual(find_user_pass("h", "i.example.com"), (None, None))
  218. self.assertEqual(find_user_pass("h", "i.example.com:80"), ('12', 'l'))
  219. self.assertEqual(find_user_pass("h", "http://i.example.com:80"),
  220. ('12', 'l'))
  221. self.assertEqual(find_user_pass("i", "j.example.com"), ('13', 'm'))
  222. self.assertEqual(find_user_pass("i", "j.example.com:80"),
  223. (None, None))
  224. self.assertEqual(find_user_pass("i", "http://j.example.com"),
  225. ('13', 'm'))
  226. self.assertEqual(find_user_pass("i", "http://j.example.com:80"),
  227. (None, None))
  228. class MockOpener:
  229. addheaders = []
  230. def open(self, req, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
  231. self.req, self.data, self.timeout = req, data, timeout
  232. def error(self, proto, *args):
  233. self.proto, self.args = proto, args
  234. class MockFile:
  235. def read(self, count=None):
  236. pass
  237. def readline(self, count=None):
  238. pass
  239. def close(self):
  240. pass
  241. class MockHeaders(dict):
  242. def getheaders(self, name):
  243. return list(self.values())
  244. class MockResponse(io.StringIO):
  245. def __init__(self, code, msg, headers, data, url=None):
  246. io.StringIO.__init__(self, data)
  247. self.code, self.msg, self.headers, self.url = code, msg, headers, url
  248. def info(self):
  249. return self.headers
  250. def geturl(self):
  251. return self.url
  252. class MockCookieJar:
  253. def add_cookie_header(self, request):
  254. self.ach_req = request
  255. def extract_cookies(self, response, request):
  256. self.ec_req, self.ec_r = request, response
  257. class FakeMethod:
  258. def __init__(self, meth_name, action, handle):
  259. self.meth_name = meth_name
  260. self.handle = handle
  261. self.action = action
  262. def __call__(self, *args):
  263. return self.handle(self.meth_name, self.action, *args)
  264. class MockHTTPResponse(io.IOBase):
  265. def __init__(self, fp, msg, status, reason):
  266. self.fp = fp
  267. self.msg = msg
  268. self.status = status
  269. self.reason = reason
  270. self.code = 200
  271. def read(self):
  272. return ''
  273. def info(self):
  274. return {}
  275. def geturl(self):
  276. return self.url
  277. class MockHTTPClass:
  278. def __init__(self):
  279. self.level = 0
  280. self.req_headers = []
  281. self.data = None
  282. self.raise_on_endheaders = False
  283. self.sock = None
  284. self._tunnel_headers = {}
  285. def __call__(self, host, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
  286. self.host = host
  287. self.timeout = timeout
  288. return self
  289. def set_debuglevel(self, level):
  290. self.level = level
  291. def set_tunnel(self, host, port=None, headers=None):
  292. self._tunnel_host = host
  293. self._tunnel_port = port
  294. if headers:
  295. self._tunnel_headers = headers
  296. else:
  297. self._tunnel_headers.clear()
  298. def request(self, method, url, body=None, headers=None, *,
  299. encode_chunked=False):
  300. self.method = method
  301. self.selector = url
  302. if headers is not None:
  303. self.req_headers += headers.items()
  304. self.req_headers.sort()
  305. if body:
  306. self.data = body
  307. self.encode_chunked = encode_chunked
  308. if self.raise_on_endheaders:
  309. raise OSError()
  310. def getresponse(self):
  311. return MockHTTPResponse(MockFile(), {}, 200, "OK")
  312. def close(self):
  313. pass
  314. class MockHandler:
  315. # useful for testing handler machinery
  316. # see add_ordered_mock_handlers() docstring
  317. handler_order = 500
  318. def __init__(self, methods):
  319. self._define_methods(methods)
  320. def _define_methods(self, methods):
  321. for spec in methods:
  322. if len(spec) == 2:
  323. name, action = spec
  324. else:
  325. name, action = spec, None
  326. meth = FakeMethod(name, action, self.handle)
  327. setattr(self.__class__, name, meth)
  328. def handle(self, fn_name, action, *args, **kwds):
  329. self.parent.calls.append((self, fn_name, args, kwds))
  330. if action is None:
  331. return None
  332. elif action == "return self":
  333. return self
  334. elif action == "return response":
  335. res = MockResponse(200, "OK", {}, "")
  336. return res
  337. elif action == "return request":
  338. return Request("http://blah/")
  339. elif action.startswith("error"):
  340. code = action[action.rfind(" ")+1:]
  341. try:
  342. code = int(code)
  343. except ValueError:
  344. pass
  345. res = MockResponse(200, "OK", {}, "")
  346. return self.parent.error("http", args[0], res, code, "", {})
  347. elif action == "raise":
  348. raise urllib.error.URLError("blah")
  349. assert False
  350. def close(self):
  351. pass
  352. def add_parent(self, parent):
  353. self.parent = parent
  354. self.parent.calls = []
  355. def __lt__(self, other):
  356. if not hasattr(other, "handler_order"):
  357. # No handler_order, leave in original order. Yuck.
  358. return True
  359. return self.handler_order < other.handler_order
  360. def add_ordered_mock_handlers(opener, meth_spec):
  361. """Create MockHandlers and add them to an OpenerDirector.
  362. meth_spec: list of lists of tuples and strings defining methods to define
  363. on handlers. eg:
  364. [["http_error", "ftp_open"], ["http_open"]]
  365. defines methods .http_error() and .ftp_open() on one handler, and
  366. .http_open() on another. These methods just record their arguments and
  367. return None. Using a tuple instead of a string causes the method to
  368. perform some action (see MockHandler.handle()), eg:
  369. [["http_error"], [("http_open", "return request")]]
  370. defines .http_error() on one handler (which simply returns None), and
  371. .http_open() on another handler, which returns a Request object.
  372. """
  373. handlers = []
  374. count = 0
  375. for meths in meth_spec:
  376. class MockHandlerSubclass(MockHandler):
  377. pass
  378. h = MockHandlerSubclass(meths)
  379. h.handler_order += count
  380. h.add_parent(opener)
  381. count = count + 1
  382. handlers.append(h)
  383. opener.add_handler(h)
  384. return handlers
  385. def build_test_opener(*handler_instances):
  386. opener = OpenerDirector()
  387. for h in handler_instances:
  388. opener.add_handler(h)
  389. return opener
  390. class MockHTTPHandler(urllib.request.BaseHandler):
  391. # useful for testing redirections and auth
  392. # sends supplied headers and code as first response
  393. # sends 200 OK as second response
  394. def __init__(self, code, headers):
  395. self.code = code
  396. self.headers = headers
  397. self.reset()
  398. def reset(self):
  399. self._count = 0
  400. self.requests = []
  401. def http_open(self, req):
  402. import email, copy
  403. self.requests.append(copy.deepcopy(req))
  404. if self._count == 0:
  405. self._count = self._count + 1
  406. name = http.client.responses[self.code]
  407. msg = email.message_from_string(self.headers)
  408. return self.parent.error(
  409. "http", req, MockFile(), self.code, name, msg)
  410. else:
  411. self.req = req
  412. msg = email.message_from_string("\r\n\r\n")
  413. return MockResponse(200, "OK", msg, "", req.get_full_url())
  414. class MockHTTPSHandler(urllib.request.AbstractHTTPHandler):
  415. # Useful for testing the Proxy-Authorization request by verifying the
  416. # properties of httpcon
  417. def __init__(self, debuglevel=0):
  418. urllib.request.AbstractHTTPHandler.__init__(self, debuglevel=debuglevel)
  419. self.httpconn = MockHTTPClass()
  420. def https_open(self, req):
  421. return self.do_open(self.httpconn, req)
  422. class MockHTTPHandlerCheckAuth(urllib.request.BaseHandler):
  423. # useful for testing auth
  424. # sends supplied code response
  425. # checks if auth header is specified in request
  426. def __init__(self, code):
  427. self.code = code
  428. self.has_auth_header = False
  429. def reset(self):
  430. self.has_auth_header = False
  431. def http_open(self, req):
  432. if req.has_header('Authorization'):
  433. self.has_auth_header = True
  434. name = http.client.responses[self.code]
  435. return MockResponse(self.code, name, MockFile(), "", req.get_full_url())
  436. class MockPasswordManager:
  437. def add_password(self, realm, uri, user, password):
  438. self.realm = realm
  439. self.url = uri
  440. self.user = user
  441. self.password = password
  442. def find_user_password(self, realm, authuri):
  443. self.target_realm = realm
  444. self.target_url = authuri
  445. return self.user, self.password
  446. class OpenerDirectorTests(unittest.TestCase):
  447. def test_add_non_handler(self):
  448. class NonHandler(object):
  449. pass
  450. self.assertRaises(TypeError,
  451. OpenerDirector().add_handler, NonHandler())
  452. def test_badly_named_methods(self):
  453. # test work-around for three methods that accidentally follow the
  454. # naming conventions for handler methods
  455. # (*_open() / *_request() / *_response())
  456. # These used to call the accidentally-named methods, causing a
  457. # TypeError in real code; here, returning self from these mock
  458. # methods would either cause no exception, or AttributeError.
  459. from urllib.error import URLError
  460. o = OpenerDirector()
  461. meth_spec = [
  462. [("do_open", "return self"), ("proxy_open", "return self")],
  463. [("redirect_request", "return self")],
  464. ]
  465. add_ordered_mock_handlers(o, meth_spec)
  466. o.add_handler(urllib.request.UnknownHandler())
  467. for scheme in "do", "proxy", "redirect":
  468. self.assertRaises(URLError, o.open, scheme+"://example.com/")
  469. def test_handled(self):
  470. # handler returning non-None means no more handlers will be called
  471. o = OpenerDirector()
  472. meth_spec = [
  473. ["http_open", "ftp_open", "http_error_302"],
  474. ["ftp_open"],
  475. [("http_open", "return self")],
  476. [("http_open", "return self")],
  477. ]
  478. handlers = add_ordered_mock_handlers(o, meth_spec)
  479. req = Request("http://example.com/")
  480. r = o.open(req)
  481. # Second .http_open() gets called, third doesn't, since second returned
  482. # non-None. Handlers without .http_open() never get any methods called
  483. # on them.
  484. # In fact, second mock handler defining .http_open() returns self
  485. # (instead of response), which becomes the OpenerDirector's return
  486. # value.
  487. self.assertEqual(r, handlers[2])
  488. calls = [(handlers[0], "http_open"), (handlers[2], "http_open")]
  489. for expected, got in zip(calls, o.calls):
  490. handler, name, args, kwds = got
  491. self.assertEqual((handler, name), expected)
  492. self.assertEqual(args, (req,))
  493. def test_handler_order(self):
  494. o = OpenerDirector()
  495. handlers = []
  496. for meths, handler_order in [([("http_open", "return self")], 500),
  497. (["http_open"], 0)]:
  498. class MockHandlerSubclass(MockHandler):
  499. pass
  500. h = MockHandlerSubclass(meths)
  501. h.handler_order = handler_order
  502. handlers.append(h)
  503. o.add_handler(h)
  504. o.open("http://example.com/")
  505. # handlers called in reverse order, thanks to their sort order
  506. self.assertEqual(o.calls[0][0], handlers[1])
  507. self.assertEqual(o.calls[1][0], handlers[0])
  508. def test_raise(self):
  509. # raising URLError stops processing of request
  510. o = OpenerDirector()
  511. meth_spec = [
  512. [("http_open", "raise")],
  513. [("http_open", "return self")],
  514. ]
  515. handlers = add_ordered_mock_handlers(o, meth_spec)
  516. req = Request("http://example.com/")
  517. self.assertRaises(urllib.error.URLError, o.open, req)
  518. self.assertEqual(o.calls, [(handlers[0], "http_open", (req,), {})])
  519. def test_http_error(self):
  520. # XXX http_error_default
  521. # http errors are a special case
  522. o = OpenerDirector()
  523. meth_spec = [
  524. [("http_open", "error 302")],
  525. [("http_error_400", "raise"), "http_open"],
  526. [("http_error_302", "return response"), "http_error_303",
  527. "http_error"],
  528. [("http_error_302")],
  529. ]
  530. handlers = add_ordered_mock_handlers(o, meth_spec)
  531. req = Request("http://example.com/")
  532. o.open(req)
  533. assert len(o.calls) == 2
  534. calls = [(handlers[0], "http_open", (req,)),
  535. (handlers[2], "http_error_302",
  536. (req, support.ALWAYS_EQ, 302, "", {}))]
  537. for expected, got in zip(calls, o.calls):
  538. handler, method_name, args = expected
  539. self.assertEqual((handler, method_name), got[:2])
  540. self.assertEqual(args, got[2])
  541. def test_processors(self):
  542. # *_request / *_response methods get called appropriately
  543. o = OpenerDirector()
  544. meth_spec = [
  545. [("http_request", "return request"),
  546. ("http_response", "return response")],
  547. [("http_request", "return request"),
  548. ("http_response", "return response")],
  549. ]
  550. handlers = add_ordered_mock_handlers(o, meth_spec)
  551. req = Request("http://example.com/")
  552. o.open(req)
  553. # processor methods are called on *all* handlers that define them,
  554. # not just the first handler that handles the request
  555. calls = [
  556. (handlers[0], "http_request"), (handlers[1], "http_request"),
  557. (handlers[0], "http_response"), (handlers[1], "http_response")]
  558. for i, (handler, name, args, kwds) in enumerate(o.calls):
  559. if i < 2:
  560. # *_request
  561. self.assertEqual((handler, name), calls[i])
  562. self.assertEqual(len(args), 1)
  563. self.assertIsInstance(args[0], Request)
  564. else:
  565. # *_response
  566. self.assertEqual((handler, name), calls[i])
  567. self.assertEqual(len(args), 2)
  568. self.assertIsInstance(args[0], Request)
  569. # response from opener.open is None, because there's no
  570. # handler that defines http_open to handle it
  571. if args[1] is not None:
  572. self.assertIsInstance(args[1], MockResponse)
  573. def sanepathname2url(path):
  574. try:
  575. path.encode("utf-8")
  576. except UnicodeEncodeError:
  577. raise unittest.SkipTest("path is not encodable to utf8")
  578. urlpath = urllib.request.pathname2url(path)
  579. if os.name == "nt" and urlpath.startswith("///"):
  580. urlpath = urlpath[2:]
  581. # XXX don't ask me about the mac...
  582. return urlpath
  583. class HandlerTests(unittest.TestCase):
  584. def test_ftp(self):
  585. class MockFTPWrapper:
  586. def __init__(self, data):
  587. self.data = data
  588. def retrfile(self, filename, filetype):
  589. self.filename, self.filetype = filename, filetype
  590. return io.StringIO(self.data), len(self.data)
  591. def close(self):
  592. pass
  593. class NullFTPHandler(urllib.request.FTPHandler):
  594. def __init__(self, data):
  595. self.data = data
  596. def connect_ftp(self, user, passwd, host, port, dirs,
  597. timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
  598. self.user, self.passwd = user, passwd
  599. self.host, self.port = host, port
  600. self.dirs = dirs
  601. self.ftpwrapper = MockFTPWrapper(self.data)
  602. return self.ftpwrapper
  603. import ftplib
  604. data = "rheum rhaponicum"
  605. h = NullFTPHandler(data)
  606. h.parent = MockOpener()
  607. for url, host, port, user, passwd, type_, dirs, filename, mimetype in [
  608. ("ftp://localhost/foo/bar/baz.html",
  609. "localhost", ftplib.FTP_PORT, "", "", "I",
  610. ["foo", "bar"], "baz.html", "text/html"),
  611. ("ftp://parrot@localhost/foo/bar/baz.html",
  612. "localhost", ftplib.FTP_PORT, "parrot", "", "I",
  613. ["foo", "bar"], "baz.html", "text/html"),
  614. ("ftp://%25parrot@localhost/foo/bar/baz.html",
  615. "localhost", ftplib.FTP_PORT, "%parrot", "", "I",
  616. ["foo", "bar"], "baz.html", "text/html"),
  617. ("ftp://%2542parrot@localhost/foo/bar/baz.html",
  618. "localhost", ftplib.FTP_PORT, "%42parrot", "", "I",
  619. ["foo", "bar"], "baz.html", "text/html"),
  620. ("ftp://localhost:80/foo/bar/",
  621. "localhost", 80, "", "", "D",
  622. ["foo", "bar"], "", None),
  623. ("ftp://localhost/baz.gif;type=a",
  624. "localhost", ftplib.FTP_PORT, "", "", "A",
  625. [], "baz.gif", None), # XXX really this should guess image/gif
  626. ]:
  627. req = Request(url)
  628. req.timeout = None
  629. r = h.ftp_open(req)
  630. # ftp authentication not yet implemented by FTPHandler
  631. self.assertEqual(h.user, user)
  632. self.assertEqual(h.passwd, passwd)
  633. self.assertEqual(h.host, socket.gethostbyname(host))
  634. self.assertEqual(h.port, port)
  635. self.assertEqual(h.dirs, dirs)
  636. self.assertEqual(h.ftpwrapper.filename, filename)
  637. self.assertEqual(h.ftpwrapper.filetype, type_)
  638. headers = r.info()
  639. self.assertEqual(headers.get("Content-type"), mimetype)
  640. self.assertEqual(int(headers["Content-length"]), len(data))
  641. def test_file(self):
  642. import email.utils
  643. h = urllib.request.FileHandler()
  644. o = h.parent = MockOpener()
  645. TESTFN = os_helper.TESTFN
  646. urlpath = sanepathname2url(os.path.abspath(TESTFN))
  647. towrite = b"hello, world\n"
  648. urls = [
  649. "file://localhost%s" % urlpath,
  650. "file://%s" % urlpath,
  651. "file://%s%s" % (socket.gethostbyname('localhost'), urlpath),
  652. ]
  653. try:
  654. localaddr = socket.gethostbyname(socket.gethostname())
  655. except socket.gaierror:
  656. localaddr = ''
  657. if localaddr:
  658. urls.append("file://%s%s" % (localaddr, urlpath))
  659. for url in urls:
  660. f = open(TESTFN, "wb")
  661. try:
  662. try:
  663. f.write(towrite)
  664. finally:
  665. f.close()
  666. r = h.file_open(Request(url))
  667. try:
  668. data = r.read()
  669. headers = r.info()
  670. respurl = r.geturl()
  671. finally:
  672. r.close()
  673. stats = os.stat(TESTFN)
  674. modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
  675. finally:
  676. os.remove(TESTFN)
  677. self.assertEqual(data, towrite)
  678. self.assertEqual(headers["Content-type"], "text/plain")
  679. self.assertEqual(headers["Content-length"], "13")
  680. self.assertEqual(headers["Last-modified"], modified)
  681. self.assertEqual(respurl, url)
  682. for url in [
  683. "file://localhost:80%s" % urlpath,
  684. "file:///file_does_not_exist.txt",
  685. "file://not-a-local-host.com//dir/file.txt",
  686. "file://%s:80%s/%s" % (socket.gethostbyname('localhost'),
  687. os.getcwd(), TESTFN),
  688. "file://somerandomhost.ontheinternet.com%s/%s" %
  689. (os.getcwd(), TESTFN),
  690. ]:
  691. try:
  692. f = open(TESTFN, "wb")
  693. try:
  694. f.write(towrite)
  695. finally:
  696. f.close()
  697. self.assertRaises(urllib.error.URLError,
  698. h.file_open, Request(url))
  699. finally:
  700. os.remove(TESTFN)
  701. h = urllib.request.FileHandler()
  702. o = h.parent = MockOpener()
  703. # XXXX why does // mean ftp (and /// mean not ftp!), and where
  704. # is file: scheme specified? I think this is really a bug, and
  705. # what was intended was to distinguish between URLs like:
  706. # file:/blah.txt (a file)
  707. # file://localhost/blah.txt (a file)
  708. # file:///blah.txt (a file)
  709. # file://ftp.example.com/blah.txt (an ftp URL)
  710. for url, ftp in [
  711. ("file://ftp.example.com//foo.txt", False),
  712. ("file://ftp.example.com///foo.txt", False),
  713. ("file://ftp.example.com/foo.txt", False),
  714. ("file://somehost//foo/something.txt", False),
  715. ("file://localhost//foo/something.txt", False),
  716. ]:
  717. req = Request(url)
  718. try:
  719. h.file_open(req)
  720. except urllib.error.URLError:
  721. self.assertFalse(ftp)
  722. else:
  723. self.assertIs(o.req, req)
  724. self.assertEqual(req.type, "ftp")
  725. self.assertEqual(req.type == "ftp", ftp)
  726. def test_http(self):
  727. h = urllib.request.AbstractHTTPHandler()
  728. o = h.parent = MockOpener()
  729. url = "http://example.com/"
  730. for method, data in [("GET", None), ("POST", b"blah")]:
  731. req = Request(url, data, {"Foo": "bar"})
  732. req.timeout = None
  733. req.add_unredirected_header("Spam", "eggs")
  734. http = MockHTTPClass()
  735. r = h.do_open(http, req)
  736. # result attributes
  737. r.read; r.readline # wrapped MockFile methods
  738. r.info; r.geturl # addinfourl methods
  739. r.code, r.msg == 200, "OK" # added from MockHTTPClass.getreply()
  740. hdrs = r.info()
  741. hdrs.get; hdrs.__contains__ # r.info() gives dict from .getreply()
  742. self.assertEqual(r.geturl(), url)
  743. self.assertEqual(http.host, "example.com")
  744. self.assertEqual(http.level, 0)
  745. self.assertEqual(http.method, method)
  746. self.assertEqual(http.selector, "/")
  747. self.assertEqual(http.req_headers,
  748. [("Connection", "close"),
  749. ("Foo", "bar"), ("Spam", "eggs")])
  750. self.assertEqual(http.data, data)
  751. # check OSError converted to URLError
  752. http.raise_on_endheaders = True
  753. self.assertRaises(urllib.error.URLError, h.do_open, http, req)
  754. # Check for TypeError on POST data which is str.
  755. req = Request("http://example.com/","badpost")
  756. self.assertRaises(TypeError, h.do_request_, req)
  757. # check adding of standard headers
  758. o.addheaders = [("Spam", "eggs")]
  759. for data in b"", None: # POST, GET
  760. req = Request("http://example.com/", data)
  761. r = MockResponse(200, "OK", {}, "")
  762. newreq = h.do_request_(req)
  763. if data is None: # GET
  764. self.assertNotIn("Content-length", req.unredirected_hdrs)
  765. self.assertNotIn("Content-type", req.unredirected_hdrs)
  766. else: # POST
  767. self.assertEqual(req.unredirected_hdrs["Content-length"], "0")
  768. self.assertEqual(req.unredirected_hdrs["Content-type"],
  769. "application/x-www-form-urlencoded")
  770. # XXX the details of Host could be better tested
  771. self.assertEqual(req.unredirected_hdrs["Host"], "example.com")
  772. self.assertEqual(req.unredirected_hdrs["Spam"], "eggs")
  773. # don't clobber existing headers
  774. req.add_unredirected_header("Content-length", "foo")
  775. req.add_unredirected_header("Content-type", "bar")
  776. req.add_unredirected_header("Host", "baz")
  777. req.add_unredirected_header("Spam", "foo")
  778. newreq = h.do_request_(req)
  779. self.assertEqual(req.unredirected_hdrs["Content-length"], "foo")
  780. self.assertEqual(req.unredirected_hdrs["Content-type"], "bar")
  781. self.assertEqual(req.unredirected_hdrs["Host"], "baz")
  782. self.assertEqual(req.unredirected_hdrs["Spam"], "foo")
  783. def test_http_body_file(self):
  784. # A regular file - chunked encoding is used unless Content Length is
  785. # already set.
  786. h = urllib.request.AbstractHTTPHandler()
  787. o = h.parent = MockOpener()
  788. file_obj = tempfile.NamedTemporaryFile(mode='w+b', delete=False)
  789. file_path = file_obj.name
  790. file_obj.close()
  791. self.addCleanup(os.unlink, file_path)
  792. with open(file_path, "rb") as f:
  793. req = Request("http://example.com/", f, {})
  794. newreq = h.do_request_(req)
  795. te = newreq.get_header('Transfer-encoding')
  796. self.assertEqual(te, "chunked")
  797. self.assertFalse(newreq.has_header('Content-length'))
  798. with open(file_path, "rb") as f:
  799. req = Request("http://example.com/", f, {"Content-Length": 30})
  800. newreq = h.do_request_(req)
  801. self.assertEqual(int(newreq.get_header('Content-length')), 30)
  802. self.assertFalse(newreq.has_header("Transfer-encoding"))
  803. def test_http_body_fileobj(self):
  804. # A file object - chunked encoding is used
  805. # unless Content Length is already set.
  806. # (Note that there are some subtle differences to a regular
  807. # file, that is why we are testing both cases.)
  808. h = urllib.request.AbstractHTTPHandler()
  809. o = h.parent = MockOpener()
  810. file_obj = io.BytesIO()
  811. req = Request("http://example.com/", file_obj, {})
  812. newreq = h.do_request_(req)
  813. self.assertEqual(newreq.get_header('Transfer-encoding'), 'chunked')
  814. self.assertFalse(newreq.has_header('Content-length'))
  815. headers = {"Content-Length": 30}
  816. req = Request("http://example.com/", file_obj, headers)
  817. newreq = h.do_request_(req)
  818. self.assertEqual(int(newreq.get_header('Content-length')), 30)
  819. self.assertFalse(newreq.has_header("Transfer-encoding"))
  820. file_obj.close()
  821. def test_http_body_pipe(self):
  822. # A file reading from a pipe.
  823. # A pipe cannot be seek'ed. There is no way to determine the
  824. # content length up front. Thus, do_request_() should fall
  825. # back to Transfer-encoding chunked.
  826. h = urllib.request.AbstractHTTPHandler()
  827. o = h.parent = MockOpener()
  828. cmd = [sys.executable, "-c", r"pass"]
  829. for headers in {}, {"Content-Length": 30}:
  830. with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc:
  831. req = Request("http://example.com/", proc.stdout, headers)
  832. newreq = h.do_request_(req)
  833. if not headers:
  834. self.assertEqual(newreq.get_header('Content-length'), None)
  835. self.assertEqual(newreq.get_header('Transfer-encoding'),
  836. 'chunked')
  837. else:
  838. self.assertEqual(int(newreq.get_header('Content-length')),
  839. 30)
  840. def test_http_body_iterable(self):
  841. # Generic iterable. There is no way to determine the content
  842. # length up front. Fall back to Transfer-encoding chunked.
  843. h = urllib.request.AbstractHTTPHandler()
  844. o = h.parent = MockOpener()
  845. def iterable_body():
  846. yield b"one"
  847. for headers in {}, {"Content-Length": 11}:
  848. req = Request("http://example.com/", iterable_body(), headers)
  849. newreq = h.do_request_(req)
  850. if not headers:
  851. self.assertEqual(newreq.get_header('Content-length'), None)
  852. self.assertEqual(newreq.get_header('Transfer-encoding'),
  853. 'chunked')
  854. else:
  855. self.assertEqual(int(newreq.get_header('Content-length')), 11)
  856. def test_http_body_empty_seq(self):
  857. # Zero-length iterable body should be treated like any other iterable
  858. h = urllib.request.AbstractHTTPHandler()
  859. h.parent = MockOpener()
  860. req = h.do_request_(Request("http://example.com/", ()))
  861. self.assertEqual(req.get_header("Transfer-encoding"), "chunked")
  862. self.assertFalse(req.has_header("Content-length"))
  863. def test_http_body_array(self):
  864. # array.array Iterable - Content Length is calculated
  865. h = urllib.request.AbstractHTTPHandler()
  866. o = h.parent = MockOpener()
  867. iterable_array = array.array("I",[1,2,3,4])
  868. for headers in {}, {"Content-Length": 16}:
  869. req = Request("http://example.com/", iterable_array, headers)
  870. newreq = h.do_request_(req)
  871. self.assertEqual(int(newreq.get_header('Content-length')),16)
  872. def test_http_handler_debuglevel(self):
  873. o = OpenerDirector()
  874. h = MockHTTPSHandler(debuglevel=1)
  875. o.add_handler(h)
  876. o.open("https://www.example.com")
  877. self.assertEqual(h._debuglevel, 1)
  878. def test_http_doubleslash(self):
  879. # Checks the presence of any unnecessary double slash in url does not
  880. # break anything. Previously, a double slash directly after the host
  881. # could cause incorrect parsing.
  882. h = urllib.request.AbstractHTTPHandler()
  883. h.parent = MockOpener()
  884. data = b""
  885. ds_urls = [
  886. "http://example.com/foo/bar/baz.html",
  887. "http://example.com//foo/bar/baz.html",
  888. "http://example.com/foo//bar/baz.html",
  889. "http://example.com/foo/bar//baz.html"
  890. ]
  891. for ds_url in ds_urls:
  892. ds_req = Request(ds_url, data)
  893. # Check whether host is determined correctly if there is no proxy
  894. np_ds_req = h.do_request_(ds_req)
  895. self.assertEqual(np_ds_req.unredirected_hdrs["Host"], "example.com")
  896. # Check whether host is determined correctly if there is a proxy
  897. ds_req.set_proxy("someproxy:3128", None)
  898. p_ds_req = h.do_request_(ds_req)
  899. self.assertEqual(p_ds_req.unredirected_hdrs["Host"], "example.com")
  900. def test_full_url_setter(self):
  901. # Checks to ensure that components are set correctly after setting the
  902. # full_url of a Request object
  903. urls = [
  904. 'http://example.com?foo=bar#baz',
  905. 'http://example.com?foo=bar&spam=eggs#bash',
  906. 'http://example.com',
  907. ]
  908. # testing a reusable request instance, but the url parameter is
  909. # required, so just use a dummy one to instantiate
  910. r = Request('http://example.com')
  911. for url in urls:
  912. r.full_url = url
  913. parsed = urlparse(url)
  914. self.assertEqual(r.get_full_url(), url)
  915. # full_url setter uses splittag to split into components.
  916. # splittag sets the fragment as None while urlparse sets it to ''
  917. self.assertEqual(r.fragment or '', parsed.fragment)
  918. self.assertEqual(urlparse(r.get_full_url()).query, parsed.query)
  919. def test_full_url_deleter(self):
  920. r = Request('http://www.example.com')
  921. del r.full_url
  922. self.assertIsNone(r.full_url)
  923. self.assertIsNone(r.fragment)
  924. self.assertEqual(r.selector, '')
  925. def test_fixpath_in_weirdurls(self):
  926. # Issue4493: urllib2 to supply '/' when to urls where path does not
  927. # start with'/'
  928. h = urllib.request.AbstractHTTPHandler()
  929. h.parent = MockOpener()
  930. weird_url = 'http://www.python.org?getspam'
  931. req = Request(weird_url)
  932. newreq = h.do_request_(req)
  933. self.assertEqual(newreq.host, 'www.python.org')
  934. self.assertEqual(newreq.selector, '/?getspam')
  935. url_without_path = 'http://www.python.org'
  936. req = Request(url_without_path)
  937. newreq = h.do_request_(req)
  938. self.assertEqual(newreq.host, 'www.python.org')
  939. self.assertEqual(newreq.selector, '')
  940. def test_errors(self):
  941. h = urllib.request.HTTPErrorProcessor()
  942. o = h.parent = MockOpener()
  943. url = "http://example.com/"
  944. req = Request(url)
  945. # all 2xx are passed through
  946. r = MockResponse(200, "OK", {}, "", url)
  947. newr = h.http_response(req, r)
  948. self.assertIs(r, newr)
  949. self.assertFalse(hasattr(o, "proto")) # o.error not called
  950. r = MockResponse(202, "Accepted", {}, "", url)
  951. newr = h.http_response(req, r)
  952. self.assertIs(r, newr)
  953. self.assertFalse(hasattr(o, "proto")) # o.error not called
  954. r = MockResponse(206, "Partial content", {}, "", url)
  955. newr = h.http_response(req, r)
  956. self.assertIs(r, newr)
  957. self.assertFalse(hasattr(o, "proto")) # o.error not called
  958. # anything else calls o.error (and MockOpener returns None, here)
  959. r = MockResponse(502, "Bad gateway", {}, "", url)
  960. self.assertIsNone(h.http_response(req, r))
  961. self.assertEqual(o.proto, "http") # o.error called
  962. self.assertEqual(o.args, (req, r, 502, "Bad gateway", {}))
  963. def test_cookies(self):
  964. cj = MockCookieJar()
  965. h = urllib.request.HTTPCookieProcessor(cj)
  966. h.parent = MockOpener()
  967. req = Request("http://example.com/")
  968. r = MockResponse(200, "OK", {}, "")
  969. newreq = h.http_request(req)
  970. self.assertIs(cj.ach_req, req)
  971. self.assertIs(cj.ach_req, newreq)
  972. self.assertEqual(req.origin_req_host, "example.com")
  973. self.assertFalse(req.unverifiable)
  974. newr = h.http_response(req, r)
  975. self.assertIs(cj.ec_req, req)
  976. self.assertIs(cj.ec_r, r)
  977. self.assertIs(r, newr)
  978. def test_redirect(self):
  979. from_url = "http://example.com/a.html"
  980. to_url = "http://example.com/b.html"
  981. h = urllib.request.HTTPRedirectHandler()
  982. o = h.parent = MockOpener()
  983. # ordinary redirect behaviour
  984. for code in 301, 302, 303, 307, 308:
  985. for data in None, "blah\nblah\n":
  986. method = getattr(h, "http_error_%s" % code)
  987. req = Request(from_url, data)
  988. req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
  989. req.add_header("Nonsense", "viking=withhold")
  990. if data is not None:
  991. req.add_header("Content-Length", str(len(data)))
  992. req.add_unredirected_header("Spam", "spam")
  993. try:
  994. method(req, MockFile(), code, "Blah",
  995. MockHeaders({"location": to_url}))
  996. except urllib.error.HTTPError:
  997. # 307 and 308 in response to POST require user OK
  998. self.assertIn(code, (307, 308))
  999. self.assertIsNotNone(data)
  1000. self.assertEqual(o.req.get_full_url(), to_url)
  1001. try:
  1002. self.assertEqual(o.req.get_method(), "GET")
  1003. except AttributeError:
  1004. self.assertFalse(o.req.data)
  1005. # now it's a GET, there should not be headers regarding content
  1006. # (possibly dragged from before being a POST)
  1007. headers = [x.lower() for x in o.req.headers]
  1008. self.assertNotIn("content-length", headers)
  1009. self.assertNotIn("content-type", headers)
  1010. self.assertEqual(o.req.headers["Nonsense"],
  1011. "viking=withhold")
  1012. self.assertNotIn("Spam", o.req.headers)
  1013. self.assertNotIn("Spam", o.req.unredirected_hdrs)
  1014. # loop detection
  1015. req = Request(from_url)
  1016. req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
  1017. def redirect(h, req, url=to_url):
  1018. h.http_error_302(req, MockFile(), 302, "Blah",
  1019. MockHeaders({"location": url}))
  1020. # Note that the *original* request shares the same record of
  1021. # redirections with the sub-requests caused by the redirections.
  1022. # detect infinite loop redirect of a URL to itself
  1023. req = Request(from_url, origin_req_host="example.com")
  1024. count = 0
  1025. req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
  1026. try:
  1027. while 1:
  1028. redirect(h, req, "http://example.com/")
  1029. count = count + 1
  1030. except urllib.error.HTTPError:
  1031. # don't stop until max_repeats, because cookies may introduce state
  1032. self.assertEqual(count, urllib.request.HTTPRedirectHandler.max_repeats)
  1033. # detect endless non-repeating chain of redirects
  1034. req = Request(from_url, origin_req_host="example.com")
  1035. count = 0
  1036. req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
  1037. try:
  1038. while 1:
  1039. redirect(h, req, "http://example.com/%d" % count)
  1040. count = count + 1
  1041. except urllib.error.HTTPError:
  1042. self.assertEqual(count,
  1043. urllib.request.HTTPRedirectHandler.max_redirections)
  1044. def test_invalid_redirect(self):
  1045. from_url = "http://example.com/a.html"
  1046. valid_schemes = ['http','https','ftp']
  1047. invalid_schemes = ['file','imap','ldap']
  1048. schemeless_url = "example.com/b.html"
  1049. h = urllib.request.HTTPRedirectHandler()
  1050. o = h.parent = MockOpener()
  1051. req = Request(from_url)
  1052. req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
  1053. for scheme in invalid_schemes:
  1054. invalid_url = scheme + '://' + schemeless_url
  1055. self.assertRaises(urllib.error.HTTPError, h.http_error_302,
  1056. req, MockFile(), 302, "Security Loophole",
  1057. MockHeaders({"location": invalid_url}))
  1058. for scheme in valid_schemes:
  1059. valid_url = scheme + '://' + schemeless_url
  1060. h.http_error_302(req, MockFile(), 302, "That's fine",
  1061. MockHeaders({"location": valid_url}))
  1062. self.assertEqual(o.req.get_full_url(), valid_url)
  1063. def test_relative_redirect(self):
  1064. from_url = "http://example.com/a.html"
  1065. relative_url = "/b.html"
  1066. h = urllib.request.HTTPRedirectHandler()
  1067. o = h.parent = MockOpener()
  1068. req = Request(from_url)
  1069. req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
  1070. valid_url = urllib.parse.urljoin(from_url,relative_url)
  1071. h.http_error_302(req, MockFile(), 302, "That's fine",
  1072. MockHeaders({"location": valid_url}))
  1073. self.assertEqual(o.req.get_full_url(), valid_url)
  1074. def test_cookie_redirect(self):
  1075. # cookies shouldn't leak into redirected requests
  1076. from http.cookiejar import CookieJar
  1077. from test.test_http_cookiejar import interact_netscape
  1078. cj = CookieJar()
  1079. interact_netscape(cj, "http://www.example.com/", "spam=eggs")
  1080. hh = MockHTTPHandler(302, "Location: http://www.cracker.com/\r\n\r\n")
  1081. hdeh = urllib.request.HTTPDefaultErrorHandler()
  1082. hrh = urllib.request.HTTPRedirectHandler()
  1083. cp = urllib.request.HTTPCookieProcessor(cj)
  1084. o = build_test_opener(hh, hdeh, hrh, cp)
  1085. o.open("http://www.example.com/")
  1086. self.assertFalse(hh.req.has_header("Cookie"))
  1087. def test_redirect_fragment(self):
  1088. redirected_url = 'http://www.example.com/index.html#OK\r\n\r\n'
  1089. hh = MockHTTPHandler(302, 'Location: ' + redirected_url)
  1090. hdeh = urllib.request.HTTPDefaultErrorHandler()
  1091. hrh = urllib.request.HTTPRedirectHandler()
  1092. o = build_test_opener(hh, hdeh, hrh)
  1093. fp = o.open('http://www.example.com')
  1094. self.assertEqual(fp.geturl(), redirected_url.strip())
  1095. def test_redirect_no_path(self):
  1096. # Issue 14132: Relative redirect strips original path
  1097. # clear _opener global variable
  1098. self.addCleanup(urllib.request.urlcleanup)
  1099. real_class = http.client.HTTPConnection
  1100. response1 = b"HTTP/1.1 302 Found\r\nLocation: ?query\r\n\r\n"
  1101. http.client.HTTPConnection = test_urllib.fakehttp(response1)
  1102. self.addCleanup(setattr, http.client, "HTTPConnection", real_class)
  1103. urls = iter(("/path", "/path?query"))
  1104. def request(conn, method, url, *pos, **kw):
  1105. self.assertEqual(url, next(urls))
  1106. real_class.request(conn, method, url, *pos, **kw)
  1107. # Change response for subsequent connection
  1108. conn.__class__.fakedata = b"HTTP/1.1 200 OK\r\n\r\nHello!"
  1109. http.client.HTTPConnection.request = request
  1110. fp = urllib.request.urlopen("http://python.org/path")
  1111. self.assertEqual(fp.geturl(), "http://python.org/path?query")
  1112. def test_redirect_encoding(self):
  1113. # Some characters in the redirect target may need special handling,
  1114. # but most ASCII characters should be treated as already encoded
  1115. class Handler(urllib.request.HTTPHandler):
  1116. def http_open(self, req):
  1117. result = self.do_open(self.connection, req)
  1118. self.last_buf = self.connection.buf
  1119. # Set up a normal response for the next request
  1120. self.connection = test_urllib.fakehttp(
  1121. b'HTTP/1.1 200 OK\r\n'
  1122. b'Content-Length: 3\r\n'
  1123. b'\r\n'
  1124. b'123'
  1125. )
  1126. return result
  1127. handler = Handler()
  1128. opener = urllib.request.build_opener(handler)
  1129. tests = (
  1130. (b'/p\xC3\xA5-dansk/', b'/p%C3%A5-dansk/'),
  1131. (b'/spaced%20path/', b'/spaced%20path/'),
  1132. (b'/spaced path/', b'/spaced%20path/'),
  1133. (b'/?p\xC3\xA5-dansk', b'/?p%C3%A5-dansk'),
  1134. )
  1135. for [location, result] in tests:
  1136. with self.subTest(repr(location)):
  1137. handler.connection = test_urllib.fakehttp(
  1138. b'HTTP/1.1 302 Redirect\r\n'
  1139. b'Location: ' + location + b'\r\n'
  1140. b'\r\n'
  1141. )
  1142. response = opener.open('http://example.com/')
  1143. expected = b'GET ' + result + b' '
  1144. request = handler.last_buf
  1145. self.assertTrue(request.startswith(expected), repr(request))
  1146. def test_proxy(self):
  1147. u = "proxy.example.com:3128"
  1148. for d in dict(http=u), dict(HTTP=u):
  1149. o = OpenerDirector()
  1150. ph = urllib.request.ProxyHandler(d)
  1151. o.add_handler(ph)
  1152. meth_spec = [
  1153. [("http_open", "return response")]
  1154. ]
  1155. handlers = add_ordered_mock_handlers(o, meth_spec)
  1156. req = Request("http://acme.example.com/")
  1157. self.assertEqual(req.host, "acme.example.com")
  1158. o.open(req)
  1159. self.assertEqual(req.host, u)
  1160. self.assertEqual([(handlers[0], "http_open")],
  1161. [tup[0:2] for tup in o.calls])
  1162. def test_proxy_no_proxy(self):
  1163. os.environ['no_proxy'] = 'python.org'
  1164. o = OpenerDirector()
  1165. ph = urllib.request.ProxyHandler(dict(http="proxy.example.com"))
  1166. o.add_handler(ph)
  1167. req = Request("http://www.perl.org/")
  1168. self.assertEqual(req.host, "www.perl.org")
  1169. o.open(req)
  1170. self.assertEqual(req.host, "proxy.example.com")
  1171. req = Request("http://www.python.org")
  1172. self.assertEqual(req.host, "www.python.org")
  1173. o.open(req)
  1174. self.assertEqual(req.host, "www.python.org")
  1175. del os.environ['no_proxy']
  1176. def test_proxy_no_proxy_all(self):
  1177. os.environ['no_proxy'] = '*'
  1178. o = OpenerDirector()
  1179. ph = urllib.request.ProxyHandler(dict(http="proxy.example.com"))
  1180. o.add_handler(ph)
  1181. req = Request("http://www.python.org")
  1182. self.assertEqual(req.host, "www.python.org")
  1183. o.open(req)
  1184. self.assertEqual(req.host, "www.python.org")
  1185. del os.environ['no_proxy']
  1186. def test_proxy_https(self):
  1187. o = OpenerDirector()
  1188. ph = urllib.request.ProxyHandler(dict(https="proxy.example.com:3128"))
  1189. o.add_handler(ph)
  1190. meth_spec = [
  1191. [("https_open", "return response")]
  1192. ]
  1193. handlers = add_ordered_mock_handlers(o, meth_spec)
  1194. req = Request("https://www.example.com/")
  1195. self.assertEqual(req.host, "www.example.com")
  1196. o.open(req)
  1197. self.assertEqual(req.host, "proxy.example.com:3128")
  1198. self.assertEqual([(handlers[0], "https_open")],
  1199. [tup[0:2] for tup in o.calls])
  1200. def test_proxy_https_proxy_authorization(self):
  1201. o = OpenerDirector()
  1202. ph = urllib.request.ProxyHandler(dict(https='proxy.example.com:3128'))
  1203. o.add_handler(ph)
  1204. https_handler = MockHTTPSHandler()
  1205. o.add_handler(https_handler)
  1206. req = Request("https://www.example.com/")
  1207. req.add_header("Proxy-Authorization", "FooBar")
  1208. req.add_header("User-Agent", "Grail")
  1209. self.assertEqual(req.host, "www.example.com")
  1210. self.assertIsNone(req._tunnel_host)
  1211. o.open(req)
  1212. # Verify Proxy-Authorization gets tunneled to request.
  1213. # httpsconn req_headers do not have the Proxy-Authorization header but
  1214. # the req will have.
  1215. self.assertNotIn(("Proxy-Authorization", "FooBar"),
  1216. https_handler.httpconn.req_headers)
  1217. self.assertIn(("User-Agent", "Grail"),
  1218. https_handler.httpconn.req_headers)
  1219. self.assertIsNotNone(req._tunnel_host)
  1220. self.assertEqual(req.host, "proxy.example.com:3128")
  1221. self.assertEqual(req.get_header("Proxy-authorization"), "FooBar")
  1222. @unittest.skipUnless(sys.platform == 'darwin', "only relevant for OSX")
  1223. def test_osx_proxy_bypass(self):
  1224. bypass = {
  1225. 'exclude_simple': False,
  1226. 'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.10',
  1227. '10.0/16']
  1228. }
  1229. # Check hosts that should trigger the proxy bypass
  1230. for host in ('foo.bar', 'www.bar.com', '127.0.0.1', '10.10.0.1',
  1231. '10.0.0.1'):
  1232. self.assertTrue(_proxy_bypass_macosx_sysconf(host, bypass),
  1233. 'expected bypass of %s to be True' % host)
  1234. # Check hosts that should not trigger the proxy bypass
  1235. for host in ('abc.foo.bar', 'bar.com', '127.0.0.2', '10.11.0.1',
  1236. 'notinbypass'):
  1237. self.assertFalse(_proxy_bypass_macosx_sysconf(host, bypass),
  1238. 'expected bypass of %s to be False' % host)
  1239. # Check the exclude_simple flag
  1240. bypass = {'exclude_simple': True, 'exceptions': []}
  1241. self.assertTrue(_proxy_bypass_macosx_sysconf('test', bypass))
  1242. # Check that invalid prefix lengths are ignored
  1243. bypass = {
  1244. 'exclude_simple': False,
  1245. 'exceptions': [ '10.0.0.0/40', '172.19.10.0/24' ]
  1246. }
  1247. host = '172.19.10.5'
  1248. self.assertTrue(_proxy_bypass_macosx_sysconf(host, bypass),
  1249. 'expected bypass of %s to be True' % host)
  1250. host = '10.0.1.5'
  1251. self.assertFalse(_proxy_bypass_macosx_sysconf(host, bypass),
  1252. 'expected bypass of %s to be False' % host)
  1253. def check_basic_auth(self, headers, realm):
  1254. with self.subTest(realm=realm, headers=headers):
  1255. opener = OpenerDirector()
  1256. password_manager = MockPasswordManager()
  1257. auth_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
  1258. body = '\r\n'.join(headers) + '\r\n\r\n'
  1259. http_handler = MockHTTPHandler(401, body)
  1260. opener.add_handler(auth_handler)
  1261. opener.add_handler(http_handler)
  1262. self._test_basic_auth(opener, auth_handler, "Authorization",
  1263. realm, http_handler, password_manager,
  1264. "http://acme.example.com/protected",
  1265. "http://acme.example.com/protected")
  1266. def test_basic_auth(self):
  1267. realm = "realm2@example.com"
  1268. realm2 = "realm2@example.com"
  1269. basic = f'Basic realm="{realm}"'
  1270. basic2 = f'Basic realm="{realm2}"'
  1271. other_no_realm = 'Otherscheme xxx'
  1272. digest = (f'Digest realm="{realm2}", '
  1273. f'qop="auth, auth-int", '
  1274. f'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", '
  1275. f'opaque="5ccc069c403ebaf9f0171e9517f40e41"')
  1276. for realm_str in (
  1277. # test "quote" and 'quote'
  1278. f'Basic realm="{realm}"',
  1279. f"Basic realm='{realm}'",
  1280. # charset is ignored
  1281. f'Basic realm="{realm}", charset="UTF-8"',
  1282. # Multiple challenges per header
  1283. f'{basic}, {basic2}',
  1284. f'{basic}, {other_no_realm}',
  1285. f'{other_no_realm}, {basic}',
  1286. f'{basic}, {digest}',
  1287. f'{digest}, {basic}',
  1288. ):
  1289. headers = [f'WWW-Authenticate: {realm_str}']
  1290. self.check_basic_auth(headers, realm)
  1291. # no quote: expect a warning
  1292. with warnings_helper.check_warnings(("Basic Auth Realm was unquoted",
  1293. UserWarning)):
  1294. headers = [f'WWW-Authenticate: Basic realm={realm}']
  1295. self.check_basic_auth(headers, realm)
  1296. # Multiple headers: one challenge per header.
  1297. # Use the first Basic realm.
  1298. for challenges in (
  1299. [basic, basic2],
  1300. [basic, digest],
  1301. [digest, basic],
  1302. ):
  1303. headers = [f'WWW-Authenticate: {challenge}'
  1304. for challenge in challenges]
  1305. self.check_basic_auth(headers, realm)
  1306. def test_proxy_basic_auth(self):
  1307. opener = OpenerDirector()
  1308. ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))
  1309. opener.add_handler(ph)
  1310. password_manager = MockPasswordManager()
  1311. auth_handler = urllib.request.ProxyBasicAuthHandler(password_manager)
  1312. realm = "ACME Networks"
  1313. http_handler = MockHTTPHandler(
  1314. 407, 'Proxy-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
  1315. opener.add_handler(auth_handler)
  1316. opener.add_handler(http_handler)
  1317. self._test_basic_auth(opener, auth_handler, "Proxy-authorization",
  1318. realm, http_handler, password_manager,
  1319. "http://acme.example.com:3128/protected",
  1320. "proxy.example.com:3128",
  1321. )
  1322. def test_basic_and_digest_auth_handlers(self):
  1323. # HTTPDigestAuthHandler raised an exception if it couldn't handle a 40*
  1324. # response (http://python.org/sf/1479302), where it should instead
  1325. # return None to allow another handler (especially
  1326. # HTTPBasicAuthHandler) to handle the response.
  1327. # Also (http://python.org/sf/14797027, RFC 2617 section 1.2), we must
  1328. # try digest first (since it's the strongest auth scheme), so we record
  1329. # order of calls here to check digest comes first:
  1330. class RecordingOpenerDirector(OpenerDirector):
  1331. def __init__(self):
  1332. OpenerDirector.__init__(self)
  1333. self.recorded = []
  1334. def record(self, info):
  1335. self.recorded.append(info)
  1336. class TestDigestAuthHandler(urllib.request.HTTPDigestAuthHandler):
  1337. def http_error_401(self, *args, **kwds):
  1338. self.parent.record("digest")
  1339. urllib.request.HTTPDigestAuthHandler.http_error_401(self,
  1340. *args, **kwds)
  1341. class TestBasicAuthHandler(urllib.request.HTTPBasicAuthHandler):
  1342. def http_error_401(self, *args, **kwds):
  1343. self.parent.record("basic")
  1344. urllib.request.HTTPBasicAuthHandler.http_error_401(self,
  1345. *args, **kwds)
  1346. opener = RecordingOpenerDirector()
  1347. password_manager = MockPasswordManager()
  1348. digest_handler = TestDigestAuthHandler(password_manager)
  1349. basic_handler = TestBasicAuthHandler(password_manager)
  1350. realm = "ACME Networks"
  1351. http_handler = MockHTTPHandler(
  1352. 401, 'WWW-Authenticate: Basic realm="%s"\r\n\r\n' % realm)
  1353. opener.add_handler(basic_handler)
  1354. opener.add_handler(digest_handler)
  1355. opener.add_handler(http_handler)
  1356. # check basic auth isn't blocked by digest handler failing
  1357. self._test_basic_auth(opener, basic_handler, "Authorization",
  1358. realm, http_handler, password_manager,
  1359. "http://acme.example.com/protected",
  1360. "http://acme.example.com/protected",
  1361. )
  1362. # check digest was tried before basic (twice, because
  1363. # _test_basic_auth called .open() twice)
  1364. self.assertEqual(opener.recorded, ["digest", "basic"]*2)
  1365. def test_unsupported_auth_digest_handler(self):
  1366. opener = OpenerDirector()
  1367. # While using DigestAuthHandler
  1368. digest_auth_handler = urllib.request.HTTPDigestAuthHandler(None)
  1369. http_handler = MockHTTPHandler(
  1370. 401, 'WWW-Authenticate: Kerberos\r\n\r\n')
  1371. opener.add_handler(digest_auth_handler)
  1372. opener.add_handler(http_handler)
  1373. self.assertRaises(ValueError, opener.open, "http://www.example.com")
  1374. def test_unsupported_auth_basic_handler(self):
  1375. # While using BasicAuthHandler
  1376. opener = OpenerDirector()
  1377. basic_auth_handler = urllib.request.HTTPBasicAuthHandler(None)
  1378. http_handler = MockHTTPHandler(
  1379. 401, 'WWW-Authenticate: NTLM\r\n\r\n')
  1380. opener.add_handler(basic_auth_handler)
  1381. opener.add_handler(http_handler)
  1382. self.assertRaises(ValueError, opener.open, "http://www.example.com")
  1383. def _test_basic_auth(self, opener, auth_handler, auth_header,
  1384. realm, http_handler, password_manager,
  1385. request_url, protected_url):
  1386. import base64
  1387. user, password = "wile", "coyote"
  1388. # .add_password() fed through to password manager
  1389. auth_handler.add_password(realm, request_url, user, password)
  1390. self.assertEqual(realm, password_manager.realm)
  1391. self.assertEqual(request_url, password_manager.url)
  1392. self.assertEqual(user, password_manager.user)
  1393. self.assertEqual(password, password_manager.password)
  1394. opener.open(request_url)
  1395. # should have asked the password manager for the username/password
  1396. self.assertEqual(password_manager.target_realm, realm)
  1397. self.assertEqual(password_manager.target_url, protected_url)
  1398. # expect one request without authorization, then one with
  1399. self.assertEqual(len(http_handler.requests), 2)
  1400. self.assertFalse(http_handler.requests[0].has_header(auth_header))
  1401. userpass = bytes('%s:%s' % (user, password), "ascii")
  1402. auth_hdr_value = ('Basic ' +
  1403. base64.encodebytes(userpass).strip().decode())
  1404. self.assertEqual(http_handler.requests[1].get_header(auth_header),
  1405. auth_hdr_value)
  1406. self.assertEqual(http_handler.requests[1].unredirected_hdrs[auth_header],
  1407. auth_hdr_value)
  1408. # if the password manager can't find a password, the handler won't
  1409. # handle the HTTP auth error
  1410. password_manager.user = password_manager.password = None
  1411. http_handler.reset()
  1412. opener.open(request_url)
  1413. self.assertEqual(len(http_handler.requests), 1)
  1414. self.assertFalse(http_handler.requests[0].has_header(auth_header))
  1415. def test_basic_prior_auth_auto_send(self):
  1416. # Assume already authenticated if is_authenticated=True
  1417. # for APIs like Github that don't return 401
  1418. user, password = "wile", "coyote"
  1419. request_url = "http://acme.example.com/protected"
  1420. http_handler = MockHTTPHandlerCheckAuth(200)
  1421. pwd_manager = HTTPPasswordMgrWithPriorAuth()
  1422. auth_prior_handler = HTTPBasicAuthHandler(pwd_manager)
  1423. auth_prior_handler.add_password(
  1424. None, request_url, user, password, is_authenticated=True)
  1425. self.assertTrue(pwd_manager.is_authenticated(request_url))
  1426. self.assertTrue(pwd_manager.is_authenticated(request_url + '/nested'))
  1427. self.assertFalse(pwd_manager.is_authenticated(request_url + 'plain'))
  1428. opener = OpenerDirector()
  1429. opener.add_handler(auth_prior_handler)
  1430. opener.add_handler(http_handler)
  1431. opener.open(request_url)
  1432. # expect request to be sent with auth header
  1433. self.assertTrue(http_handler.has_auth_header)
  1434. def test_basic_prior_auth_send_after_first_success(self):
  1435. # Auto send auth header after authentication is successful once
  1436. user, password = 'wile', 'coyote'
  1437. request_url = 'http://acme.example.com/protected'
  1438. realm = 'ACME'
  1439. pwd_manager = HTTPPasswordMgrWithPriorAuth()
  1440. auth_prior_handler = HTTPBasicAuthHandler(pwd_manager)
  1441. auth_prior_handler.add_password(realm, request_url, user, password)
  1442. is_auth = pwd_manager.is_authenticated(request_url)
  1443. self.assertFalse(is_auth)
  1444. opener = OpenerDirector()
  1445. opener.add_handler(auth_prior_handler)
  1446. http_handler = MockHTTPHandler(
  1447. 401, 'WWW-Authenticate: Basic realm="%s"\r\n\r\n' % None)
  1448. opener.add_handler(http_handler)
  1449. opener.open(request_url)
  1450. is_auth = pwd_manager.is_authenticated(request_url)
  1451. self.assertTrue(is_auth)
  1452. http_handler = MockHTTPHandlerCheckAuth(200)
  1453. self.assertFalse(http_handler.has_auth_header)
  1454. opener = OpenerDirector()
  1455. opener.add_handler(auth_prior_handler)
  1456. opener.add_handler(http_handler)
  1457. # After getting 200 from MockHTTPHandler
  1458. # Next request sends header in the first request
  1459. opener.open(request_url)
  1460. # expect request to be sent with auth header
  1461. self.assertTrue(http_handler.has_auth_header)
  1462. def test_http_closed(self):
  1463. """Test the connection is cleaned up when the response is closed"""
  1464. for (transfer, data) in (
  1465. ("Connection: close", b"data"),
  1466. ("Transfer-Encoding: chunked", b"4\r\ndata\r\n0\r\n\r\n"),
  1467. ("Content-Length: 4", b"data"),
  1468. ):
  1469. header = "HTTP/1.1 200 OK\r\n{}\r\n\r\n".format(transfer)
  1470. conn = test_urllib.fakehttp(header.encode() + data)
  1471. handler = urllib.request.AbstractHTTPHandler()
  1472. req = Request("http://dummy/")
  1473. req.timeout = None
  1474. with handler.do_open(conn, req) as resp:
  1475. resp.read()
  1476. self.assertTrue(conn.fakesock.closed,
  1477. "Connection not closed with {!r}".format(transfer))
  1478. def test_invalid_closed(self):
  1479. """Test the connection is cleaned up after an invalid response"""
  1480. conn = test_urllib.fakehttp(b"")
  1481. handler = urllib.request.AbstractHTTPHandler()
  1482. req = Request("http://dummy/")
  1483. req.timeout = None
  1484. with self.assertRaises(http.client.BadStatusLine):
  1485. handler.do_open(conn, req)
  1486. self.assertTrue(conn.fakesock.closed, "Connection not closed")
  1487. class MiscTests(unittest.TestCase):
  1488. def opener_has_handler(self, opener, handler_class):
  1489. self.assertTrue(any(h.__class__ == handler_class
  1490. for h in opener.handlers))
  1491. def test_build_opener(self):
  1492. class MyHTTPHandler(urllib.request.HTTPHandler):
  1493. pass
  1494. class FooHandler(urllib.request.BaseHandler):
  1495. def foo_open(self):
  1496. pass
  1497. class BarHandler(urllib.request.BaseHandler):
  1498. def bar_open(self):
  1499. pass
  1500. build_opener = urllib.request.build_opener
  1501. o = build_opener(FooHandler, BarHandler)
  1502. self.opener_has_handler(o, FooHandler)
  1503. self.opener_has_handler(o, BarHandler)
  1504. # can take a mix of classes and instances
  1505. o = build_opener(FooHandler, BarHandler())
  1506. self.opener_has_handler(o, FooHandler)
  1507. self.opener_has_handler(o, BarHandler)
  1508. # subclasses of default handlers override default handlers
  1509. o = build_opener(MyHTTPHandler)
  1510. self.opener_has_handler(o, MyHTTPHandler)
  1511. # a particular case of overriding: default handlers can be passed
  1512. # in explicitly
  1513. o = build_opener()
  1514. self.opener_has_handler(o, urllib.request.HTTPHandler)
  1515. o = build_opener(urllib.request.HTTPHandler)
  1516. self.opener_has_handler(o, urllib.request.HTTPHandler)
  1517. o = build_opener(urllib.request.HTTPHandler())
  1518. self.opener_has_handler(o, urllib.request.HTTPHandler)
  1519. # Issue2670: multiple handlers sharing the same base class
  1520. class MyOtherHTTPHandler(urllib.request.HTTPHandler):
  1521. pass
  1522. o = build_opener(MyHTTPHandler, MyOtherHTTPHandler)
  1523. self.opener_has_handler(o, MyHTTPHandler)
  1524. self.opener_has_handler(o, MyOtherHTTPHandler)
  1525. def test_HTTPError_interface(self):
  1526. """
  1527. Issue 13211 reveals that HTTPError didn't implement the URLError
  1528. interface even though HTTPError is a subclass of URLError.
  1529. """
  1530. msg = 'something bad happened'
  1531. url = code = fp = None
  1532. hdrs = 'Content-Length: 42'
  1533. err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
  1534. self.assertTrue(hasattr(err, 'reason'))
  1535. self.assertEqual(err.reason, 'something bad happened')
  1536. self.assertTrue(hasattr(err, 'headers'))
  1537. self.assertEqual(err.headers, 'Content-Length: 42')
  1538. expected_errmsg = 'HTTP Error %s: %s' % (err.code, err.msg)
  1539. self.assertEqual(str(err), expected_errmsg)
  1540. expected_errmsg = '<HTTPError %s: %r>' % (err.code, err.msg)
  1541. self.assertEqual(repr(err), expected_errmsg)
  1542. def test_gh_98778(self):
  1543. x = urllib.error.HTTPError("url", 405, "METHOD NOT ALLOWED", None, None)
  1544. self.assertEqual(getattr(x, "__notes__", ()), ())
  1545. def test_parse_proxy(self):
  1546. parse_proxy_test_cases = [
  1547. ('proxy.example.com',
  1548. (None, None, None, 'proxy.example.com')),
  1549. ('proxy.example.com:3128',
  1550. (None, None, None, 'proxy.example.com:3128')),
  1551. ('proxy.example.com', (None, None, None, 'proxy.example.com')),
  1552. ('proxy.example.com:3128',
  1553. (None, None, None, 'proxy.example.com:3128')),
  1554. # The authority component may optionally include userinfo
  1555. # (assumed to be # username:password):
  1556. ('joe:password@proxy.example.com',
  1557. (None, 'joe', 'password', 'proxy.example.com')),
  1558. ('joe:password@proxy.example.com:3128',
  1559. (None, 'joe', 'password', 'proxy.example.com:3128')),
  1560. #Examples with URLS
  1561. ('http://proxy.example.com/',
  1562. ('http', None, None, 'proxy.example.com')),
  1563. ('http://proxy.example.com:3128/',
  1564. ('http', None, None, 'proxy.example.com:3128')),
  1565. ('http://joe:password@proxy.example.com/',
  1566. ('http', 'joe', 'password', 'proxy.example.com')),
  1567. ('http://joe:password@proxy.example.com:3128',
  1568. ('http', 'joe', 'password', 'proxy.example.com:3128')),
  1569. # Everything after the authority is ignored
  1570. ('ftp://joe:password@proxy.example.com/rubbish:3128',
  1571. ('ftp', 'joe', 'password', 'proxy.example.com')),
  1572. # Test for no trailing '/' case
  1573. ('http://joe:password@proxy.example.com',
  1574. ('http', 'joe', 'password', 'proxy.example.com')),
  1575. # Testcases with '/' character in username, password
  1576. ('http://user/name:password@localhost:22',
  1577. ('http', 'user/name', 'password', 'localhost:22')),
  1578. ('http://username:pass/word@localhost:22',
  1579. ('http', 'username', 'pass/word', 'localhost:22')),
  1580. ('http://user/name:pass/word@localhost:22',
  1581. ('http', 'user/name', 'pass/word', 'localhost:22')),
  1582. ]
  1583. for tc, expected in parse_proxy_test_cases:
  1584. self.assertEqual(_parse_proxy(tc), expected)
  1585. self.assertRaises(ValueError, _parse_proxy, 'file:/ftp.example.com'),
  1586. def test_unsupported_algorithm(self):
  1587. handler = AbstractDigestAuthHandler()
  1588. with self.assertRaises(ValueError) as exc:
  1589. handler.get_algorithm_impls('invalid')
  1590. self.assertEqual(
  1591. str(exc.exception),
  1592. "Unsupported digest authentication algorithm 'invalid'"
  1593. )
  1594. class RequestTests(unittest.TestCase):
  1595. class PutRequest(Request):
  1596. method = 'PUT'
  1597. def setUp(self):
  1598. self.get = Request("http://www.python.org/~jeremy/")
  1599. self.post = Request("http://www.python.org/~jeremy/",
  1600. "data",
  1601. headers={"X-Test": "test"})
  1602. self.head = Request("http://www.python.org/~jeremy/", method='HEAD')
  1603. self.put = self.PutRequest("http://www.python.org/~jeremy/")
  1604. self.force_post = self.PutRequest("http://www.python.org/~jeremy/",
  1605. method="POST")
  1606. def test_method(self):
  1607. self.assertEqual("POST", self.post.get_method())
  1608. self.assertEqual("GET", self.get.get_method())
  1609. self.assertEqual("HEAD", self.head.get_method())
  1610. self.assertEqual("PUT", self.put.get_method())
  1611. self.assertEqual("POST", self.force_post.get_method())
  1612. def test_data(self):
  1613. self.assertFalse(self.get.data)
  1614. self.assertEqual("GET", self.get.get_method())
  1615. self.get.data = "spam"
  1616. self.assertTrue(self.get.data)
  1617. self.assertEqual("POST", self.get.get_method())
  1618. # issue 16464
  1619. # if we change data we need to remove content-length header
  1620. # (cause it's most probably calculated for previous value)
  1621. def test_setting_data_should_remove_content_length(self):
  1622. self.assertNotIn("Content-length", self.get.unredirected_hdrs)
  1623. self.get.add_unredirected_header("Content-length", 42)
  1624. self.assertEqual(42, self.get.unredirected_hdrs["Content-length"])
  1625. self.get.data = "spam"
  1626. self.assertNotIn("Content-length", self.get.unredirected_hdrs)
  1627. # issue 17485 same for deleting data.
  1628. def test_deleting_data_should_remove_content_length(self):
  1629. self.assertNotIn("Content-length", self.get.unredirected_hdrs)
  1630. self.get.data = 'foo'
  1631. self.get.add_unredirected_header("Content-length", 3)
  1632. self.assertEqual(3, self.get.unredirected_hdrs["Content-length"])
  1633. del self.get.data
  1634. self.assertNotIn("Content-length", self.get.unredirected_hdrs)
  1635. def test_get_full_url(self):
  1636. self.assertEqual("http://www.python.org/~jeremy/",
  1637. self.get.get_full_url())
  1638. def test_selector(self):
  1639. self.assertEqual("/~jeremy/", self.get.selector)
  1640. req = Request("http://www.python.org/")
  1641. self.assertEqual("/", req.selector)
  1642. def test_get_type(self):
  1643. self.assertEqual("http", self.get.type)
  1644. def test_get_host(self):
  1645. self.assertEqual("www.python.org", self.get.host)
  1646. def test_get_host_unquote(self):
  1647. req = Request("http://www.%70ython.org/")
  1648. self.assertEqual("www.python.org", req.host)
  1649. def test_proxy(self):
  1650. self.assertFalse(self.get.has_proxy())
  1651. self.get.set_proxy("www.perl.org", "http")
  1652. self.assertTrue(self.get.has_proxy())
  1653. self.assertEqual("www.python.org", self.get.origin_req_host)
  1654. self.assertEqual("www.perl.org", self.get.host)
  1655. def test_wrapped_url(self):
  1656. req = Request("<URL:http://www.python.org>")
  1657. self.assertEqual("www.python.org", req.host)
  1658. def test_url_fragment(self):
  1659. req = Request("http://www.python.org/?qs=query#fragment=true")
  1660. self.assertEqual("/?qs=query", req.selector)
  1661. req = Request("http://www.python.org/#fun=true")
  1662. self.assertEqual("/", req.selector)
  1663. # Issue 11703: geturl() omits fragment in the original URL.
  1664. url = 'http://docs.python.org/library/urllib2.html#OK'
  1665. req = Request(url)
  1666. self.assertEqual(req.get_full_url(), url)
  1667. def test_url_fullurl_get_full_url(self):
  1668. urls = ['http://docs.python.org',
  1669. 'http://docs.python.org/library/urllib2.html#OK',
  1670. 'http://www.python.org/?qs=query#fragment=true']
  1671. for url in urls:
  1672. req = Request(url)
  1673. self.assertEqual(req.get_full_url(), req.full_url)
  1674. if __name__ == "__main__":
  1675. unittest.main()