test_additional_resource.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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.server import FakeSite, make_request
  18. from tests.unittest import HomeserverTestCase
  19. class _AsyncTestCustomEndpoint:
  20. def __init__(self, config, module_api):
  21. pass
  22. async def handle_request(self, request):
  23. respond_with_json(request, 200, {"some_key": "some_value_async"})
  24. class _SyncTestCustomEndpoint:
  25. def __init__(self, config, module_api):
  26. pass
  27. async def handle_request(self, request):
  28. respond_with_json(request, 200, {"some_key": "some_value_sync"})
  29. class AdditionalResourceTests(HomeserverTestCase):
  30. """Very basic tests that `AdditionalResource` works correctly with sync
  31. and async handlers.
  32. """
  33. def test_async(self):
  34. handler = _AsyncTestCustomEndpoint({}, None).handle_request
  35. resource = AdditionalResource(self.hs, handler)
  36. channel = make_request(self.reactor, FakeSite(resource), "GET", "/")
  37. self.assertEqual(channel.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. resource = AdditionalResource(self.hs, handler)
  42. channel = make_request(self.reactor, FakeSite(resource), "GET", "/")
  43. self.assertEqual(channel.code, 200)
  44. self.assertEqual(channel.json_body, {"some_key": "some_value_sync"})