Rename create_test_json_resource to create_test_resource (#8759)

The root resource isn't necessarily a JsonResource, so rename this method
accordingly, and update a couple of test classes to use the method rather than
directly manipulating self.resource.
This commit is contained in:
Richard van der Hoff 2020-11-16 14:45:52 +00:00 committed by GitHub
parent ebc405446e
commit 791d7cd6f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 16 additions and 23 deletions

1
changelog.d/8759.misc Normal file
View File

@ -0,0 +1 @@
Refactor test utilities for injecting HTTP requests.

View File

@ -240,8 +240,8 @@ class BaseMultiWorkerStreamTestCase(unittest.HomeserverTestCase):
lambda: self._handle_http_replication_attempt(self.hs, 8765), lambda: self._handle_http_replication_attempt(self.hs, 8765),
) )
def create_test_json_resource(self): def create_test_resource(self):
"""Overrides `HomeserverTestCase.create_test_json_resource`. """Overrides `HomeserverTestCase.create_test_resource`.
""" """
# We override this so that it automatically registers all the HTTP # We override this so that it automatically registers all the HTTP
# replication servlets, without having to explicitly do that in all # replication servlets, without having to explicitly do that in all

View File

@ -35,7 +35,7 @@ from tests import unittest
class VersionTestCase(unittest.HomeserverTestCase): class VersionTestCase(unittest.HomeserverTestCase):
url = "/_synapse/admin/v1/server_version" url = "/_synapse/admin/v1/server_version"
def create_test_json_resource(self): def create_test_resource(self):
resource = JsonResource(self.hs) resource = JsonResource(self.hs)
VersionServlet(self.hs).register(resource) VersionServlet(self.hs).register(resource)
return resource return resource

View File

@ -41,7 +41,7 @@ class BaseRemoteKeyResourceTestCase(unittest.HomeserverTestCase):
self.http_client = Mock() self.http_client = Mock()
return self.setup_test_homeserver(http_client=self.http_client) return self.setup_test_homeserver(http_client=self.http_client)
def create_test_json_resource(self): def create_test_resource(self):
return create_resource_tree( return create_resource_tree(
{"/_matrix/key/v2": KeyApiV2Resource(self.hs)}, root_resource=NoResource() {"/_matrix/key/v2": KeyApiV2Resource(self.hs)}, root_resource=NoResource()
) )

View File

@ -20,11 +20,9 @@ from tests import unittest
class HealthCheckTests(unittest.HomeserverTestCase): class HealthCheckTests(unittest.HomeserverTestCase):
def setUp(self): def create_test_resource(self):
super().setUp()
# replace the JsonResource with a HealthResource. # replace the JsonResource with a HealthResource.
self.resource = HealthResource() return HealthResource()
def test_health(self): def test_health(self):
request, channel = self.make_request("GET", "/health", shorthand=False) request, channel = self.make_request("GET", "/health", shorthand=False)

View File

@ -20,11 +20,9 @@ from tests import unittest
class WellKnownTests(unittest.HomeserverTestCase): class WellKnownTests(unittest.HomeserverTestCase):
def setUp(self): def create_test_resource(self):
super().setUp()
# replace the JsonResource with a WellKnownResource # replace the JsonResource with a WellKnownResource
self.resource = WellKnownResource(self.hs) return WellKnownResource(self.hs)
def test_well_known(self): def test_well_known(self):
self.hs.config.public_baseurl = "https://tesths" self.hs.config.public_baseurl = "https://tesths"

View File

@ -30,6 +30,7 @@ from twisted.internet.defer import Deferred, ensureDeferred, succeed
from twisted.python.failure import Failure from twisted.python.failure import Failure
from twisted.python.threadpool import ThreadPool from twisted.python.threadpool import ThreadPool
from twisted.trial import unittest from twisted.trial import unittest
from twisted.web.resource import Resource
from synapse.api.constants import EventTypes, Membership from synapse.api.constants import EventTypes, Membership
from synapse.config.homeserver import HomeServerConfig from synapse.config.homeserver import HomeServerConfig
@ -239,10 +240,8 @@ class HomeserverTestCase(TestCase):
if not isinstance(self.hs, HomeServer): if not isinstance(self.hs, HomeServer):
raise Exception("A homeserver wasn't returned, but %r" % (self.hs,)) raise Exception("A homeserver wasn't returned, but %r" % (self.hs,))
# Register the resources # create the root resource, and a site to wrap it.
self.resource = self.create_test_json_resource() self.resource = self.create_test_resource()
# create a site to wrap the resource.
self.site = SynapseSite( self.site = SynapseSite(
logger_name="synapse.access.http.fake", logger_name="synapse.access.http.fake",
site_tag=self.hs.config.server.server_name, site_tag=self.hs.config.server.server_name,
@ -323,15 +322,12 @@ class HomeserverTestCase(TestCase):
hs = self.setup_test_homeserver() hs = self.setup_test_homeserver()
return hs return hs
def create_test_json_resource(self): def create_test_resource(self) -> Resource:
""" """
Create a test JsonResource, with the relevant servlets registerd to it Create a the root resource for the test server.
The default implementation calls each function in `servlets` to do the The default implementation creates a JsonResource and calls each function in
registration. `servlets` to register servletes against it
Returns:
JsonResource:
""" """
resource = JsonResource(self.hs) resource = JsonResource(self.hs)