Skip to content

Commit 39733c1

Browse files
authored
Add a basic test fetching thumbnail from _matrix/federation/v1/media/thumbnail (#732)
* add a basic test fetching thumbnail from federation endpoint * test federation thumbnail body against thumbnail body * verify we we actually got an image
1 parent 6e4426a commit 39733c1

File tree

1 file changed

+97
-5
lines changed

1 file changed

+97
-5
lines changed

tests/media_thumbnail_test.go

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@ package tests
22

33
import (
44
"bytes"
5+
"context"
6+
"github.com/matrix-org/complement"
7+
"github.com/matrix-org/complement/client"
8+
"github.com/matrix-org/complement/federation"
9+
"github.com/matrix-org/complement/helpers"
10+
"github.com/matrix-org/complement/internal/data"
511
"github.com/matrix-org/complement/runtime"
12+
"github.com/matrix-org/gomatrixserverlib/fclient"
13+
"github.com/matrix-org/gomatrixserverlib/spec"
614
"image/jpeg"
715
"image/png"
816
"io"
17+
"mime"
18+
"mime/multipart"
919
"net/http"
1020
"net/url"
1121
"strings"
1222
"testing"
13-
14-
"github.com/matrix-org/complement"
15-
"github.com/matrix-org/complement/client"
16-
"github.com/matrix-org/complement/helpers"
17-
"github.com/matrix-org/complement/internal/data"
1823
)
1924

2025
// TODO: add JPEG testing
@@ -68,6 +73,93 @@ func TestRemotePngThumbnail(t *testing.T) {
6873
}
6974
}
7075

76+
func TestFederationThumbnail(t *testing.T) {
77+
runtime.SkipIf(t, runtime.Dendrite)
78+
79+
deployment := complement.Deploy(t, 1)
80+
defer deployment.Destroy(t)
81+
82+
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
83+
84+
srv := federation.NewServer(t, deployment,
85+
federation.HandleKeyRequests(),
86+
)
87+
cancel := srv.Listen()
88+
defer cancel()
89+
origin := spec.ServerName(srv.ServerName())
90+
91+
fileName := "test.png"
92+
contentType := "image/png"
93+
94+
uri := alice.UploadContent(t, data.LargePng, fileName, contentType)
95+
mediaOrigin, mediaId := client.SplitMxc(uri)
96+
97+
path := []string{"_matrix", "media", "v3", "thumbnail", mediaOrigin, mediaId}
98+
res := alice.MustDo(t, "GET", path, client.WithQueries(url.Values{
99+
"width": []string{"32"},
100+
"height": []string{"32"},
101+
"method": []string{"scale"},
102+
}))
103+
104+
if res.StatusCode != 200 {
105+
t.Fatalf("thumbnail request for uploaded file failed")
106+
}
107+
108+
body, err := io.ReadAll(res.Body)
109+
if err != nil {
110+
t.Fatalf("thumbnail request for uploaded file failed: %s", err)
111+
}
112+
113+
fedReq := fclient.NewFederationRequest(
114+
"GET",
115+
origin,
116+
"hs1",
117+
"/_matrix/federation/v1/media/thumbnail/"+mediaId+"?method=scale&width=32&height=32",
118+
)
119+
120+
resp, err := srv.DoFederationRequest(context.Background(), t, deployment, fedReq)
121+
if err != nil {
122+
t.Fatalf("federation thumbnail request for uploaded file failed: %s", err)
123+
}
124+
if resp.StatusCode != http.StatusOK {
125+
t.Fatalf("federation thumbnail request for uploaded file failed with status code: %v", resp.StatusCode)
126+
}
127+
128+
resContentType := resp.Header.Get("Content-Type")
129+
_, params, err := mime.ParseMediaType(resContentType)
130+
131+
if err != nil {
132+
t.Fatalf("failed to parse multipart response: %s", err)
133+
}
134+
135+
foundImage := false
136+
reader := multipart.NewReader(resp.Body, params["boundary"])
137+
for {
138+
p, err := reader.NextPart()
139+
if err == io.EOF { // End of the multipart content
140+
break
141+
}
142+
if err != nil {
143+
t.Fatalf("failed to read multipart response: %s", err)
144+
}
145+
146+
partContentType := p.Header.Get("Content-Type")
147+
if partContentType == contentType {
148+
imageBody, err := io.ReadAll(p)
149+
if err != nil {
150+
t.Fatalf("failed to read multipart part %s: %s", partContentType, err)
151+
}
152+
if !bytes.Equal(imageBody, body) {
153+
t.Fatalf("body does not match uploaded file")
154+
}
155+
foundImage = true
156+
}
157+
}
158+
if !foundImage {
159+
t.Fatalf("No image was found in response.")
160+
}
161+
}
162+
71163
func fetchAndValidateThumbnail(t *testing.T, c *client.CSAPI, mxcUri string, authenticated bool) {
72164
t.Helper()
73165

0 commit comments

Comments
 (0)