-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Request & follow redirects for /media/v3/download #16701
Changes from 2 commits
c2c5942
fa4f637
8837c38
12c6193
b209ee7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,10 +27,11 @@ | |
|
||
from twisted.internet import defer | ||
from twisted.internet.defer import Deferred | ||
from twisted.python.failure import Failure | ||
from twisted.test.proto_helpers import MemoryReactor | ||
from twisted.web.resource import Resource | ||
|
||
from synapse.api.errors import Codes | ||
from synapse.api.errors import Codes, HttpResponseException | ||
from synapse.events import EventBase | ||
from synapse.http.types import QueryParams | ||
from synapse.logging.context import make_deferred_yieldable | ||
|
@@ -257,10 +258,15 @@ def write_to( | |
output_stream.write(data) | ||
return response | ||
|
||
def write_err(f: Failure) -> Failure: | ||
f.trap(HttpResponseException) | ||
output_stream.write(f.value.response) | ||
return f | ||
Comment on lines
+262
to
+265
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one was new to me: https://docs.twisted.org/en/stable/api/twisted.python.failure.Failure.html#trap TL;DR the trap call is a no-op if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd missed that this was in test code though. Why do we need to add this as an errback all of a sudden? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to add it because we know call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahhh, I think I see: we never called errback on the mock until now! |
||
|
||
d: Deferred[Tuple[bytes, Tuple[int, Dict[bytes, List[bytes]]]]] = Deferred() | ||
self.fetches.append((d, destination, path, args)) | ||
# Note that this callback changes the value held by d. | ||
d_after_callback = d.addCallback(write_to) | ||
d_after_callback = d.addCallbacks(write_to, write_err) | ||
return make_deferred_yieldable(d_after_callback) | ||
|
||
# Mock out the homeserver's MatrixFederationHttpClient | ||
|
@@ -316,7 +322,7 @@ def _req( | |
self.assertEqual(len(self.fetches), 1) | ||
self.assertEqual(self.fetches[0][1], "example.com") | ||
self.assertEqual( | ||
self.fetches[0][2], "/_matrix/media/r0/download/" + self.media_id | ||
self.fetches[0][2], "/_matrix/media/v3/download/" + self.media_id | ||
) | ||
self.assertEqual( | ||
self.fetches[0][3], {"allow_remote": "false", "timeout_ms": "20000"} | ||
|
@@ -671,6 +677,52 @@ def test_cross_origin_resource_policy_header(self) -> None: | |
[b"cross-origin"], | ||
) | ||
|
||
def test_unknown_v3_endpoint(self) -> None: | ||
""" | ||
If the v3 endpoint fails, try the r0 one. | ||
""" | ||
channel = self.make_request( | ||
"GET", | ||
f"/_matrix/media/v3/download/{self.media_id}", | ||
shorthand=False, | ||
await_result=False, | ||
) | ||
self.pump() | ||
|
||
# We've made one fetch, to example.com, using the media URL, and asking | ||
# the other server not to do a remote fetch | ||
self.assertEqual(len(self.fetches), 1) | ||
self.assertEqual(self.fetches[0][1], "example.com") | ||
self.assertEqual( | ||
self.fetches[0][2], "/_matrix/media/v3/download/" + self.media_id | ||
) | ||
|
||
# The result which says the endpoint is unknown. | ||
unknown_endpoint = b'{"errcode":"M_UNRECOGNIZED","error":"Unknown request"}' | ||
self.fetches[0][0].errback( | ||
HttpResponseException(404, "NOT FOUND", unknown_endpoint) | ||
) | ||
|
||
self.pump() | ||
|
||
# There should now be another request to the r0 URL. | ||
self.assertEqual(len(self.fetches), 2) | ||
self.assertEqual(self.fetches[1][1], "example.com") | ||
self.assertEqual( | ||
self.fetches[1][2], f"/_matrix/media/r0/download/{self.media_id}" | ||
) | ||
|
||
headers = { | ||
b"Content-Length": [b"%d" % (len(self.test_image.data))], | ||
} | ||
|
||
self.fetches[1][0].callback( | ||
(self.test_image.data, (len(self.test_image.data), headers)) | ||
) | ||
|
||
self.pump() | ||
self.assertEqual(channel.code, 200) | ||
|
||
|
||
class TestSpamCheckerLegacy: | ||
"""A spam checker module that rejects all media that includes the bytes | ||
|
Uh oh!
There was an error while loading. Please reload this page.