test_additional_resource.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright 2018 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.http.additional_resource import AdditionalResource
  15. from synapse.http.server import respond_with_json
  16. from tests.server import FakeSite, make_request
  17. from tests.unittest import HomeserverTestCase
  18. class _AsyncTestCustomEndpoint:
  19. def __init__(self, config, module_api):
  20. pass
  21. async def handle_request(self, request):
  22. respond_with_json(request, 200, {"some_key": "some_value_async"})
  23. class _SyncTestCustomEndpoint:
  24. def __init__(self, config, module_api):
  25. pass
  26. async def handle_request(self, request):
  27. respond_with_json(request, 200, {"some_key": "some_value_sync"})
  28. class AdditionalResourceTests(HomeserverTestCase):
  29. """Very basic tests that `AdditionalResource` works correctly with sync
  30. and async handlers.
  31. """
  32. def test_async(self):
  33. handler = _AsyncTestCustomEndpoint({}, None).handle_request
  34. resource = AdditionalResource(self.hs, handler)
  35. channel = make_request(
  36. self.reactor, FakeSite(resource, self.reactor), "GET", "/"
  37. )
  38. self.assertEqual(channel.code, 200)
  39. self.assertEqual(channel.json_body, {"some_key": "some_value_async"})
  40. def test_sync(self):
  41. handler = _SyncTestCustomEndpoint({}, None).handle_request
  42. resource = AdditionalResource(self.hs, handler)
  43. channel = make_request(
  44. self.reactor, FakeSite(resource, self.reactor), "GET", "/"
  45. )
  46. self.assertEqual(channel.code, 200)
  47. self.assertEqual(channel.json_body, {"some_key": "some_value_sync"})