Make `make_request` actually render the request

remove the stubbing out of `request.process`, so that `requestReceived` also renders the request via the appropriate resource.

Replace render() with a stub for now.
pull/8761/head
Richard van der Hoff 2020-11-15 22:47:54 +00:00
parent 1f41422c98
commit 129ae841e5
9 changed files with 57 additions and 68 deletions

View File

@ -68,15 +68,15 @@ class MediaRepoShardTestCase(BaseMultiWorkerStreamTestCase):
the media which the caller should respond to. the media which the caller should respond to.
""" """
resource = hs.get_media_repository_resource().children[b"download"] resource = hs.get_media_repository_resource().children[b"download"]
request, channel = make_request( _, channel = make_request(
self.reactor, self.reactor,
FakeSite(resource), FakeSite(resource),
"GET", "GET",
"/{}/{}".format(target, media_id), "/{}/{}".format(target, media_id),
shorthand=False, shorthand=False,
access_token=self.access_token, access_token=self.access_token,
await_result=False,
) )
request.render(resource)
self.pump() self.pump()
clients = self.reactor.tcpClients clients = self.reactor.tcpClients

View File

@ -231,8 +231,6 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
shorthand=False, shorthand=False,
access_token=admin_user_tok, access_token=admin_user_tok,
) )
request.render(self.download_resource)
self.pump(1.0)
# Should be quarantined # Should be quarantined
self.assertEqual( self.assertEqual(
@ -301,8 +299,6 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
shorthand=False, shorthand=False,
access_token=non_admin_user_tok, access_token=non_admin_user_tok,
) )
request.render(self.download_resource)
self.pump(1.0)
# Should be successful # Should be successful
self.assertEqual(200, int(channel.code), msg=channel.result["body"]) self.assertEqual(200, int(channel.code), msg=channel.result["body"])
@ -478,8 +474,6 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
shorthand=False, shorthand=False,
access_token=non_admin_user_tok, access_token=non_admin_user_tok,
) )
request.render(self.download_resource)
self.pump(1.0)
# Shouldn't be quarantined # Shouldn't be quarantined
self.assertEqual( self.assertEqual(

View File

@ -133,8 +133,6 @@ class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
shorthand=False, shorthand=False,
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
request.render(download_resource)
self.pump(1.0)
# Should be successful # Should be successful
self.assertEqual( self.assertEqual(
@ -172,8 +170,6 @@ class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
shorthand=False, shorthand=False,
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
request.render(download_resource)
self.pump(1.0)
self.assertEqual( self.assertEqual(
404, 404,
channel.code, channel.code,
@ -548,8 +544,6 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
shorthand=False, shorthand=False,
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
request.render(download_resource)
self.pump(1.0)
if expect_success: if expect_success:
self.assertEqual( self.assertEqual(

View File

@ -310,7 +310,7 @@ class RestHelper:
""" """
image_length = len(image_data) image_length = len(image_data)
path = "/_matrix/media/r0/upload?filename=%s" % (filename,) path = "/_matrix/media/r0/upload?filename=%s" % (filename,)
request, channel = make_request( _, channel = make_request(
self.hs.get_reactor(), self.hs.get_reactor(),
FakeSite(resource), FakeSite(resource),
"POST", "POST",
@ -319,8 +319,6 @@ class RestHelper:
access_token=tok, access_token=tok,
custom_headers=[(b"Content-Length", str(image_length))], custom_headers=[(b"Content-Length", str(image_length))],
) )
request.render(resource)
self.hs.get_reactor().pump([100])
assert channel.code == expect_code, "Expected: %d, got: %d, resp: %r" % ( assert channel.code == expect_code, "Expected: %d, got: %d, resp: %r" % (
expect_code, expect_code,

View File

@ -263,8 +263,6 @@ class PasswordResetTestCase(unittest.HomeserverTestCase):
path, path,
shorthand=False, shorthand=False,
) )
request.render(self.submit_token_resource)
self.pump()
self.assertEquals(200, channel.code, channel.result) self.assertEquals(200, channel.code, channel.result)
@ -288,8 +286,6 @@ class PasswordResetTestCase(unittest.HomeserverTestCase):
shorthand=False, shorthand=False,
content_is_form=True, content_is_form=True,
) )
request.render(self.submit_token_resource)
self.pump()
self.assertEquals(200, channel.code, channel.result) self.assertEquals(200, channel.code, channel.result)
def _get_link_from_email(self): def _get_link_from_email(self):

View File

@ -234,8 +234,8 @@ class MediaRepoTests(unittest.HomeserverTestCase):
"GET", "GET",
self.media_id, self.media_id,
shorthand=False, shorthand=False,
await_result=False,
) )
request.render(self.download_resource)
self.pump() self.pump()
# We've made one fetch, to example.com, using the media URL, and asking # We've made one fetch, to example.com, using the media URL, and asking
@ -330,8 +330,8 @@ class MediaRepoTests(unittest.HomeserverTestCase):
"GET", "GET",
self.media_id + params, self.media_id + params,
shorthand=False, shorthand=False,
await_result=False,
) )
request.render(self.thumbnail_resource)
self.pump() self.pump()
headers = { headers = {
@ -359,7 +359,6 @@ class MediaRepoTests(unittest.HomeserverTestCase):
channel.json_body, channel.json_body,
{ {
"errcode": "M_NOT_FOUND", "errcode": "M_NOT_FOUND",
"error": "Not found [b'example.com', b'12345?width=32&height=32&method=%s']" "error": "Not found [b'example.com', b'12345']",
% method,
}, },
) )

View File

@ -140,9 +140,11 @@ class URLPreviewTests(unittest.HomeserverTestCase):
self.lookups["matrix.org"] = [(IPv4Address, "10.1.2.3")] self.lookups["matrix.org"] = [(IPv4Address, "10.1.2.3")]
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://matrix.org", shorthand=False, "GET",
"preview_url?url=http://matrix.org",
shorthand=False,
await_result=False,
) )
request.render(self.preview_url)
self.pump() self.pump()
client = self.reactor.tcpClients[0][2].buildProtocol(None) client = self.reactor.tcpClients[0][2].buildProtocol(None)
@ -165,8 +167,6 @@ class URLPreviewTests(unittest.HomeserverTestCase):
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://matrix.org", shorthand=False "GET", "preview_url?url=http://matrix.org", shorthand=False
) )
request.render(self.preview_url)
self.pump()
# Check the cache response has the same content # Check the cache response has the same content
self.assertEqual(channel.code, 200) self.assertEqual(channel.code, 200)
@ -183,8 +183,6 @@ class URLPreviewTests(unittest.HomeserverTestCase):
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://matrix.org", shorthand=False "GET", "preview_url?url=http://matrix.org", shorthand=False
) )
request.render(self.preview_url)
self.pump()
# Check the cache response has the same content # Check the cache response has the same content
self.assertEqual(channel.code, 200) self.assertEqual(channel.code, 200)
@ -204,9 +202,11 @@ class URLPreviewTests(unittest.HomeserverTestCase):
) )
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://matrix.org", shorthand=False, "GET",
"preview_url?url=http://matrix.org",
shorthand=False,
await_result=False,
) )
request.render(self.preview_url)
self.pump() self.pump()
client = self.reactor.tcpClients[0][2].buildProtocol(None) client = self.reactor.tcpClients[0][2].buildProtocol(None)
@ -237,9 +237,11 @@ class URLPreviewTests(unittest.HomeserverTestCase):
) )
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://matrix.org", shorthand=False "GET",
"preview_url?url=http://matrix.org",
shorthand=False,
await_result=False,
) )
request.render(self.preview_url)
self.pump() self.pump()
client = self.reactor.tcpClients[0][2].buildProtocol(None) client = self.reactor.tcpClients[0][2].buildProtocol(None)
@ -270,9 +272,11 @@ class URLPreviewTests(unittest.HomeserverTestCase):
) )
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://matrix.org", shorthand=False "GET",
"preview_url?url=http://matrix.org",
shorthand=False,
await_result=False,
) )
request.render(self.preview_url)
self.pump() self.pump()
client = self.reactor.tcpClients[0][2].buildProtocol(None) client = self.reactor.tcpClients[0][2].buildProtocol(None)
@ -301,9 +305,11 @@ class URLPreviewTests(unittest.HomeserverTestCase):
self.lookups["example.com"] = [(IPv4Address, "10.1.2.3")] self.lookups["example.com"] = [(IPv4Address, "10.1.2.3")]
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://example.com", shorthand=False "GET",
"preview_url?url=http://example.com",
shorthand=False,
await_result=False,
) )
request.render(self.preview_url)
self.pump() self.pump()
client = self.reactor.tcpClients[0][2].buildProtocol(None) client = self.reactor.tcpClients[0][2].buildProtocol(None)
@ -331,8 +337,6 @@ class URLPreviewTests(unittest.HomeserverTestCase):
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://example.com", shorthand=False "GET", "preview_url?url=http://example.com", shorthand=False
) )
request.render(self.preview_url)
self.pump()
# No requests made. # No requests made.
self.assertEqual(len(self.reactor.tcpClients), 0) self.assertEqual(len(self.reactor.tcpClients), 0)
@ -354,8 +358,6 @@ class URLPreviewTests(unittest.HomeserverTestCase):
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://example.com", shorthand=False "GET", "preview_url?url=http://example.com", shorthand=False
) )
request.render(self.preview_url)
self.pump()
self.assertEqual(channel.code, 502) self.assertEqual(channel.code, 502)
self.assertEqual( self.assertEqual(
@ -373,8 +375,6 @@ class URLPreviewTests(unittest.HomeserverTestCase):
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://192.168.1.1", shorthand=False "GET", "preview_url?url=http://192.168.1.1", shorthand=False
) )
request.render(self.preview_url)
self.pump()
# No requests made. # No requests made.
self.assertEqual(len(self.reactor.tcpClients), 0) self.assertEqual(len(self.reactor.tcpClients), 0)
@ -394,8 +394,6 @@ class URLPreviewTests(unittest.HomeserverTestCase):
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://1.1.1.2", shorthand=False "GET", "preview_url?url=http://1.1.1.2", shorthand=False
) )
request.render(self.preview_url)
self.pump()
self.assertEqual(channel.code, 403) self.assertEqual(channel.code, 403)
self.assertEqual( self.assertEqual(
@ -414,9 +412,11 @@ class URLPreviewTests(unittest.HomeserverTestCase):
self.lookups["example.com"] = [(IPv4Address, "1.1.1.1")] self.lookups["example.com"] = [(IPv4Address, "1.1.1.1")]
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://example.com", shorthand=False "GET",
"preview_url?url=http://example.com",
shorthand=False,
await_result=False,
) )
request.render(self.preview_url)
self.pump() self.pump()
client = self.reactor.tcpClients[0][2].buildProtocol(None) client = self.reactor.tcpClients[0][2].buildProtocol(None)
@ -451,8 +451,6 @@ class URLPreviewTests(unittest.HomeserverTestCase):
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://example.com", shorthand=False "GET", "preview_url?url=http://example.com", shorthand=False
) )
request.render(self.preview_url)
self.pump()
self.assertEqual(channel.code, 502) self.assertEqual(channel.code, 502)
self.assertEqual( self.assertEqual(
channel.json_body, channel.json_body,
@ -473,8 +471,6 @@ class URLPreviewTests(unittest.HomeserverTestCase):
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://example.com", shorthand=False "GET", "preview_url?url=http://example.com", shorthand=False
) )
request.render(self.preview_url)
self.pump()
# No requests made. # No requests made.
self.assertEqual(len(self.reactor.tcpClients), 0) self.assertEqual(len(self.reactor.tcpClients), 0)
@ -496,8 +492,6 @@ class URLPreviewTests(unittest.HomeserverTestCase):
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://example.com", shorthand=False "GET", "preview_url?url=http://example.com", shorthand=False
) )
request.render(self.preview_url)
self.pump()
self.assertEqual(channel.code, 502) self.assertEqual(channel.code, 502)
self.assertEqual( self.assertEqual(
@ -515,8 +509,6 @@ class URLPreviewTests(unittest.HomeserverTestCase):
request, channel = self.make_request( request, channel = self.make_request(
"OPTIONS", "preview_url?url=http://example.com", shorthand=False "OPTIONS", "preview_url?url=http://example.com", shorthand=False
) )
request.render(self.preview_url)
self.pump()
self.assertEqual(channel.code, 200) self.assertEqual(channel.code, 200)
self.assertEqual(channel.json_body, {}) self.assertEqual(channel.json_body, {})
@ -528,9 +520,11 @@ class URLPreviewTests(unittest.HomeserverTestCase):
# Build and make a request to the server # Build and make a request to the server
request, channel = self.make_request( request, channel = self.make_request(
"GET", "preview_url?url=http://example.com", shorthand=False "GET",
"preview_url?url=http://example.com",
shorthand=False,
await_result=False,
) )
request.render(self.preview_url)
self.pump() self.pump()
# Extract Synapse's tcp client # Extract Synapse's tcp client
@ -603,8 +597,8 @@ class URLPreviewTests(unittest.HomeserverTestCase):
"GET", "GET",
"preview_url?url=http://twitter.com/matrixdotorg/status/12345", "preview_url?url=http://twitter.com/matrixdotorg/status/12345",
shorthand=False, shorthand=False,
await_result=False,
) )
request.render(self.preview_url)
self.pump() self.pump()
client = self.reactor.tcpClients[0][2].buildProtocol(None) client = self.reactor.tcpClients[0][2].buildProtocol(None)
@ -668,8 +662,8 @@ class URLPreviewTests(unittest.HomeserverTestCase):
"GET", "GET",
"preview_url?url=http://twitter.com/matrixdotorg/status/12345", "preview_url?url=http://twitter.com/matrixdotorg/status/12345",
shorthand=False, shorthand=False,
await_result=False,
) )
request.render(self.preview_url)
self.pump() self.pump()
client = self.reactor.tcpClients[0][2].buildProtocol(None) client = self.reactor.tcpClients[0][2].buildProtocol(None)

View File

@ -171,16 +171,18 @@ def make_request(
shorthand=True, shorthand=True,
federation_auth_origin=None, federation_auth_origin=None,
content_is_form=False, content_is_form=False,
await_result: bool = True,
custom_headers: Optional[ custom_headers: Optional[
Iterable[Tuple[Union[bytes, str], Union[bytes, str]]] Iterable[Tuple[Union[bytes, str], Union[bytes, str]]]
] = None, ] = None,
): ):
""" """
Make a web request using the given method and path, feed it the Make a web request using the given method, path and content, and render it
content, and return the Request and the Channel underneath.
Returns the Request and the Channel underneath.
Args: Args:
site: The twisted Site to associate with the Channel site: The twisted Site to use to render the request
method (bytes/unicode): The HTTP request method ("verb"). method (bytes/unicode): The HTTP request method ("verb").
path (bytes/unicode): The HTTP path, suitably URL encoded (e.g. path (bytes/unicode): The HTTP path, suitably URL encoded (e.g.
@ -196,6 +198,10 @@ def make_request(
custom_headers: (name, value) pairs to add as request headers custom_headers: (name, value) pairs to add as request headers
await_result: whether to wait for the request to complete rendering. If true,
will pump the reactor until the the renderer tells the channel the request
is finished.
Returns: Returns:
Tuple[synapse.http.site.SynapseRequest, channel] Tuple[synapse.http.site.SynapseRequest, channel]
""" """
@ -225,11 +231,9 @@ def make_request(
channel = FakeChannel(site, reactor) channel = FakeChannel(site, reactor)
req = request(channel) req = request(channel)
req.process = lambda: b""
req.content = BytesIO(content) req.content = BytesIO(content)
# Twisted expects to be at the end of the content when parsing the request. # Twisted expects to be at the end of the content when parsing the request.
req.content.seek(SEEK_END) req.content.seek(SEEK_END)
req.postpath = list(map(unquote, path[1:].split(b"/")))
if access_token: if access_token:
req.requestHeaders.addRawHeader( req.requestHeaders.addRawHeader(
@ -257,12 +261,14 @@ def make_request(
req.requestReceived(method, path, b"1.1") req.requestReceived(method, path, b"1.1")
if await_result:
channel.await_result()
return req, channel return req, channel
def render(request, resource, clock): def render(request, resource, clock):
request.render(resource) pass
request._channel.await_result()
@implementer(IReactorPluggableNameResolver) @implementer(IReactorPluggableNameResolver)

View File

@ -377,6 +377,7 @@ class HomeserverTestCase(TestCase):
shorthand: bool = True, shorthand: bool = True,
federation_auth_origin: str = None, federation_auth_origin: str = None,
content_is_form: bool = False, content_is_form: bool = False,
await_result: bool = True,
) -> Tuple[SynapseRequest, FakeChannel]: ) -> Tuple[SynapseRequest, FakeChannel]:
... ...
@ -391,6 +392,7 @@ class HomeserverTestCase(TestCase):
shorthand: bool = True, shorthand: bool = True,
federation_auth_origin: str = None, federation_auth_origin: str = None,
content_is_form: bool = False, content_is_form: bool = False,
await_result: bool = True,
) -> Tuple[T, FakeChannel]: ) -> Tuple[T, FakeChannel]:
... ...
@ -404,6 +406,7 @@ class HomeserverTestCase(TestCase):
shorthand: bool = True, shorthand: bool = True,
federation_auth_origin: str = None, federation_auth_origin: str = None,
content_is_form: bool = False, content_is_form: bool = False,
await_result: bool = True,
) -> Tuple[T, FakeChannel]: ) -> Tuple[T, FakeChannel]:
""" """
Create a SynapseRequest at the path using the method and containing the Create a SynapseRequest at the path using the method and containing the
@ -422,6 +425,10 @@ class HomeserverTestCase(TestCase):
content_is_form: Whether the content is URL encoded form data. Adds the content_is_form: Whether the content is URL encoded form data. Adds the
'Content-Type': 'application/x-www-form-urlencoded' header. 'Content-Type': 'application/x-www-form-urlencoded' header.
await_result: whether to wait for the request to complete rendering. If
true (the default), will pump the test reactor until the the renderer
tells the channel the request is finished.
Returns: Returns:
Tuple[synapse.http.site.SynapseRequest, channel] Tuple[synapse.http.site.SynapseRequest, channel]
""" """
@ -436,6 +443,7 @@ class HomeserverTestCase(TestCase):
shorthand, shorthand,
federation_auth_origin, federation_auth_origin,
content_is_form, content_is_form,
await_result,
) )
def render(self, request): def render(self, request):