Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

requesthelper.py 15KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Helpers related to HTTP requests, used by tests.
  5. """
  6. from __future__ import annotations
  7. __all__ = ["DummyChannel", "DummyRequest"]
  8. from io import BytesIO
  9. from typing import Dict, List, Optional
  10. from zope.interface import implementer, verify
  11. from incremental import Version
  12. from twisted.internet.address import IPv4Address, IPv6Address
  13. from twisted.internet.defer import Deferred
  14. from twisted.internet.interfaces import IAddress, ISSLTransport
  15. from twisted.internet.task import Clock
  16. from twisted.python.deprecate import deprecated
  17. from twisted.trial import unittest
  18. from twisted.web._responses import FOUND
  19. from twisted.web.http_headers import Headers
  20. from twisted.web.resource import Resource
  21. from twisted.web.server import NOT_DONE_YET, Session, Site
  22. textLinearWhitespaceComponents = [f"Foo{lw}bar" for lw in ["\r", "\n", "\r\n"]]
  23. sanitizedText = "Foo bar"
  24. bytesLinearWhitespaceComponents = [
  25. component.encode("ascii") for component in textLinearWhitespaceComponents
  26. ]
  27. sanitizedBytes = sanitizedText.encode("ascii")
  28. @implementer(IAddress)
  29. class NullAddress:
  30. """
  31. A null implementation of L{IAddress}.
  32. """
  33. class DummyChannel:
  34. class TCP:
  35. port = 80
  36. disconnected = False
  37. def __init__(self, peer=None):
  38. if peer is None:
  39. peer = IPv4Address("TCP", "192.168.1.1", 12344)
  40. self._peer = peer
  41. self.written = BytesIO()
  42. self.producers = []
  43. def getPeer(self):
  44. return self._peer
  45. def write(self, data):
  46. if not isinstance(data, bytes):
  47. raise TypeError(f"Can only write bytes to a transport, not {data!r}")
  48. self.written.write(data)
  49. def writeSequence(self, iovec):
  50. for data in iovec:
  51. self.write(data)
  52. def getHost(self):
  53. return IPv4Address("TCP", "10.0.0.1", self.port)
  54. def registerProducer(self, producer, streaming):
  55. self.producers.append((producer, streaming))
  56. def unregisterProducer(self):
  57. pass
  58. def loseConnection(self):
  59. self.disconnected = True
  60. @implementer(ISSLTransport)
  61. class SSL(TCP):
  62. def abortConnection(self):
  63. # ITCPTransport.abortConnection
  64. pass
  65. def getTcpKeepAlive(self):
  66. # ITCPTransport.getTcpKeepAlive
  67. pass
  68. def getTcpNoDelay(self):
  69. # ITCPTransport.getTcpNoDelay
  70. pass
  71. def loseWriteConnection(self):
  72. # ITCPTransport.loseWriteConnection
  73. pass
  74. def setTcpKeepAlive(self, enabled):
  75. # ITCPTransport.setTcpKeepAlive
  76. pass
  77. def setTcpNoDelay(self, enabled):
  78. # ITCPTransport.setTcpNoDelay
  79. pass
  80. def getPeerCertificate(self):
  81. # ISSLTransport.getPeerCertificate
  82. pass
  83. site = Site(Resource())
  84. def __init__(self, peer=None):
  85. self.transport = self.TCP(peer)
  86. def requestDone(self, request):
  87. pass
  88. def writeHeaders(self, version, code, reason, headers):
  89. response_line = version + b" " + code + b" " + reason + b"\r\n"
  90. headerSequence = [response_line]
  91. headerSequence.extend(name + b": " + value + b"\r\n" for name, value in headers)
  92. headerSequence.append(b"\r\n")
  93. self.transport.writeSequence(headerSequence)
  94. def getPeer(self):
  95. return self.transport.getPeer()
  96. def getHost(self):
  97. return self.transport.getHost()
  98. def registerProducer(self, producer, streaming):
  99. self.transport.registerProducer(producer, streaming)
  100. def unregisterProducer(self):
  101. self.transport.unregisterProducer()
  102. def write(self, data):
  103. self.transport.write(data)
  104. def writeSequence(self, iovec):
  105. self.transport.writeSequence(iovec)
  106. def loseConnection(self):
  107. self.transport.loseConnection()
  108. def endRequest(self):
  109. pass
  110. def isSecure(self):
  111. return isinstance(self.transport, self.SSL)
  112. def abortConnection(self):
  113. # ITCPTransport.abortConnection
  114. pass
  115. def getTcpKeepAlive(self):
  116. # ITCPTransport.getTcpKeepAlive
  117. pass
  118. def getTcpNoDelay(self):
  119. # ITCPTransport.getTcpNoDelay
  120. pass
  121. def loseWriteConnection(self):
  122. # ITCPTransport.loseWriteConnection
  123. pass
  124. def setTcpKeepAlive(self):
  125. # ITCPTransport.setTcpKeepAlive
  126. pass
  127. def setTcpNoDelay(self):
  128. # ITCPTransport.setTcpNoDelay
  129. pass
  130. def getPeerCertificate(self):
  131. # ISSLTransport.getPeerCertificate
  132. pass
  133. class DummyRequest:
  134. """
  135. Represents a dummy or fake request. See L{twisted.web.server.Request}.
  136. @ivar _finishedDeferreds: L{None} or a C{list} of L{Deferreds} which will
  137. be called back with L{None} when C{finish} is called or which will be
  138. errbacked if C{processingFailed} is called.
  139. @type requestheaders: C{Headers}
  140. @ivar requestheaders: A Headers instance that stores values for all request
  141. headers.
  142. @type responseHeaders: C{Headers}
  143. @ivar responseHeaders: A Headers instance that stores values for all
  144. response headers.
  145. @type responseCode: C{int}
  146. @ivar responseCode: The response code which was passed to
  147. C{setResponseCode}.
  148. @type written: C{list} of C{bytes}
  149. @ivar written: The bytes which have been written to the request.
  150. """
  151. uri = b"http://dummy/"
  152. method = b"GET"
  153. client: Optional[IAddress] = None
  154. sitepath: List[bytes]
  155. written: List[bytes]
  156. prepath: List[bytes]
  157. args: Dict[bytes, List[bytes]]
  158. _finishedDeferreds: List[Deferred[None]]
  159. def registerProducer(self, prod, s):
  160. """
  161. Call an L{IPullProducer}'s C{resumeProducing} method in a
  162. loop until it unregisters itself.
  163. @param prod: The producer.
  164. @type prod: L{IPullProducer}
  165. @param s: Whether or not the producer is streaming.
  166. """
  167. # XXX: Handle IPushProducers
  168. self.go = 1
  169. while self.go:
  170. prod.resumeProducing()
  171. def unregisterProducer(self):
  172. self.go = 0
  173. def __init__(
  174. self,
  175. postpath: list[bytes],
  176. session: Optional[Session] = None,
  177. client: Optional[IAddress] = None,
  178. ) -> None:
  179. self.sitepath = []
  180. self.written = []
  181. self.finished = 0
  182. self.postpath = postpath
  183. self.prepath = []
  184. self.session = None
  185. self.protoSession = session or Session(site=None, uid=b"0", reactor=Clock())
  186. self.args = {}
  187. self.requestHeaders = Headers()
  188. self.responseHeaders = Headers()
  189. self.responseCode = None
  190. self._finishedDeferreds = []
  191. self._serverName = b"dummy"
  192. self.clientproto = b"HTTP/1.0"
  193. def getAllHeaders(self):
  194. """
  195. Return dictionary mapping the names of all received headers to the last
  196. value received for each.
  197. Since this method does not return all header information,
  198. C{self.requestHeaders.getAllRawHeaders()} may be preferred.
  199. NOTE: This function is a direct copy of
  200. C{twisted.web.http.Request.getAllRawHeaders}.
  201. """
  202. headers = {}
  203. for k, v in self.requestHeaders.getAllRawHeaders():
  204. headers[k.lower()] = v[-1]
  205. return headers
  206. def getHeader(self, name):
  207. """
  208. Retrieve the value of a request header.
  209. @type name: C{bytes}
  210. @param name: The name of the request header for which to retrieve the
  211. value. Header names are compared case-insensitively.
  212. @rtype: C{bytes} or L{None}
  213. @return: The value of the specified request header.
  214. """
  215. return self.requestHeaders.getRawHeaders(name.lower(), [None])[0]
  216. def setHeader(self, name, value):
  217. """TODO: make this assert on write() if the header is content-length"""
  218. self.responseHeaders.addRawHeader(name, value)
  219. def getSession(self, sessionInterface=None):
  220. if self.session:
  221. return self.session
  222. assert (
  223. not self.written
  224. ), "Session cannot be requested after data has been written."
  225. self.session = self.protoSession
  226. return self.session
  227. def render(self, resource):
  228. """
  229. Render the given resource as a response to this request.
  230. This implementation only handles a few of the most common behaviors of
  231. resources. It can handle a render method that returns a string or
  232. C{NOT_DONE_YET}. It doesn't know anything about the semantics of
  233. request methods (eg HEAD) nor how to set any particular headers.
  234. Basically, it's largely broken, but sufficient for some tests at least.
  235. It should B{not} be expanded to do all the same stuff L{Request} does.
  236. Instead, L{DummyRequest} should be phased out and L{Request} (or some
  237. other real code factored in a different way) used.
  238. """
  239. result = resource.render(self)
  240. if result is NOT_DONE_YET:
  241. return
  242. self.write(result)
  243. self.finish()
  244. def write(self, data):
  245. if not isinstance(data, bytes):
  246. raise TypeError("write() only accepts bytes")
  247. self.written.append(data)
  248. def notifyFinish(self) -> Deferred[None]:
  249. """
  250. Return a L{Deferred} which is called back with L{None} when the request
  251. is finished. This will probably only work if you haven't called
  252. C{finish} yet.
  253. """
  254. finished: Deferred[None] = Deferred()
  255. self._finishedDeferreds.append(finished)
  256. return finished
  257. def finish(self):
  258. """
  259. Record that the request is finished and callback and L{Deferred}s
  260. waiting for notification of this.
  261. """
  262. self.finished = self.finished + 1
  263. if self._finishedDeferreds is not None:
  264. observers = self._finishedDeferreds
  265. self._finishedDeferreds = None
  266. for obs in observers:
  267. obs.callback(None)
  268. def processingFailed(self, reason):
  269. """
  270. Errback and L{Deferreds} waiting for finish notification.
  271. """
  272. if self._finishedDeferreds is not None:
  273. observers = self._finishedDeferreds
  274. self._finishedDeferreds = None
  275. for obs in observers:
  276. obs.errback(reason)
  277. def addArg(self, name, value):
  278. self.args[name] = [value]
  279. def setResponseCode(self, code, message=None):
  280. """
  281. Set the HTTP status response code, but takes care that this is called
  282. before any data is written.
  283. """
  284. assert (
  285. not self.written
  286. ), "Response code cannot be set after data has" "been written: {}.".format(
  287. "@@@@".join(self.written)
  288. )
  289. self.responseCode = code
  290. self.responseMessage = message
  291. def setLastModified(self, when):
  292. assert (
  293. not self.written
  294. ), "Last-Modified cannot be set after data has " "been written: {}.".format(
  295. "@@@@".join(self.written)
  296. )
  297. def setETag(self, tag):
  298. assert (
  299. not self.written
  300. ), "ETag cannot be set after data has been " "written: {}.".format(
  301. "@@@@".join(self.written)
  302. )
  303. @deprecated(Version("Twisted", 18, 4, 0), replacement="getClientAddress")
  304. def getClientIP(self):
  305. """
  306. Return the IPv4 address of the client which made this request, if there
  307. is one, otherwise L{None}.
  308. """
  309. if isinstance(self.client, (IPv4Address, IPv6Address)):
  310. return self.client.host
  311. return None
  312. def getClientAddress(self):
  313. """
  314. Return the L{IAddress} of the client that made this request.
  315. @return: an address.
  316. @rtype: an L{IAddress} provider.
  317. """
  318. if self.client is None:
  319. return NullAddress()
  320. return self.client
  321. def getRequestHostname(self):
  322. """
  323. Get a dummy hostname associated to the HTTP request.
  324. @rtype: C{bytes}
  325. @returns: a dummy hostname
  326. """
  327. return self._serverName
  328. def getHost(self):
  329. """
  330. Get a dummy transport's host.
  331. @rtype: C{IPv4Address}
  332. @returns: a dummy transport's host
  333. """
  334. return IPv4Address("TCP", "127.0.0.1", 80)
  335. def setHost(self, host, port, ssl=0):
  336. """
  337. Change the host and port the request thinks it's using.
  338. @type host: C{bytes}
  339. @param host: The value to which to change the host header.
  340. @type ssl: C{bool}
  341. @param ssl: A flag which, if C{True}, indicates that the request is
  342. considered secure (if C{True}, L{isSecure} will return C{True}).
  343. """
  344. self._forceSSL = ssl # set first so isSecure will work
  345. if self.isSecure():
  346. default = 443
  347. else:
  348. default = 80
  349. if port == default:
  350. hostHeader = host
  351. else:
  352. hostHeader = b"%b:%d" % (host, port)
  353. self.requestHeaders.addRawHeader(b"host", hostHeader)
  354. def redirect(self, url):
  355. """
  356. Utility function that does a redirect.
  357. The request should have finish() called after this.
  358. """
  359. self.setResponseCode(FOUND)
  360. self.setHeader(b"location", url)
  361. class DummyRequestTests(unittest.SynchronousTestCase):
  362. """
  363. Tests for L{DummyRequest}.
  364. """
  365. def test_getClientIPDeprecated(self):
  366. """
  367. L{DummyRequest.getClientIP} is deprecated in favor of
  368. L{DummyRequest.getClientAddress}
  369. """
  370. request = DummyRequest([])
  371. request.getClientIP()
  372. warnings = self.flushWarnings(
  373. offendingFunctions=[self.test_getClientIPDeprecated]
  374. )
  375. self.assertEqual(1, len(warnings))
  376. [warning] = warnings
  377. self.assertEqual(warning.get("category"), DeprecationWarning)
  378. self.assertEqual(
  379. warning.get("message"),
  380. (
  381. "twisted.web.test.requesthelper.DummyRequest.getClientIP "
  382. "was deprecated in Twisted 18.4.0; "
  383. "please use getClientAddress instead"
  384. ),
  385. )
  386. def test_getClientIPSupportsIPv6(self):
  387. """
  388. L{DummyRequest.getClientIP} supports IPv6 addresses, just like
  389. L{twisted.web.http.Request.getClientIP}.
  390. """
  391. request = DummyRequest([])
  392. client = IPv6Address("TCP", "::1", 12345)
  393. request.client = client
  394. self.assertEqual("::1", request.getClientIP())
  395. def test_getClientAddressWithoutClient(self):
  396. """
  397. L{DummyRequest.getClientAddress} returns an L{IAddress}
  398. provider no C{client} has been set.
  399. """
  400. request = DummyRequest([])
  401. null = request.getClientAddress()
  402. verify.verifyObject(IAddress, null)
  403. def test_getClientAddress(self):
  404. """
  405. L{DummyRequest.getClientAddress} returns the C{client}.
  406. """
  407. request = DummyRequest([])
  408. client = IPv4Address("TCP", "127.0.0.1", 12345)
  409. request.client = client
  410. address = request.getClientAddress()
  411. self.assertIs(address, client)