test_script.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.web.script}.
  5. """
  6. import os
  7. from twisted.trial.unittest import TestCase
  8. from twisted.python.filepath import FilePath
  9. from twisted.web.http import NOT_FOUND
  10. from twisted.web.script import ResourceScriptDirectory, PythonScript
  11. from twisted.web.test._util import _render
  12. from twisted.web.test.requesthelper import DummyRequest
  13. class ResourceScriptDirectoryTests(TestCase):
  14. """
  15. Tests for L{ResourceScriptDirectory}.
  16. """
  17. def test_renderNotFound(self):
  18. """
  19. L{ResourceScriptDirectory.render} sets the HTTP response code to I{NOT
  20. FOUND}.
  21. """
  22. resource = ResourceScriptDirectory(self.mktemp())
  23. request = DummyRequest([b''])
  24. d = _render(resource, request)
  25. def cbRendered(ignored):
  26. self.assertEqual(request.responseCode, NOT_FOUND)
  27. d.addCallback(cbRendered)
  28. return d
  29. def test_notFoundChild(self):
  30. """
  31. L{ResourceScriptDirectory.getChild} returns a resource which renders an
  32. response with the HTTP I{NOT FOUND} status code if the indicated child
  33. does not exist as an entry in the directory used to initialized the
  34. L{ResourceScriptDirectory}.
  35. """
  36. path = self.mktemp()
  37. os.makedirs(path)
  38. resource = ResourceScriptDirectory(path)
  39. request = DummyRequest([b'foo'])
  40. child = resource.getChild("foo", request)
  41. d = _render(child, request)
  42. def cbRendered(ignored):
  43. self.assertEqual(request.responseCode, NOT_FOUND)
  44. d.addCallback(cbRendered)
  45. return d
  46. def test_render(self):
  47. """
  48. L{ResourceScriptDirectory.getChild} returns a resource which renders a
  49. response with the HTTP 200 status code and the content of the rpy's
  50. C{request} global.
  51. """
  52. tmp = FilePath(self.mktemp())
  53. tmp.makedirs()
  54. tmp.child("test.rpy").setContent(b"""
  55. from twisted.web.resource import Resource
  56. class TestResource(Resource):
  57. isLeaf = True
  58. def render_GET(self, request):
  59. return b'ok'
  60. resource = TestResource()""")
  61. resource = ResourceScriptDirectory(tmp._asBytesPath())
  62. request = DummyRequest([b''])
  63. child = resource.getChild(b"test.rpy", request)
  64. d = _render(child, request)
  65. def cbRendered(ignored):
  66. self.assertEqual(b"".join(request.written), b"ok")
  67. d.addCallback(cbRendered)
  68. return d
  69. class PythonScriptTests(TestCase):
  70. """
  71. Tests for L{PythonScript}.
  72. """
  73. def test_notFoundRender(self):
  74. """
  75. If the source file a L{PythonScript} is initialized with doesn't exist,
  76. L{PythonScript.render} sets the HTTP response code to I{NOT FOUND}.
  77. """
  78. resource = PythonScript(self.mktemp(), None)
  79. request = DummyRequest([b''])
  80. d = _render(resource, request)
  81. def cbRendered(ignored):
  82. self.assertEqual(request.responseCode, NOT_FOUND)
  83. d.addCallback(cbRendered)
  84. return d
  85. def test_renderException(self):
  86. """
  87. L{ResourceScriptDirectory.getChild} returns a resource which renders a
  88. response with the HTTP 200 status code and the content of the rpy's
  89. C{request} global.
  90. """
  91. tmp = FilePath(self.mktemp())
  92. tmp.makedirs()
  93. child = tmp.child("test.epy")
  94. child.setContent(b'raise Exception("nooo")')
  95. resource = PythonScript(child._asBytesPath(), None)
  96. request = DummyRequest([b''])
  97. d = _render(resource, request)
  98. def cbRendered(ignored):
  99. self.assertIn(b"nooo", b"".join(request.written))
  100. d.addCallback(cbRendered)
  101. return d