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