Skip to content

Commit b742049

Browse files
committed
async uploads: add initial set of tests
Signed-off-by: Sumner Evans <[email protected]>
1 parent 39436e9 commit b742049

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

internal/client/client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ type CSAPI struct {
9797
txnID int64
9898
}
9999

100+
// CreateMedia creates an MXC URI for asynchronous media uploads.
101+
func (c *CSAPI) CreateMedia(t *testing.T) string {
102+
t.Helper()
103+
res := c.MustDoFunc(t, "POST", []string{"_matrix", "media", "v1", "create"})
104+
body := ParseJSON(t, res)
105+
return GetJSONFieldStr(t, body, "content_uri")
106+
}
107+
108+
// UploadMediaAsync uploads the provided content to the given server and media ID. Fails the test on error.
109+
func (c *CSAPI) UploadMediaAsync(t *testing.T, serverName, mediaID string, fileBody []byte, fileName string, contentType string) {
110+
t.Helper()
111+
c.MustDoFunc(t, "PUT", []string{"_matrix", "media", "v3", "upload", serverName, mediaID})
112+
}
113+
100114
// UploadContent uploads the provided content with an optional file name. Fails the test on error. Returns the MXC URI.
101115
func (c *CSAPI) UploadContent(t *testing.T, fileBody []byte, fileName string, contentType string) string {
102116
t.Helper()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package csapi_tests
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
"testing"
7+
8+
"github.com/matrix-org/complement/internal/b"
9+
"github.com/matrix-org/complement/internal/client"
10+
"github.com/matrix-org/complement/internal/data"
11+
"github.com/matrix-org/complement/runtime"
12+
)
13+
14+
func TestAsyncUpload(t *testing.T) {
15+
runtime.SkipIf(t, runtime.Dendrite) // Dendrite doesn't support async uploads
16+
17+
deployment := Deploy(t, b.BlueprintAlice)
18+
defer deployment.Destroy(t)
19+
20+
alice := deployment.Client(t, "hs1", "@alice:hs1")
21+
22+
var mxcURI, mediaID string
23+
t.Run("Create media", func(t *testing.T) {
24+
mxcURI = alice.CreateMedia(t)
25+
parts := strings.Split(mxcURI, "/")
26+
mediaID = parts[len(parts)-1]
27+
})
28+
29+
origin, mediaID := client.SplitMxc(mxcURI)
30+
31+
t.Run("Not yet uploaded", func(t *testing.T) {
32+
// Check that the media is not yet uploaded
33+
res := alice.DoFunc(t, "GET", []string{"_matrix", "media", "v3", "download", origin, mediaID})
34+
if res.StatusCode != http.StatusGatewayTimeout {
35+
t.Fatalf("Expected 504 response code, got %d", res.StatusCode)
36+
}
37+
})
38+
39+
t.Run("Upload media", func(t *testing.T) {
40+
alice.UploadMediaAsync(t, origin, mediaID, data.MatrixPng, "test.png", "image/png")
41+
})
42+
43+
t.Run("Cannot upload to a media ID that has already been uploaded to", func(t *testing.T) {
44+
alice.UploadMediaAsync(t, origin, mediaID, data.MatrixPng, "test.png", "image/png")
45+
})
46+
}

0 commit comments

Comments
 (0)