test_server.py 4.3 KB

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