test_additional_resource.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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(self.reactor, FakeSite(resource), "GET", "/")
  36. self.assertEqual(channel.code, 200)
  37. self.assertEqual(channel.json_body, {"some_key": "some_value_async"})
  38. def test_sync(self):
  39. handler = _SyncTestCustomEndpoint({}, None).handle_request
  40. resource = AdditionalResource(self.hs, handler)
  41. channel = make_request(self.reactor, FakeSite(resource), "GET", "/")
  42. self.assertEqual(channel.code, 200)
  43. self.assertEqual(channel.json_body, {"some_key": "some_value_sync"})