-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a basic test fetching thumbnail from _matrix/federation/v1/media/thumbnail
#732
Changes from 1 commit
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 |
---|---|---|
|
@@ -2,19 +2,23 @@ package tests | |
|
||
import ( | ||
"bytes" | ||
"context" | ||
"github.com/matrix-org/complement" | ||
"github.com/matrix-org/complement/client" | ||
"github.com/matrix-org/complement/federation" | ||
"github.com/matrix-org/complement/helpers" | ||
"github.com/matrix-org/complement/internal/data" | ||
"github.com/matrix-org/complement/runtime" | ||
"github.com/matrix-org/gomatrixserverlib/fclient" | ||
"github.com/matrix-org/gomatrixserverlib/spec" | ||
"image/jpeg" | ||
"image/png" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/matrix-org/complement" | ||
"github.com/matrix-org/complement/client" | ||
"github.com/matrix-org/complement/helpers" | ||
"github.com/matrix-org/complement/internal/data" | ||
) | ||
|
||
// TODO: add JPEG testing | ||
|
@@ -68,6 +72,60 @@ func TestRemotePngThumbnail(t *testing.T) { | |
} | ||
} | ||
|
||
func TestFederationThumbnail(t *testing.T) { | ||
runtime.SkipIf(t, runtime.Dendrite) | ||
|
||
deployment := complement.Deploy(t, 1) | ||
defer deployment.Destroy(t) | ||
|
||
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{}) | ||
|
||
srv := federation.NewServer(t, deployment, | ||
federation.HandleKeyRequests(), | ||
) | ||
cancel := srv.Listen() | ||
defer cancel() | ||
origin := spec.ServerName(srv.ServerName()) | ||
|
||
fileName := "test.png" | ||
contentType := "image/png" | ||
|
||
uri := alice.UploadContent(t, data.LargePng, fileName, contentType) | ||
_, mediaId := client.SplitMxc(uri) | ||
|
||
fedReq := fclient.NewFederationRequest( | ||
"GET", | ||
origin, | ||
"hs1", | ||
"/_matrix/federation/v1/media/thumbnail/"+mediaId+"?method=crop&width=14&height=14&animated=false", | ||
) | ||
|
||
resp, err := srv.DoFederationRequest(context.Background(), t, deployment, fedReq) | ||
if err != nil { | ||
t.Fatalf("thumbnail request for uploaded file failed: %s", err) | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
t.Fatalf("thumbnail request for uploaded file failed with status code: %v", resp.StatusCode) | ||
} | ||
|
||
resType := resp.Header.Get("Content-Type") | ||
mimeType := strings.Split(resType, ";")[0] | ||
|
||
if mimeType != "multipart/mixed" { | ||
t.Fatalf("thumbnail request did not return multipart/mixed response, returned %s instead", mimeType) | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
length := len(body) | ||
|
||
contentLength, err := strconv.Atoi(resp.Header.Get("Content-Length")) | ||
|
||
if contentLength != length { | ||
t.Fatalf("Content-Length and actual length are different: %d, %d", contentLength, length) | ||
} | ||
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. Can we add a check after the loop to make sure that an image was actually included in the response, mostly to make it clear that we are actually checking the image. Just something like 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. Right this is sorted - if you're happy with the change can you hit the merge button for me (as I no longer posses the power). Thanks so much! |
||
|
||
} | ||
|
||
func fetchAndValidateThumbnail(t *testing.T, c *client.CSAPI, mxcUri string, authenticated bool) { | ||
t.Helper() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we instead compare the received boddy to
test.png
? I don't think we necessarily should mandate that servers setContent-Length
in the black box test suite.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will that still fail if the
Content-Length
is set, but incorrect? That's what the synapse bug is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it probably makes sense to move the content-length check to the existing Synapse-specific tests for this endpoint: https://github.com/element-hq/synapse/blob/70b0e386032ed2f3ecf25fcf9a4b6c31335ffdc4/tests/federation/test_federation_media.py#L152
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Complement should definitely be checking
Content-Length
for all requests on all implementations - maybe it already is and was just missing a call to the federation thumbnail endpoint, I don't know.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To expand: HTTP servers can either respond with
Content-Length
or chunked transfer encoding, and I don't think we should be mandating one or the other. We could do this by explicitly checking for the two cases, but what we actually care about is that the file was correctly transmitted (and it won't be if theContent-Length
is wrong, unless golang HTTP client does something non-HTTP-spec compliant).