test_server.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import re
  2. from twisted.internet.defer import Deferred
  3. from twisted.test.proto_helpers import MemoryReactorClock
  4. from synapse.api.errors import Codes, SynapseError
  5. from synapse.http.server import JsonResource
  6. from synapse.util import Clock
  7. from tests import unittest
  8. from tests.server import make_request, render, setup_test_homeserver
  9. class JsonResourceTests(unittest.TestCase):
  10. def setUp(self):
  11. self.reactor = MemoryReactorClock()
  12. self.hs_clock = Clock(self.reactor)
  13. self.homeserver = setup_test_homeserver(
  14. self.addCleanup, http_client=None, clock=self.hs_clock, reactor=self.reactor
  15. )
  16. def test_handler_for_request(self):
  17. """
  18. JsonResource.handler_for_request gives correctly decoded URL args to
  19. the callback, while Twisted will give the raw bytes of URL query
  20. arguments.
  21. """
  22. got_kwargs = {}
  23. def _callback(request, **kwargs):
  24. got_kwargs.update(kwargs)
  25. return (200, kwargs)
  26. res = JsonResource(self.homeserver)
  27. res.register_paths(
  28. "GET", [re.compile("^/_matrix/foo/(?P<room_id>[^/]*)$")], _callback
  29. )
  30. request, channel = make_request(b"GET", b"/_matrix/foo/%E2%98%83?a=%E2%98%83")
  31. render(request, res, self.reactor)
  32. self.assertEqual(request.args, {b'a': [u"\N{SNOWMAN}".encode('utf8')]})
  33. self.assertEqual(got_kwargs, {u"room_id": u"\N{SNOWMAN}"})
  34. def test_callback_direct_exception(self):
  35. """
  36. If the web callback raises an uncaught exception, it will be translated
  37. into a 500.
  38. """
  39. def _callback(request, **kwargs):
  40. raise Exception("boo")
  41. res = JsonResource(self.homeserver)
  42. res.register_paths("GET", [re.compile("^/_matrix/foo$")], _callback)
  43. request, channel = make_request(b"GET", b"/_matrix/foo")
  44. render(request, res, self.reactor)
  45. self.assertEqual(channel.result["code"], b'500')
  46. def test_callback_indirect_exception(self):
  47. """
  48. If the web callback raises an uncaught exception in a Deferred, it will
  49. be translated into a 500.
  50. """
  51. def _throw(*args):
  52. raise Exception("boo")
  53. def _callback(request, **kwargs):
  54. d = Deferred()
  55. d.addCallback(_throw)
  56. self.reactor.callLater(1, d.callback, True)
  57. return d
  58. res = JsonResource(self.homeserver)
  59. res.register_paths("GET", [re.compile("^/_matrix/foo$")], _callback)
  60. request, channel = make_request(b"GET", b"/_matrix/foo")
  61. render(request, res, self.reactor)
  62. self.assertEqual(channel.result["code"], b'500')
  63. def test_callback_synapseerror(self):
  64. """
  65. If the web callback raises a SynapseError, it returns the appropriate
  66. status code and message set in it.
  67. """
  68. def _callback(request, **kwargs):
  69. raise SynapseError(403, "Forbidden!!one!", Codes.FORBIDDEN)
  70. res = JsonResource(self.homeserver)
  71. res.register_paths("GET", [re.compile("^/_matrix/foo$")], _callback)
  72. request, channel = make_request(b"GET", b"/_matrix/foo")
  73. render(request, res, self.reactor)
  74. self.assertEqual(channel.result["code"], b'403')
  75. self.assertEqual(channel.json_body["error"], "Forbidden!!one!")
  76. self.assertEqual(channel.json_body["errcode"], "M_FORBIDDEN")
  77. def test_no_handler(self):
  78. """
  79. If there is no handler to process the request, Synapse will return 400.
  80. """
  81. def _callback(request, **kwargs):
  82. """
  83. Not ever actually called!
  84. """
  85. self.fail("shouldn't ever get here")
  86. res = JsonResource(self.homeserver)
  87. res.register_paths("GET", [re.compile("^/_matrix/foo$")], _callback)
  88. request, channel = make_request(b"GET", b"/_matrix/foobar")
  89. render(request, res, self.reactor)
  90. self.assertEqual(channel.result["code"], b'400')
  91. self.assertEqual(channel.json_body["error"], "Unrecognized request")
  92. self.assertEqual(channel.json_body["errcode"], "M_UNRECOGNIZED")