test_additional_resource.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.http.additional_resource import AdditionalResource
  16. from synapse.http.server import respond_with_json
  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. self.resource = AdditionalResource(self.hs, handler)
  35. request, channel = self.make_request("GET", "/")
  36. self.render(request)
  37. self.assertEqual(request.code, 200)
  38. self.assertEqual(channel.json_body, {"some_key": "some_value_async"})
  39. def test_sync(self):
  40. handler = _SyncTestCustomEndpoint({}, None).handle_request
  41. self.resource = AdditionalResource(self.hs, handler)
  42. request, channel = self.make_request("GET", "/")
  43. self.render(request)
  44. self.assertEqual(request.code, 200)
  45. self.assertEqual(channel.json_body, {"some_key": "some_value_sync"})