test_webclient.py 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for the old L{twisted.web.client} APIs, C{getPage} and friends.
  5. """
  6. from __future__ import division, absolute_import
  7. import os
  8. from errno import ENOSPC
  9. try:
  10. from urlparse import urlparse, urljoin
  11. except ImportError:
  12. from urllib.parse import urlparse, urljoin
  13. from twisted.python.compat import networkString, nativeString, intToBytes
  14. from twisted.trial import unittest, util
  15. from twisted.web import server, client, error, resource
  16. from twisted.web.static import Data
  17. from twisted.web.util import Redirect
  18. from twisted.internet import reactor, defer, interfaces
  19. from twisted.python.filepath import FilePath
  20. from twisted.python.log import msg
  21. from twisted.protocols.policies import WrappingFactory
  22. from twisted.test.proto_helpers import (
  23. StringTransport, waitUntilAllDisconnected)
  24. try:
  25. from twisted.internet import ssl
  26. except:
  27. ssl = None
  28. from twisted import test
  29. serverPEM = FilePath(test.__file__).sibling('server.pem')
  30. serverPEMPath = serverPEM.asBytesMode().path
  31. class ExtendedRedirect(resource.Resource):
  32. """
  33. Redirection resource.
  34. The HTTP status code is set according to the C{code} query parameter.
  35. @type lastMethod: C{bytes}
  36. @ivar lastMethod: Last handled HTTP request method
  37. """
  38. isLeaf = True
  39. lastMethod = None
  40. def __init__(self, url):
  41. resource.Resource.__init__(self)
  42. self.url = url
  43. def render(self, request):
  44. if self.lastMethod:
  45. self.lastMethod = request.method
  46. return b"OK Thnx!"
  47. else:
  48. self.lastMethod = request.method
  49. code = int(request.args[b'code'][0])
  50. return self.redirectTo(self.url, request, code)
  51. def getChild(self, name, request):
  52. return self
  53. def redirectTo(self, url, request, code):
  54. request.setResponseCode(code)
  55. request.setHeader(b"location", url)
  56. return b"OK Bye!"
  57. class ForeverTakingResource(resource.Resource):
  58. """
  59. L{ForeverTakingResource} is a resource which never finishes responding
  60. to requests.
  61. """
  62. def __init__(self, write=False):
  63. resource.Resource.__init__(self)
  64. self._write = write
  65. def render(self, request):
  66. if self._write:
  67. request.write(b'some bytes')
  68. return server.NOT_DONE_YET
  69. class ForeverTakingNoReadingResource(resource.Resource):
  70. """
  71. L{ForeverTakingNoReadingResource} is a resource that never finishes
  72. responding and that removes itself from the read loop.
  73. """
  74. def __init__(self):
  75. resource.Resource.__init__(self)
  76. def render(self, request):
  77. # Stop the producing.
  78. request.transport.pauseProducing()
  79. return server.NOT_DONE_YET
  80. class CookieMirrorResource(resource.Resource):
  81. def render(self, request):
  82. l = []
  83. for k,v in sorted(list(request.received_cookies.items())):
  84. l.append((nativeString(k), nativeString(v)))
  85. l.sort()
  86. return networkString(repr(l))
  87. class RawCookieMirrorResource(resource.Resource):
  88. def render(self, request):
  89. header = request.getHeader(b'cookie')
  90. if header is None:
  91. return b'None'
  92. return networkString(repr(nativeString(header)))
  93. class ErrorResource(resource.Resource):
  94. def render(self, request):
  95. request.setResponseCode(401)
  96. if request.args.get(b"showlength"):
  97. request.setHeader(b"content-length", b"0")
  98. return b""
  99. class NoLengthResource(resource.Resource):
  100. def render(self, request):
  101. return b"nolength"
  102. class HostHeaderResource(resource.Resource):
  103. """
  104. A testing resource which renders itself as the value of the host header
  105. from the request.
  106. """
  107. def render(self, request):
  108. return request.requestHeaders.getRawHeaders(b"host")[0]
  109. class PayloadResource(resource.Resource):
  110. """
  111. A testing resource which renders itself as the contents of the request body
  112. as long as the request body is 100 bytes long, otherwise which renders
  113. itself as C{"ERROR"}.
  114. """
  115. def render(self, request):
  116. data = request.content.read()
  117. contentLength = request.requestHeaders.getRawHeaders(b"content-length")[0]
  118. if len(data) != 100 or int(contentLength) != 100:
  119. return b"ERROR"
  120. return data
  121. class DelayResource(resource.Resource):
  122. def __init__(self, seconds):
  123. self.seconds = seconds
  124. def render(self, request):
  125. def response():
  126. request.write(b'some bytes')
  127. request.finish()
  128. reactor.callLater(self.seconds, response)
  129. return server.NOT_DONE_YET
  130. class BrokenDownloadResource(resource.Resource):
  131. def render(self, request):
  132. # only sends 3 bytes even though it claims to send 5
  133. request.setHeader(b"content-length", b"5")
  134. request.write(b'abc')
  135. return b''
  136. class CountingRedirect(Redirect):
  137. """
  138. A L{Redirect} resource that keeps track of the number of times the
  139. resource has been accessed.
  140. """
  141. def __init__(self, *a, **kw):
  142. Redirect.__init__(self, *a, **kw)
  143. self.count = 0
  144. def render(self, request):
  145. self.count += 1
  146. return Redirect.render(self, request)
  147. class CountingResource(resource.Resource):
  148. """
  149. A resource that keeps track of the number of times it has been accessed.
  150. """
  151. def __init__(self):
  152. resource.Resource.__init__(self)
  153. self.count = 0
  154. def render(self, request):
  155. self.count += 1
  156. return b"Success"
  157. class URLJoinTests(unittest.TestCase):
  158. """
  159. Tests for L{client._urljoin}.
  160. """
  161. def test_noFragments(self):
  162. """
  163. L{client._urljoin} does not include a fragment identifier in the
  164. resulting URL if neither the base nor the new path include a fragment
  165. identifier.
  166. """
  167. self.assertEqual(
  168. client._urljoin(b'http://foo.com/bar', b'/quux'),
  169. b'http://foo.com/quux')
  170. self.assertEqual(
  171. client._urljoin(b'http://foo.com/bar#', b'/quux'),
  172. b'http://foo.com/quux')
  173. self.assertEqual(
  174. client._urljoin(b'http://foo.com/bar', b'/quux#'),
  175. b'http://foo.com/quux')
  176. def test_preserveFragments(self):
  177. """
  178. L{client._urljoin} preserves the fragment identifier from either the
  179. new path or the base URL respectively, as specified in the HTTP 1.1 bis
  180. draft.
  181. @see: U{https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-22#section-7.1.2}
  182. """
  183. self.assertEqual(
  184. client._urljoin(b'http://foo.com/bar#frag', b'/quux'),
  185. b'http://foo.com/quux#frag')
  186. self.assertEqual(
  187. client._urljoin(b'http://foo.com/bar', b'/quux#frag2'),
  188. b'http://foo.com/quux#frag2')
  189. self.assertEqual(
  190. client._urljoin(b'http://foo.com/bar#frag', b'/quux#frag2'),
  191. b'http://foo.com/quux#frag2')
  192. class HTTPPageGetterTests(unittest.TestCase):
  193. """
  194. Tests for L{HTTPPagerGetter}, the HTTP client protocol implementation
  195. used to implement L{getPage}.
  196. """
  197. suppress = [util.suppress(category=DeprecationWarning)]
  198. def test_earlyHeaders(self):
  199. """
  200. When a connection is made, L{HTTPPagerGetter} sends the headers from
  201. its factory's C{headers} dict. If I{Host} or I{Content-Length} is
  202. present in this dict, the values are not sent, since they are sent with
  203. special values before the C{headers} dict is processed. If
  204. I{User-Agent} is present in the dict, it overrides the value of the
  205. C{agent} attribute of the factory. If I{Cookie} is present in the
  206. dict, its value is added to the values from the factory's C{cookies}
  207. attribute.
  208. """
  209. factory = client.HTTPClientFactory(
  210. b'http://foo/bar',
  211. agent=b"foobar",
  212. cookies={b'baz': b'quux'},
  213. postdata=b"some data",
  214. headers={
  215. b'Host': b'example.net',
  216. b'User-Agent': b'fooble',
  217. b'Cookie': b'blah blah',
  218. b'Content-Length': b'12981',
  219. b'Useful': b'value'})
  220. transport = StringTransport()
  221. protocol = client.HTTPPageGetter()
  222. protocol.factory = factory
  223. protocol.makeConnection(transport)
  224. result = transport.value()
  225. for expectedHeader in [
  226. b"Host: example.net\r\n",
  227. b"User-Agent: foobar\r\n",
  228. b"Content-Length: 9\r\n",
  229. b"Useful: value\r\n",
  230. b"connection: close\r\n",
  231. b"Cookie: blah blah; baz=quux\r\n"]:
  232. self.assertIn(expectedHeader, result)
  233. class WebClientTests(unittest.TestCase):
  234. suppress = [util.suppress(category=DeprecationWarning)]
  235. def _listen(self, site):
  236. return reactor.listenTCP(0, site, interface="127.0.0.1")
  237. def setUp(self):
  238. self.agent = None # for twisted.web.client.Agent test
  239. self.cleanupServerConnections = 0
  240. r = resource.Resource()
  241. r.putChild(b"file", Data(b"0123456789", "text/html"))
  242. r.putChild(b"redirect", Redirect(b"/file"))
  243. self.infiniteRedirectResource = CountingRedirect(b"/infiniteRedirect")
  244. r.putChild(b"infiniteRedirect", self.infiniteRedirectResource)
  245. r.putChild(b"wait", ForeverTakingResource())
  246. r.putChild(b"write-then-wait", ForeverTakingResource(write=True))
  247. r.putChild(b"never-read", ForeverTakingNoReadingResource())
  248. r.putChild(b"error", ErrorResource())
  249. r.putChild(b"nolength", NoLengthResource())
  250. r.putChild(b"host", HostHeaderResource())
  251. r.putChild(b"payload", PayloadResource())
  252. r.putChild(b"broken", BrokenDownloadResource())
  253. r.putChild(b"cookiemirror", CookieMirrorResource())
  254. r.putChild(b'delay1', DelayResource(1))
  255. r.putChild(b'delay2', DelayResource(2))
  256. self.afterFoundGetCounter = CountingResource()
  257. r.putChild(b"afterFoundGetCounter", self.afterFoundGetCounter)
  258. r.putChild(b"afterFoundGetRedirect", Redirect(b"/afterFoundGetCounter"))
  259. miscasedHead = Data(b"miscased-head GET response content", "major/minor")
  260. miscasedHead.render_Head = lambda request: b"miscased-head content"
  261. r.putChild(b"miscased-head", miscasedHead)
  262. self.extendedRedirect = ExtendedRedirect(b'/extendedRedirect')
  263. r.putChild(b"extendedRedirect", self.extendedRedirect)
  264. self.site = server.Site(r, timeout=None)
  265. self.wrapper = WrappingFactory(self.site)
  266. self.port = self._listen(self.wrapper)
  267. self.portno = self.port.getHost().port
  268. def tearDown(self):
  269. if self.agent:
  270. # clean up connections for twisted.web.client.Agent test.
  271. self.agent.closeCachedConnections()
  272. self.agent = None
  273. # If the test indicated it might leave some server-side connections
  274. # around, clean them up.
  275. connections = list(self.wrapper.protocols.keys())
  276. # If there are fewer server-side connections than requested,
  277. # that's okay. Some might have noticed that the client closed
  278. # the connection and cleaned up after themselves.
  279. for n in range(min(len(connections), self.cleanupServerConnections)):
  280. proto = connections.pop()
  281. msg("Closing %r" % (proto,))
  282. proto.transport.abortConnection()
  283. d = self.port.stopListening()
  284. return defer.DeferredList([waitUntilAllDisconnected(
  285. reactor, list(self.wrapper.protocols.keys())), d])
  286. def getURL(self, path):
  287. host = "http://127.0.0.1:%d/" % self.portno
  288. return networkString(urljoin(host, nativeString(path)))
  289. def testPayload(self):
  290. s = b"0123456789" * 10
  291. return client.getPage(self.getURL("payload"), postdata=s
  292. ).addCallback(self.assertEqual, s
  293. )
  294. def test_getPageBrokenDownload(self):
  295. """
  296. If the connection is closed before the number of bytes indicated by
  297. I{Content-Length} have been received, the L{Deferred} returned by
  298. L{getPage} fails with L{PartialDownloadError}.
  299. """
  300. d = client.getPage(self.getURL("broken"))
  301. d = self.assertFailure(d, client.PartialDownloadError)
  302. d.addCallback(lambda exc: self.assertEqual(exc.response, b"abc"))
  303. return d
  304. def test_downloadPageBrokenDownload(self):
  305. """
  306. If the connection is closed before the number of bytes indicated by
  307. I{Content-Length} have been received, the L{Deferred} returned by
  308. L{downloadPage} fails with L{PartialDownloadError}.
  309. """
  310. # test what happens when download gets disconnected in the middle
  311. path = FilePath(self.mktemp())
  312. d = client.downloadPage(self.getURL("broken"), path.path)
  313. d = self.assertFailure(d, client.PartialDownloadError)
  314. def checkResponse(response):
  315. """
  316. The HTTP status code from the server is propagated through the
  317. C{PartialDownloadError}.
  318. """
  319. self.assertEqual(response.status, b"200")
  320. self.assertEqual(response.message, b"OK")
  321. return response
  322. d.addCallback(checkResponse)
  323. def cbFailed(ignored):
  324. self.assertEqual(path.getContent(), b"abc")
  325. d.addCallback(cbFailed)
  326. return d
  327. def test_downloadPageLogsFileCloseError(self):
  328. """
  329. If there is an exception closing the file being written to after the
  330. connection is prematurely closed, that exception is logged.
  331. """
  332. class BrokenFile:
  333. def write(self, bytes):
  334. pass
  335. def close(self):
  336. raise IOError(ENOSPC, "No file left on device")
  337. d = client.downloadPage(self.getURL("broken"), BrokenFile())
  338. d = self.assertFailure(d, client.PartialDownloadError)
  339. def cbFailed(ignored):
  340. self.assertEqual(len(self.flushLoggedErrors(IOError)), 1)
  341. d.addCallback(cbFailed)
  342. return d
  343. def testHostHeader(self):
  344. # if we pass Host header explicitly, it should be used, otherwise
  345. # it should extract from url
  346. return defer.gatherResults([
  347. client.getPage(self.getURL("host")).addCallback(
  348. self.assertEqual, b"127.0.0.1:" + intToBytes(self.portno)),
  349. client.getPage(self.getURL("host"),
  350. headers={b"Host": b"www.example.com"}).addCallback(
  351. self.assertEqual, b"www.example.com")])
  352. def test_getPage(self):
  353. """
  354. L{client.getPage} returns a L{Deferred} which is called back with
  355. the body of the response if the default method B{GET} is used.
  356. """
  357. d = client.getPage(self.getURL("file"))
  358. d.addCallback(self.assertEqual, b"0123456789")
  359. return d
  360. def test_getPageHEAD(self):
  361. """
  362. L{client.getPage} returns a L{Deferred} which is called back with
  363. the empty string if the method is I{HEAD} and there is a successful
  364. response code.
  365. """
  366. d = client.getPage(self.getURL("file"), method=b"HEAD")
  367. d.addCallback(self.assertEqual, b"")
  368. return d
  369. def test_getPageNotQuiteHEAD(self):
  370. """
  371. If the request method is a different casing of I{HEAD} (ie, not all
  372. capitalized) then it is not a I{HEAD} request and the response body
  373. is returned.
  374. """
  375. d = client.getPage(self.getURL("miscased-head"), method=b'Head')
  376. d.addCallback(self.assertEqual, b"miscased-head content")
  377. return d
  378. def test_timeoutNotTriggering(self):
  379. """
  380. When a non-zero timeout is passed to L{getPage} and the page is
  381. retrieved before the timeout period elapses, the L{Deferred} is
  382. called back with the contents of the page.
  383. """
  384. d = client.getPage(self.getURL("host"), timeout=100)
  385. d.addCallback(self.assertEqual,
  386. networkString("127.0.0.1:%s" % (self.portno,)))
  387. return d
  388. def test_timeoutTriggering(self):
  389. """
  390. When a non-zero timeout is passed to L{getPage} and that many
  391. seconds elapse before the server responds to the request. the
  392. L{Deferred} is errbacked with a L{error.TimeoutError}.
  393. """
  394. # This will probably leave some connections around.
  395. self.cleanupServerConnections = 1
  396. return self.assertFailure(
  397. client.getPage(self.getURL("wait"), timeout=0.000001),
  398. defer.TimeoutError)
  399. def testDownloadPage(self):
  400. downloads = []
  401. downloadData = [("file", self.mktemp(), b"0123456789"),
  402. ("nolength", self.mktemp(), b"nolength")]
  403. for (url, name, data) in downloadData:
  404. d = client.downloadPage(self.getURL(url), name)
  405. d.addCallback(self._cbDownloadPageTest, data, name)
  406. downloads.append(d)
  407. return defer.gatherResults(downloads)
  408. def _cbDownloadPageTest(self, ignored, data, name):
  409. with open(name, "rb") as f:
  410. bytes = f.read()
  411. self.assertEqual(bytes, data)
  412. def testDownloadPageError1(self):
  413. class errorfile:
  414. def write(self, data):
  415. raise IOError("badness happened during write")
  416. def close(self):
  417. pass
  418. ef = errorfile()
  419. return self.assertFailure(
  420. client.downloadPage(self.getURL("file"), ef),
  421. IOError)
  422. def testDownloadPageError2(self):
  423. class errorfile:
  424. def write(self, data):
  425. pass
  426. def close(self):
  427. raise IOError("badness happened during close")
  428. ef = errorfile()
  429. return self.assertFailure(
  430. client.downloadPage(self.getURL("file"), ef),
  431. IOError)
  432. def testDownloadPageError3(self):
  433. # make sure failures in open() are caught too. This is tricky.
  434. # Might only work on posix.
  435. open("unwritable", "wb").close()
  436. os.chmod("unwritable", 0) # make it unwritable (to us)
  437. d = self.assertFailure(
  438. client.downloadPage(self.getURL("file"), "unwritable"),
  439. IOError)
  440. d.addBoth(self._cleanupDownloadPageError3)
  441. return d
  442. def _cleanupDownloadPageError3(self, ignored):
  443. os.chmod("unwritable", 0o700)
  444. os.unlink("unwritable")
  445. return ignored
  446. def _downloadTest(self, method):
  447. dl = []
  448. for (url, code) in [("nosuchfile", b"404"), ("error", b"401"),
  449. ("error?showlength=1", b"401")]:
  450. d = method(url)
  451. d = self.assertFailure(d, error.Error)
  452. d.addCallback(lambda exc, code=code: self.assertEqual(exc.args[0], code))
  453. dl.append(d)
  454. return defer.DeferredList(dl, fireOnOneErrback=True)
  455. def testServerError(self):
  456. return self._downloadTest(lambda url: client.getPage(self.getURL(url)))
  457. def testDownloadServerError(self):
  458. return self._downloadTest(lambda url: client.downloadPage(self.getURL(url), url.split('?')[0]))
  459. def testFactoryInfo(self):
  460. url = self.getURL('file')
  461. uri = client.URI.fromBytes(url)
  462. factory = client.HTTPClientFactory(url)
  463. reactor.connectTCP(nativeString(uri.host), uri.port, factory)
  464. return factory.deferred.addCallback(self._cbFactoryInfo, factory)
  465. def _cbFactoryInfo(self, ignoredResult, factory):
  466. self.assertEqual(factory.status, b'200')
  467. self.assertTrue(factory.version.startswith(b'HTTP/'))
  468. self.assertEqual(factory.message, b'OK')
  469. self.assertEqual(factory.response_headers[b'content-length'][0], b'10')
  470. def test_followRedirect(self):
  471. """
  472. By default, L{client.getPage} follows redirects and returns the content
  473. of the target resource.
  474. """
  475. d = client.getPage(self.getURL("redirect"))
  476. d.addCallback(self.assertEqual, b"0123456789")
  477. return d
  478. def test_noFollowRedirect(self):
  479. """
  480. If C{followRedirect} is passed a false value, L{client.getPage} does not
  481. follow redirects and returns a L{Deferred} which fails with
  482. L{error.PageRedirect} when it encounters one.
  483. """
  484. d = self.assertFailure(
  485. client.getPage(self.getURL("redirect"), followRedirect=False),
  486. error.PageRedirect)
  487. d.addCallback(self._cbCheckLocation)
  488. return d
  489. def _cbCheckLocation(self, exc):
  490. self.assertEqual(exc.location, b"/file")
  491. def test_infiniteRedirection(self):
  492. """
  493. When more than C{redirectLimit} HTTP redirects are encountered, the
  494. page request fails with L{InfiniteRedirection}.
  495. """
  496. def checkRedirectCount(*a):
  497. self.assertEqual(f._redirectCount, 13)
  498. self.assertEqual(self.infiniteRedirectResource.count, 13)
  499. f = client._makeGetterFactory(
  500. self.getURL('infiniteRedirect'),
  501. client.HTTPClientFactory,
  502. redirectLimit=13)
  503. d = self.assertFailure(f.deferred, error.InfiniteRedirection)
  504. d.addCallback(checkRedirectCount)
  505. return d
  506. def test_isolatedFollowRedirect(self):
  507. """
  508. C{client.HTTPPagerGetter} instances each obey the C{followRedirect}
  509. value passed to the L{client.getPage} call which created them.
  510. """
  511. d1 = client.getPage(self.getURL('redirect'), followRedirect=True)
  512. d2 = client.getPage(self.getURL('redirect'), followRedirect=False)
  513. d = self.assertFailure(d2, error.PageRedirect
  514. ).addCallback(lambda dummy: d1)
  515. return d
  516. def test_afterFoundGet(self):
  517. """
  518. Enabling unsafe redirection behaviour overwrites the method of
  519. redirected C{POST} requests with C{GET}.
  520. """
  521. url = self.getURL('extendedRedirect?code=302')
  522. f = client.HTTPClientFactory(url, followRedirect=True, method=b"POST")
  523. self.assertFalse(
  524. f.afterFoundGet,
  525. "By default, afterFoundGet must be disabled")
  526. def gotPage(page):
  527. self.assertEqual(
  528. self.extendedRedirect.lastMethod,
  529. b"GET",
  530. "With afterFoundGet, the HTTP method must change to GET")
  531. d = client.getPage(
  532. url, followRedirect=True, afterFoundGet=True, method=b"POST")
  533. d.addCallback(gotPage)
  534. return d
  535. def test_downloadAfterFoundGet(self):
  536. """
  537. Passing C{True} for C{afterFoundGet} to L{client.downloadPage} invokes
  538. the same kind of redirect handling as passing that argument to
  539. L{client.getPage} invokes.
  540. """
  541. url = self.getURL('extendedRedirect?code=302')
  542. def gotPage(page):
  543. self.assertEqual(
  544. self.extendedRedirect.lastMethod,
  545. b"GET",
  546. "With afterFoundGet, the HTTP method must change to GET")
  547. d = client.downloadPage(url, "downloadTemp",
  548. followRedirect=True, afterFoundGet=True, method=b"POST")
  549. d.addCallback(gotPage)
  550. return d
  551. def test_afterFoundGetMakesOneRequest(self):
  552. """
  553. When C{afterFoundGet} is C{True}, L{client.getPage} only issues one
  554. request to the server when following the redirect. This is a regression
  555. test, see #4760.
  556. """
  557. def checkRedirectCount(*a):
  558. self.assertEqual(self.afterFoundGetCounter.count, 1)
  559. url = self.getURL('afterFoundGetRedirect')
  560. d = client.getPage(
  561. url, followRedirect=True, afterFoundGet=True, method=b"POST")
  562. d.addCallback(checkRedirectCount)
  563. return d
  564. def test_downloadTimeout(self):
  565. """
  566. If the timeout indicated by the C{timeout} parameter to
  567. L{client.HTTPDownloader.__init__} elapses without the complete response
  568. being received, the L{defer.Deferred} returned by
  569. L{client.downloadPage} fires with a L{Failure} wrapping a
  570. L{defer.TimeoutError}.
  571. """
  572. self.cleanupServerConnections = 2
  573. # Verify the behavior if no bytes are ever written.
  574. first = client.downloadPage(
  575. self.getURL("wait"),
  576. self.mktemp(), timeout=0.01)
  577. # Verify the behavior if some bytes are written but then the request
  578. # never completes.
  579. second = client.downloadPage(
  580. self.getURL("write-then-wait"),
  581. self.mktemp(), timeout=0.01)
  582. return defer.gatherResults([
  583. self.assertFailure(first, defer.TimeoutError),
  584. self.assertFailure(second, defer.TimeoutError)])
  585. def test_downloadTimeoutsWorkWithoutReading(self):
  586. """
  587. If the timeout indicated by the C{timeout} parameter to
  588. L{client.HTTPDownloader.__init__} elapses without the complete response
  589. being received, the L{defer.Deferred} returned by
  590. L{client.downloadPage} fires with a L{Failure} wrapping a
  591. L{defer.TimeoutError}, even if the remote peer isn't reading data from
  592. the socket.
  593. """
  594. self.cleanupServerConnections = 1
  595. # The timeout here needs to be slightly longer to give the resource a
  596. # change to stop the reading.
  597. d = client.downloadPage(
  598. self.getURL("never-read"),
  599. self.mktemp(), timeout=0.05)
  600. return self.assertFailure(d, defer.TimeoutError)
  601. def test_downloadHeaders(self):
  602. """
  603. After L{client.HTTPDownloader.deferred} fires, the
  604. L{client.HTTPDownloader} instance's C{status} and C{response_headers}
  605. attributes are populated with the values from the response.
  606. """
  607. def checkHeaders(factory):
  608. self.assertEqual(factory.status, b'200')
  609. self.assertEqual(factory.response_headers[b'content-type'][0], b'text/html')
  610. self.assertEqual(factory.response_headers[b'content-length'][0], b'10')
  611. os.unlink(factory.fileName)
  612. factory = client._makeGetterFactory(
  613. self.getURL('file'),
  614. client.HTTPDownloader,
  615. fileOrName=self.mktemp())
  616. return factory.deferred.addCallback(lambda _: checkHeaders(factory))
  617. def test_downloadCookies(self):
  618. """
  619. The C{cookies} dict passed to the L{client.HTTPDownloader}
  620. initializer is used to populate the I{Cookie} header included in the
  621. request sent to the server.
  622. """
  623. output = self.mktemp()
  624. factory = client._makeGetterFactory(
  625. self.getURL('cookiemirror'),
  626. client.HTTPDownloader,
  627. fileOrName=output,
  628. cookies={b'foo': b'bar'})
  629. def cbFinished(ignored):
  630. self.assertEqual(
  631. FilePath(output).getContent(),
  632. b"[('foo', 'bar')]")
  633. factory.deferred.addCallback(cbFinished)
  634. return factory.deferred
  635. def test_downloadRedirectLimit(self):
  636. """
  637. When more than C{redirectLimit} HTTP redirects are encountered, the
  638. page request fails with L{InfiniteRedirection}.
  639. """
  640. def checkRedirectCount(*a):
  641. self.assertEqual(f._redirectCount, 7)
  642. self.assertEqual(self.infiniteRedirectResource.count, 7)
  643. f = client._makeGetterFactory(
  644. self.getURL('infiniteRedirect'),
  645. client.HTTPDownloader,
  646. fileOrName=self.mktemp(),
  647. redirectLimit=7)
  648. d = self.assertFailure(f.deferred, error.InfiniteRedirection)
  649. d.addCallback(checkRedirectCount)
  650. return d
  651. def test_setURL(self):
  652. """
  653. L{client.HTTPClientFactory.setURL} alters the scheme, host, port and
  654. path for absolute URLs.
  655. """
  656. url = b'http://example.com'
  657. f = client.HTTPClientFactory(url)
  658. self.assertEqual(
  659. (url, b'http', b'example.com', 80, b'/'),
  660. (f.url, f.scheme, f.host, f.port, f.path))
  661. def test_setURLRemovesFragment(self):
  662. """
  663. L{client.HTTPClientFactory.setURL} removes the fragment identifier from
  664. the path component.
  665. """
  666. f = client.HTTPClientFactory(b'http://example.com')
  667. url = b'https://foo.com:8443/bar;123?a#frag'
  668. f.setURL(url)
  669. self.assertEqual(
  670. (url, b'https', b'foo.com', 8443, b'/bar;123?a'),
  671. (f.url, f.scheme, f.host, f.port, f.path))
  672. def test_setURLRelativePath(self):
  673. """
  674. L{client.HTTPClientFactory.setURL} alters the path in a relative URL.
  675. """
  676. f = client.HTTPClientFactory(b'http://example.com')
  677. url = b'/hello'
  678. f.setURL(url)
  679. self.assertEqual(
  680. (url, b'http', b'example.com', 80, b'/hello'),
  681. (f.url, f.scheme, f.host, f.port, f.path))
  682. class WebClientSSLTests(WebClientTests):
  683. def _listen(self, site):
  684. return reactor.listenSSL(
  685. 0, site,
  686. contextFactory=ssl.DefaultOpenSSLContextFactory(
  687. serverPEMPath, serverPEMPath),
  688. interface="127.0.0.1")
  689. def getURL(self, path):
  690. return networkString("https://127.0.0.1:%d/%s" % (self.portno, path))
  691. def testFactoryInfo(self):
  692. url = self.getURL('file')
  693. uri = client.URI.fromBytes(url)
  694. factory = client.HTTPClientFactory(url)
  695. reactor.connectSSL(nativeString(uri.host), uri.port, factory,
  696. ssl.ClientContextFactory())
  697. # The base class defines _cbFactoryInfo correctly for this
  698. return factory.deferred.addCallback(self._cbFactoryInfo, factory)
  699. class WebClientRedirectBetweenSSLandPlainTextTests(unittest.TestCase):
  700. suppress = [util.suppress(category=DeprecationWarning)]
  701. def getHTTPS(self, path):
  702. return networkString("https://127.0.0.1:%d/%s" % (self.tlsPortno, path))
  703. def getHTTP(self, path):
  704. return networkString("http://127.0.0.1:%d/%s" % (self.plainPortno, path))
  705. def setUp(self):
  706. plainRoot = Data(b'not me', 'text/plain')
  707. tlsRoot = Data(b'me neither', 'text/plain')
  708. plainSite = server.Site(plainRoot, timeout=None)
  709. tlsSite = server.Site(tlsRoot, timeout=None)
  710. self.tlsPort = reactor.listenSSL(
  711. 0, tlsSite,
  712. contextFactory=ssl.DefaultOpenSSLContextFactory(
  713. serverPEMPath, serverPEMPath),
  714. interface="127.0.0.1")
  715. self.plainPort = reactor.listenTCP(0, plainSite, interface="127.0.0.1")
  716. self.plainPortno = self.plainPort.getHost().port
  717. self.tlsPortno = self.tlsPort.getHost().port
  718. plainRoot.putChild(b'one', Redirect(self.getHTTPS('two')))
  719. tlsRoot.putChild(b'two', Redirect(self.getHTTP('three')))
  720. plainRoot.putChild(b'three', Redirect(self.getHTTPS('four')))
  721. tlsRoot.putChild(b'four', Data(b'FOUND IT!', 'text/plain'))
  722. def tearDown(self):
  723. ds = list(
  724. map(defer.maybeDeferred,
  725. [self.plainPort.stopListening, self.tlsPort.stopListening]))
  726. return defer.gatherResults(ds)
  727. def testHoppingAround(self):
  728. return client.getPage(self.getHTTP("one")
  729. ).addCallback(self.assertEqual, b"FOUND IT!"
  730. )
  731. class CookieTests(unittest.TestCase):
  732. suppress = [util.suppress(category=DeprecationWarning)]
  733. def _listen(self, site):
  734. return reactor.listenTCP(0, site, interface="127.0.0.1")
  735. def setUp(self):
  736. root = Data(b'El toro!', 'text/plain')
  737. root.putChild(b"cookiemirror", CookieMirrorResource())
  738. root.putChild(b"rawcookiemirror", RawCookieMirrorResource())
  739. site = server.Site(root, timeout=None)
  740. self.port = self._listen(site)
  741. self.portno = self.port.getHost().port
  742. def tearDown(self):
  743. return self.port.stopListening()
  744. def getHTTP(self, path):
  745. return networkString("http://127.0.0.1:%d/%s" % (self.portno, path))
  746. def testNoCookies(self):
  747. return client.getPage(self.getHTTP("cookiemirror")
  748. ).addCallback(self.assertEqual, b"[]"
  749. )
  750. def testSomeCookies(self):
  751. cookies = {b'foo': b'bar', b'baz': b'quux'}
  752. return client.getPage(self.getHTTP("cookiemirror"), cookies=cookies
  753. ).addCallback(self.assertEqual, b"[('baz', 'quux'), ('foo', 'bar')]"
  754. )
  755. def testRawNoCookies(self):
  756. return client.getPage(self.getHTTP("rawcookiemirror")
  757. ).addCallback(self.assertEqual, b"None"
  758. )
  759. def testRawSomeCookies(self):
  760. cookies = {b'foo': b'bar', b'baz': b'quux'}
  761. return client.getPage(self.getHTTP("rawcookiemirror"), cookies=cookies
  762. ).addCallback(self.assertIn,
  763. (b"'foo=bar; baz=quux'", b"'baz=quux; foo=bar'")
  764. )
  765. def testCookieHeaderParsing(self):
  766. factory = client.HTTPClientFactory(b'http://foo.example.com/')
  767. proto = factory.buildProtocol('127.42.42.42')
  768. transport = StringTransport()
  769. proto.makeConnection(transport)
  770. for line in [
  771. b'200 Ok',
  772. b'Squash: yes',
  773. b'Hands: stolen',
  774. b'Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT',
  775. b'Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/',
  776. b'Set-Cookie: SHIPPING=FEDEX; path=/foo',
  777. b'',
  778. b'body',
  779. b'more body',
  780. ]:
  781. proto.dataReceived(line + b'\r\n')
  782. self.assertEqual(transport.value(),
  783. b'GET / HTTP/1.0\r\n'
  784. b'Host: foo.example.com\r\n'
  785. b'User-Agent: Twisted PageGetter\r\n'
  786. b'\r\n')
  787. self.assertEqual(factory.cookies,
  788. {
  789. b'CUSTOMER': b'WILE_E_COYOTE',
  790. b'PART_NUMBER': b'ROCKET_LAUNCHER_0001',
  791. b'SHIPPING': b'FEDEX',
  792. })
  793. class HostHeaderTests(unittest.TestCase):
  794. """
  795. Test that L{HTTPClientFactory} includes the port in the host header
  796. if needed.
  797. """
  798. suppress = [util.suppress(category=DeprecationWarning)]
  799. def _getHost(self, bytes):
  800. """
  801. Retrieve the value of the I{Host} header from the serialized
  802. request given by C{bytes}.
  803. """
  804. for line in bytes.split(b'\r\n'):
  805. try:
  806. name, value = line.split(b':', 1)
  807. if name.strip().lower() == b'host':
  808. return value.strip()
  809. except ValueError:
  810. pass
  811. def test_HTTPDefaultPort(self):
  812. """
  813. No port should be included in the host header when connecting to the
  814. default HTTP port.
  815. """
  816. factory = client.HTTPClientFactory(b'http://foo.example.com/')
  817. proto = factory.buildProtocol(b'127.42.42.42')
  818. proto.makeConnection(StringTransport())
  819. self.assertEqual(self._getHost(proto.transport.value()),
  820. b'foo.example.com')
  821. def test_HTTPPort80(self):
  822. """
  823. No port should be included in the host header when connecting to the
  824. default HTTP port even if it is in the URL.
  825. """
  826. factory = client.HTTPClientFactory(b'http://foo.example.com:80/')
  827. proto = factory.buildProtocol('127.42.42.42')
  828. proto.makeConnection(StringTransport())
  829. self.assertEqual(self._getHost(proto.transport.value()),
  830. b'foo.example.com')
  831. def test_HTTPNotPort80(self):
  832. """
  833. The port should be included in the host header when connecting to the
  834. a non default HTTP port.
  835. """
  836. factory = client.HTTPClientFactory(b'http://foo.example.com:8080/')
  837. proto = factory.buildProtocol('127.42.42.42')
  838. proto.makeConnection(StringTransport())
  839. self.assertEqual(self._getHost(proto.transport.value()),
  840. b'foo.example.com:8080')
  841. def test_HTTPSDefaultPort(self):
  842. """
  843. No port should be included in the host header when connecting to the
  844. default HTTPS port.
  845. """
  846. factory = client.HTTPClientFactory(b'https://foo.example.com/')
  847. proto = factory.buildProtocol('127.42.42.42')
  848. proto.makeConnection(StringTransport())
  849. self.assertEqual(self._getHost(proto.transport.value()),
  850. b'foo.example.com')
  851. def test_HTTPSPort443(self):
  852. """
  853. No port should be included in the host header when connecting to the
  854. default HTTPS port even if it is in the URL.
  855. """
  856. factory = client.HTTPClientFactory(b'https://foo.example.com:443/')
  857. proto = factory.buildProtocol('127.42.42.42')
  858. proto.makeConnection(StringTransport())
  859. self.assertEqual(self._getHost(proto.transport.value()),
  860. b'foo.example.com')
  861. def test_HTTPSNotPort443(self):
  862. """
  863. The port should be included in the host header when connecting to the
  864. a non default HTTPS port.
  865. """
  866. factory = client.HTTPClientFactory(b'http://foo.example.com:8080/')
  867. proto = factory.buildProtocol('127.42.42.42')
  868. proto.makeConnection(StringTransport())
  869. self.assertEqual(self._getHost(proto.transport.value()),
  870. b'foo.example.com:8080')
  871. if ssl is None or not hasattr(ssl, 'DefaultOpenSSLContextFactory'):
  872. for case in [WebClientSSLTests, WebClientRedirectBetweenSSLandPlainTextTests]:
  873. case.skip = "OpenSSL not present"
  874. if not interfaces.IReactorSSL(reactor, None):
  875. for case in [WebClientSSLTests, WebClientRedirectBetweenSSLandPlainTextTests]:
  876. case.skip = "Reactor doesn't support SSL"
  877. class URITests:
  878. """
  879. Abstract tests for L{twisted.web.client.URI}.
  880. Subclass this and L{unittest.TestCase}. Then provide a value for
  881. C{host} and C{uriHost}.
  882. @ivar host: A host specification for use in tests, must be L{bytes}.
  883. @ivar uriHost: The host specification in URI form, must be a L{bytes}. In
  884. most cases this is identical with C{host}. IPv6 address literals are an
  885. exception, according to RFC 3986 section 3.2.2, as they need to be
  886. enclosed in brackets. In this case this variable is different.
  887. """
  888. def makeURIString(self, template):
  889. """
  890. Replace the string "HOST" in C{template} with this test's host.
  891. Byte strings Python between (and including) versions 3.0 and 3.4
  892. cannot be formatted using C{%} or C{format} so this does a simple
  893. replace.
  894. @type template: L{bytes}
  895. @param template: A string containing "HOST".
  896. @rtype: L{bytes}
  897. @return: A string where "HOST" has been replaced by C{self.host}.
  898. """
  899. self.assertIsInstance(self.host, bytes)
  900. self.assertIsInstance(self.uriHost, bytes)
  901. self.assertIsInstance(template, bytes)
  902. self.assertIn(b"HOST", template)
  903. return template.replace(b"HOST", self.uriHost)
  904. def assertURIEquals(self, uri, scheme, netloc, host, port, path,
  905. params=b'', query=b'', fragment=b''):
  906. """
  907. Assert that all of a L{client.URI}'s components match the expected
  908. values.
  909. @param uri: U{client.URI} instance whose attributes will be checked
  910. for equality.
  911. @type scheme: L{bytes}
  912. @param scheme: URI scheme specifier.
  913. @type netloc: L{bytes}
  914. @param netloc: Network location component.
  915. @type host: L{bytes}
  916. @param host: Host name.
  917. @type port: L{int}
  918. @param port: Port number.
  919. @type path: L{bytes}
  920. @param path: Hierarchical path.
  921. @type params: L{bytes}
  922. @param params: Parameters for last path segment, defaults to C{b''}.
  923. @type query: L{bytes}
  924. @param query: Query string, defaults to C{b''}.
  925. @type fragment: L{bytes}
  926. @param fragment: Fragment identifier, defaults to C{b''}.
  927. """
  928. self.assertEqual(
  929. (scheme, netloc, host, port, path, params, query, fragment),
  930. (uri.scheme, uri.netloc, uri.host, uri.port, uri.path, uri.params,
  931. uri.query, uri.fragment))
  932. def test_parseDefaultPort(self):
  933. """
  934. L{client.URI.fromBytes} by default assumes port 80 for the I{http}
  935. scheme and 443 for the I{https} scheme.
  936. """
  937. uri = client.URI.fromBytes(self.makeURIString(b'http://HOST'))
  938. self.assertEqual(80, uri.port)
  939. # Weird (but commonly accepted) structure uses default port.
  940. uri = client.URI.fromBytes(self.makeURIString(b'http://HOST:'))
  941. self.assertEqual(80, uri.port)
  942. uri = client.URI.fromBytes(self.makeURIString(b'https://HOST'))
  943. self.assertEqual(443, uri.port)
  944. def test_parseCustomDefaultPort(self):
  945. """
  946. L{client.URI.fromBytes} accepts a C{defaultPort} parameter that
  947. overrides the normal default port logic.
  948. """
  949. uri = client.URI.fromBytes(
  950. self.makeURIString(b'http://HOST'), defaultPort=5144)
  951. self.assertEqual(5144, uri.port)
  952. uri = client.URI.fromBytes(
  953. self.makeURIString(b'https://HOST'), defaultPort=5144)
  954. self.assertEqual(5144, uri.port)
  955. def test_netlocHostPort(self):
  956. """
  957. Parsing a I{URI} splits the network location component into I{host} and
  958. I{port}.
  959. """
  960. uri = client.URI.fromBytes(
  961. self.makeURIString(b'http://HOST:5144'))
  962. self.assertEqual(5144, uri.port)
  963. self.assertEqual(self.host, uri.host)
  964. self.assertEqual(self.uriHost + b':5144', uri.netloc)
  965. # Spaces in the hostname are trimmed, the default path is /.
  966. uri = client.URI.fromBytes(self.makeURIString(b'http://HOST '))
  967. self.assertEqual(self.uriHost, uri.netloc)
  968. def test_path(self):
  969. """
  970. Parse the path from a I{URI}.
  971. """
  972. uri = self.makeURIString(b'http://HOST/foo/bar')
  973. parsed = client.URI.fromBytes(uri)
  974. self.assertURIEquals(
  975. parsed,
  976. scheme=b'http',
  977. netloc=self.uriHost,
  978. host=self.host,
  979. port=80,
  980. path=b'/foo/bar')
  981. self.assertEqual(uri, parsed.toBytes())
  982. def test_noPath(self):
  983. """
  984. The path of a I{URI} that has no path is the empty string.
  985. """
  986. uri = self.makeURIString(b'http://HOST')
  987. parsed = client.URI.fromBytes(uri)
  988. self.assertURIEquals(
  989. parsed,
  990. scheme=b'http',
  991. netloc=self.uriHost,
  992. host=self.host,
  993. port=80,
  994. path=b'')
  995. self.assertEqual(uri, parsed.toBytes())
  996. def test_emptyPath(self):
  997. """
  998. The path of a I{URI} with an empty path is C{b'/'}.
  999. """
  1000. uri = self.makeURIString(b'http://HOST/')
  1001. self.assertURIEquals(
  1002. client.URI.fromBytes(uri),
  1003. scheme=b'http',
  1004. netloc=self.uriHost,
  1005. host=self.host,
  1006. port=80,
  1007. path=b'/')
  1008. def test_param(self):
  1009. """
  1010. Parse I{URI} parameters from a I{URI}.
  1011. """
  1012. uri = self.makeURIString(b'http://HOST/foo/bar;param')
  1013. parsed = client.URI.fromBytes(uri)
  1014. self.assertURIEquals(
  1015. parsed,
  1016. scheme=b'http',
  1017. netloc=self.uriHost,
  1018. host=self.host,
  1019. port=80,
  1020. path=b'/foo/bar',
  1021. params=b'param')
  1022. self.assertEqual(uri, parsed.toBytes())
  1023. def test_query(self):
  1024. """
  1025. Parse the query string from a I{URI}.
  1026. """
  1027. uri = self.makeURIString(b'http://HOST/foo/bar;param?a=1&b=2')
  1028. parsed = client.URI.fromBytes(uri)
  1029. self.assertURIEquals(
  1030. parsed,
  1031. scheme=b'http',
  1032. netloc=self.uriHost,
  1033. host=self.host,
  1034. port=80,
  1035. path=b'/foo/bar',
  1036. params=b'param',
  1037. query=b'a=1&b=2')
  1038. self.assertEqual(uri, parsed.toBytes())
  1039. def test_fragment(self):
  1040. """
  1041. Parse the fragment identifier from a I{URI}.
  1042. """
  1043. uri = self.makeURIString(b'http://HOST/foo/bar;param?a=1&b=2#frag')
  1044. parsed = client.URI.fromBytes(uri)
  1045. self.assertURIEquals(
  1046. parsed,
  1047. scheme=b'http',
  1048. netloc=self.uriHost,
  1049. host=self.host,
  1050. port=80,
  1051. path=b'/foo/bar',
  1052. params=b'param',
  1053. query=b'a=1&b=2',
  1054. fragment=b'frag')
  1055. self.assertEqual(uri, parsed.toBytes())
  1056. def test_originForm(self):
  1057. """
  1058. L{client.URI.originForm} produces an absolute I{URI} path including
  1059. the I{URI} path.
  1060. """
  1061. uri = client.URI.fromBytes(
  1062. self.makeURIString(b'http://HOST/foo'))
  1063. self.assertEqual(b'/foo', uri.originForm)
  1064. def test_originFormComplex(self):
  1065. """
  1066. L{client.URI.originForm} produces an absolute I{URI} path including
  1067. the I{URI} path, parameters and query string but excludes the fragment
  1068. identifier.
  1069. """
  1070. uri = client.URI.fromBytes(
  1071. self.makeURIString(b'http://HOST/foo;param?a=1#frag'))
  1072. self.assertEqual(b'/foo;param?a=1', uri.originForm)
  1073. def test_originFormNoPath(self):
  1074. """
  1075. L{client.URI.originForm} produces a path of C{b'/'} when the I{URI}
  1076. specifies no path.
  1077. """
  1078. uri = client.URI.fromBytes(self.makeURIString(b'http://HOST'))
  1079. self.assertEqual(b'/', uri.originForm)
  1080. def test_originFormEmptyPath(self):
  1081. """
  1082. L{client.URI.originForm} produces a path of C{b'/'} when the I{URI}
  1083. specifies an empty path.
  1084. """
  1085. uri = client.URI.fromBytes(
  1086. self.makeURIString(b'http://HOST/'))
  1087. self.assertEqual(b'/', uri.originForm)
  1088. def test_externalUnicodeInterference(self):
  1089. """
  1090. L{client.URI.fromBytes} parses the scheme, host, and path elements
  1091. into L{bytes}, even when passed an URL which has previously been passed
  1092. to L{urlparse} as a L{unicode} string.
  1093. """
  1094. goodInput = self.makeURIString(b'http://HOST/path')
  1095. badInput = goodInput.decode('ascii')
  1096. urlparse(badInput)
  1097. uri = client.URI.fromBytes(goodInput)
  1098. self.assertIsInstance(uri.scheme, bytes)
  1099. self.assertIsInstance(uri.host, bytes)
  1100. self.assertIsInstance(uri.path, bytes)
  1101. class URITestsForHostname(URITests, unittest.TestCase):
  1102. """
  1103. Tests for L{twisted.web.client.URI} with host names.
  1104. """
  1105. uriHost = host = b"example.com"
  1106. class URITestsForIPv4(URITests, unittest.TestCase):
  1107. """
  1108. Tests for L{twisted.web.client.URI} with IPv4 host addresses.
  1109. """
  1110. uriHost = host = b"192.168.1.67"
  1111. class URITestsForIPv6(URITests, unittest.TestCase):
  1112. """
  1113. Tests for L{twisted.web.client.URI} with IPv6 host addresses.
  1114. IPv6 addresses must always be surrounded by square braces in URIs. No
  1115. attempt is made to test without.
  1116. """
  1117. host = b"fe80::20c:29ff:fea4:c60"
  1118. uriHost = b"[fe80::20c:29ff:fea4:c60]"
  1119. def test_hostBracketIPv6AddressLiteral(self):
  1120. """
  1121. Brackets around IPv6 addresses are stripped in the host field. The host
  1122. field is then exported with brackets in the output of
  1123. L{client.URI.toBytes}.
  1124. """
  1125. uri = client.URI.fromBytes(b"http://[::1]:80/index.html")
  1126. self.assertEqual(uri.host, b"::1")
  1127. self.assertEqual(uri.netloc, b"[::1]:80")
  1128. self.assertEqual(uri.toBytes(), b'http://[::1]:80/index.html')
  1129. class DeprecationTests(unittest.TestCase):
  1130. """
  1131. Tests that L{client.getPage} and friends are deprecated.
  1132. """
  1133. def test_getPageDeprecated(self):
  1134. """
  1135. L{client.getPage} is deprecated.
  1136. """
  1137. port = reactor.listenTCP(
  1138. 0, server.Site(Data(b'', 'text/plain')), interface="127.0.0.1")
  1139. portno = port.getHost().port
  1140. self.addCleanup(port.stopListening)
  1141. url = networkString("http://127.0.0.1:%d" % (portno,))
  1142. d = client.getPage(url)
  1143. warningInfo = self.flushWarnings([self.test_getPageDeprecated])
  1144. self.assertEqual(len(warningInfo), 1)
  1145. self.assertEqual(warningInfo[0]['category'], DeprecationWarning)
  1146. self.assertEqual(
  1147. warningInfo[0]['message'],
  1148. "twisted.web.client.getPage was deprecated in "
  1149. "Twisted 16.7.0; please use https://pypi.org/project/treq/ or twisted.web.client.Agent instead")
  1150. return d.addErrback(lambda _: None)
  1151. def test_downloadPageDeprecated(self):
  1152. """
  1153. L{client.downloadPage} is deprecated.
  1154. """
  1155. port = reactor.listenTCP(
  1156. 0, server.Site(Data(b'', 'text/plain')), interface="127.0.0.1")
  1157. portno = port.getHost().port
  1158. self.addCleanup(port.stopListening)
  1159. url = networkString("http://127.0.0.1:%d" % (portno,))
  1160. path = FilePath(self.mktemp())
  1161. d = client.downloadPage(url, path.path)
  1162. warningInfo = self.flushWarnings([self.test_downloadPageDeprecated])
  1163. self.assertEqual(len(warningInfo), 1)
  1164. self.assertEqual(warningInfo[0]['category'], DeprecationWarning)
  1165. self.assertEqual(
  1166. warningInfo[0]['message'],
  1167. "twisted.web.client.downloadPage was deprecated in "
  1168. "Twisted 16.7.0; please use https://pypi.org/project/treq/ or twisted.web.client.Agent instead")
  1169. return d.addErrback(lambda _: None)
  1170. def _testDeprecatedClass(self, klass):
  1171. """
  1172. Assert that accessing the given class was deprecated.
  1173. @param klass: The class being deprecated.
  1174. @type klass: L{str}
  1175. """
  1176. getattr(client, klass)
  1177. warningInfo = self.flushWarnings()
  1178. self.assertEqual(len(warningInfo), 1)
  1179. self.assertEqual(warningInfo[0]['category'], DeprecationWarning)
  1180. self.assertEqual(
  1181. warningInfo[0]['message'],
  1182. "twisted.web.client.{} was deprecated in "
  1183. "Twisted 16.7.0: please use https://pypi.org/project/treq/ or twisted.web.client.Agent instead".format(klass))
  1184. def test_httpPageGetterDeprecated(self):
  1185. """
  1186. L{client.HTTPPageGetter} is deprecated.
  1187. """
  1188. self._testDeprecatedClass("HTTPPageGetter")
  1189. def test_httpPageDownloaderDeprecated(self):
  1190. """
  1191. L{client.HTTPPageDownloader} is deprecated.
  1192. """
  1193. self._testDeprecatedClass("HTTPPageDownloader")
  1194. def test_httpClientFactoryDeprecated(self):
  1195. """
  1196. L{client.HTTPClientFactory} is deprecated.
  1197. """
  1198. self._testDeprecatedClass("HTTPClientFactory")
  1199. def test_httpDownloaderDeprecated(self):
  1200. """
  1201. L{client.HTTPDownloader} is deprecated.
  1202. """
  1203. self._testDeprecatedClass("HTTPDownloader")