1
0

test_webclient.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright 2022 The Matrix.org Foundation C.I.C.
  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 http import HTTPStatus
  15. from typing import Dict
  16. from twisted.web.resource import Resource
  17. from synapse.app.homeserver import SynapseHomeServer
  18. from synapse.config.server import HttpListenerConfig, HttpResourceConfig, ListenerConfig
  19. from synapse.http.site import SynapseSite
  20. from tests.server import make_request
  21. from tests.unittest import HomeserverTestCase, create_resource_tree, override_config
  22. class WebClientTests(HomeserverTestCase):
  23. @override_config(
  24. {
  25. "web_client_location": "https://example.org",
  26. }
  27. )
  28. def test_webclient_resolves_with_client_resource(self):
  29. """
  30. Tests that both client and webclient resources can be accessed simultaneously.
  31. This is a regression test created in response to https://github.com/matrix-org/synapse/issues/11763.
  32. """
  33. for resource_name_order_list in [
  34. ["webclient", "client"],
  35. ["client", "webclient"],
  36. ]:
  37. # Create a dictionary from path regex -> resource
  38. resource_dict: Dict[str, Resource] = {}
  39. for resource_name in resource_name_order_list:
  40. resource_dict.update(
  41. SynapseHomeServer._configure_named_resource(self.hs, resource_name)
  42. )
  43. # Create a root resource which ties the above resources together into one
  44. root_resource = Resource()
  45. create_resource_tree(resource_dict, root_resource)
  46. # Create a site configured with this resource to make HTTP requests against
  47. listener_config = ListenerConfig(
  48. port=8008,
  49. bind_addresses=["127.0.0.1"],
  50. type="http",
  51. http_options=HttpListenerConfig(
  52. resources=[HttpResourceConfig(names=resource_name_order_list)]
  53. ),
  54. )
  55. test_site = SynapseSite(
  56. logger_name="synapse.access.http.fake",
  57. site_tag=self.hs.config.server.server_name,
  58. config=listener_config,
  59. resource=root_resource,
  60. server_version_string="1",
  61. max_request_body_size=1234,
  62. reactor=self.reactor,
  63. )
  64. # Attempt to make requests to endpoints on both the webclient and client resources
  65. # on test_site.
  66. self._request_client_and_webclient_resources(test_site)
  67. def _request_client_and_webclient_resources(self, test_site: SynapseSite) -> None:
  68. """Make a request to an endpoint on both the webclient and client-server resources
  69. of the given SynapseSite.
  70. Args:
  71. test_site: The SynapseSite object to make requests against.
  72. """
  73. # Ensure that the *webclient* resource is behaving as expected (we get redirected to
  74. # the configured web_client_location)
  75. channel = make_request(
  76. self.reactor,
  77. site=test_site,
  78. method="GET",
  79. path="/_matrix/client",
  80. )
  81. # Check that we are being redirected to the webclient location URI.
  82. self.assertEqual(channel.code, HTTPStatus.FOUND)
  83. self.assertEqual(
  84. channel.headers.getRawHeaders("Location"), ["https://example.org"]
  85. )
  86. # Ensure that a request to the *client* resource works.
  87. channel = make_request(
  88. self.reactor,
  89. site=test_site,
  90. method="GET",
  91. path="/_matrix/client/v3/login",
  92. )
  93. self.assertEqual(channel.code, HTTPStatus.OK)
  94. self.assertIn("flows", channel.json_body)