Add create_resource_dict method to HomeserverTestCase

Rather than using a single JsonResource, construct a resource tree, as we do in
the prod code, and allow testcases to add extra resources by overriding
`create_resource_dict`.
This commit is contained in:
Richard van der Hoff 2020-12-02 15:21:00 +00:00
parent 8388384a64
commit 693516e756

View File

@ -20,7 +20,7 @@ import hmac
import inspect import inspect
import logging import logging
import time import time
from typing import Optional, Tuple, Type, TypeVar, Union, overload from typing import Dict, Optional, Tuple, Type, TypeVar, Union, overload
from mock import Mock, patch from mock import Mock, patch
@ -46,6 +46,7 @@ from synapse.logging.context import (
) )
from synapse.server import HomeServer from synapse.server import HomeServer
from synapse.types import UserID, create_requester from synapse.types import UserID, create_requester
from synapse.util.httpresourcetree import create_resource_tree
from synapse.util.ratelimitutils import FederationRateLimiter from synapse.util.ratelimitutils import FederationRateLimiter
from tests.server import FakeChannel, get_clock, make_request, setup_test_homeserver from tests.server import FakeChannel, get_clock, make_request, setup_test_homeserver
@ -320,15 +321,28 @@ class HomeserverTestCase(TestCase):
""" """
Create a the root resource for the test server. Create a the root resource for the test server.
The default implementation creates a JsonResource and calls each function in The default calls `self.create_resource_dict` and builds the resultant dict
`servlets` to register servletes against it into a tree.
""" """
resource = JsonResource(self.hs) root_resource = Resource()
create_resource_tree(self.create_resource_dict(), root_resource)
return root_resource
def create_resource_dict(self) -> Dict[str, Resource]:
"""Create a resource tree for the test server
A resource tree is a mapping from path to twisted.web.resource.
The default implementation creates a JsonResource and calls each function in
`servlets` to register servlets against it.
"""
servlet_resource = JsonResource(self.hs)
for servlet in self.servlets: for servlet in self.servlets:
servlet(self.hs, resource) servlet(self.hs, servlet_resource)
return {
return resource "/_matrix/client": servlet_resource,
"/_synapse/admin": servlet_resource,
}
def default_config(self): def default_config(self):
""" """