From d6c3b171db4b4fe4fff4a0ce20f7a4f4a676a613 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Thu, 8 Jun 2017 10:40:39 +0200 Subject: [PATCH] cmd/mediaapi-integration-tests: Add upload/download/thumbnail tests --- .../cmd/mediaapi-integration-tests/main.go | 96 ++++++++++++++++++- 1 file changed, 91 insertions(+), 5 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/cmd/mediaapi-integration-tests/main.go b/src/github.com/matrix-org/dendrite/cmd/mediaapi-integration-tests/main.go index bc0f8b43f..d41ecad0c 100644 --- a/src/github.com/matrix-org/dendrite/cmd/mediaapi-integration-tests/main.go +++ b/src/github.com/matrix-org/dendrite/cmd/mediaapi-integration-tests/main.go @@ -21,6 +21,7 @@ import ( "os" "os/exec" "path" + "path/filepath" "strconv" "time" @@ -38,6 +39,8 @@ var ( testDatabaseName = test.Defaulting(os.Getenv("DATABASE_NAME"), "mediaapi_test") // Postgres docker container name (for running psql) postgresContainerName = os.Getenv("POSTGRES_CONTAINER") + // Test image to be uploaded/downloaded + testJPEG = test.Defaulting(os.Getenv("TEST_JPEG_PATH"), "src/github.com/matrix-org/dendrite/cmd/mediaapi-integration-tests/totem.jpg") ) var thumbnailPregenerationConfig = (` @@ -103,6 +106,8 @@ func startMediaAPI(suffix string, dynamicThumbnails bool) (*exec.Cmd, chan error postgresContainerName, databases, ) + + fmt.Printf("==TESTSERVER== STARTED %v -> %v : %v\n", proxyAddr, serverAddr, dir) return cmd, cmdChan, serverAddr, proxyCmd, proxyCmdChan, proxyAddr, dir } @@ -149,23 +154,80 @@ func main() { defer server1ProxyCmd.Process.Kill() testDownload(server1ProxyAddr, server1ProxyAddr, "doesnotexist", "", 404, server1CmdChan) + // upload a JPEG file + testUpload(server1ProxyAddr, testJPEG, "image/jpeg", `{ + "content_uri": "mxc://localhost:18001/1VuVy8u_hmDllD8BrcY0deM34Bl7SPJeY9J6BkMmpx0" + }`, 200, server1CmdChan) + + // download that JPEG file + testDownload(server1ProxyAddr, "localhost:18001", "1VuVy8u_hmDllD8BrcY0deM34Bl7SPJeY9J6BkMmpx0", "", 200, server1CmdChan) + + // thumbnail that JPEG file + testThumbnail(64, 64, "crop", server1ProxyAddr, "localhost:18001", "1VuVy8u_hmDllD8BrcY0deM34Bl7SPJeY9J6BkMmpx0", "", 200, server1CmdChan) + // create server2 with dynamic thumbnail generation server2Cmd, server2CmdChan, _, server2ProxyCmd, _, server2ProxyAddr, server2Dir := startMediaAPI("2", true) defer cleanUpServer(server2Cmd, server2Dir) defer server2ProxyCmd.Process.Kill() testDownload(server2ProxyAddr, server2ProxyAddr, "doesnotexist", "", 404, server2CmdChan) + + // download that JPEG file via server2 + testDownload(server2ProxyAddr, "localhost:18001", "1VuVy8u_hmDllD8BrcY0deM34Bl7SPJeY9J6BkMmpx0", "", 200, server2CmdChan) + + // thumbnail that JPEG file via server2 + testThumbnail(800, 600, "scale", server2ProxyAddr, "localhost:18001", "1VuVy8u_hmDllD8BrcY0deM34Bl7SPJeY9J6BkMmpx0", "", 200, server2CmdChan) + + // thumbnail that JPEG file via server2 + testThumbnail(10000, 10000, "scale", server2ProxyAddr, "localhost:18001", "1VuVy8u_hmDllD8BrcY0deM34Bl7SPJeY9J6BkMmpx0", "", 200, server2CmdChan) + } -func getMediaURI(scheme, host, endpoint string, components []string) string { +func getMediaURI(scheme, host, endpoint, query string, components []string) string { pathComponents := []string{host, "_matrix/media/v1", endpoint} pathComponents = append(pathComponents, components...) - return scheme + path.Join(pathComponents...) + return scheme + path.Join(pathComponents...) + query +} + +func testUpload(host, filePath, contentType, wantedBody string, wantedStatusCode int, serverCmdChan chan error) { + fmt.Printf("==TESTING== upload %v to %v\n", filePath, host) + file, err := os.Open(filePath) + defer file.Close() + if err != nil { + panic(err) + } + filename := filepath.Base(filePath) + stat, err := file.Stat() + if os.IsNotExist(err) { + panic(err) + } + fileSize := stat.Size() + + req, err := http.NewRequest( + "POST", + getMediaURI("http://", host, "upload", "?filename="+filename, nil), + file, + ) + if err != nil { + panic(err) + } + req.ContentLength = fileSize + req.Header.Set("Content-Type", contentType) + + testReq := &test.Request{ + Req: req, + WantedStatusCode: wantedStatusCode, + WantedBody: test.CanonicalJSONInput([]string{wantedBody})[0], + } + if err := testReq.Do(); err != nil { + panic(err) + } + fmt.Printf("==TESTING== upload %v to %v PASSED\n", filePath, host) } func testDownload(host, origin, mediaID, wantedBody string, wantedStatusCode int, serverCmdChan chan error) { req, err := http.NewRequest( "GET", - getMediaURI("http://", host, "download", []string{ + getMediaURI("http://", host, "download", "", []string{ origin, mediaID, }), @@ -177,7 +239,31 @@ func testDownload(host, origin, mediaID, wantedBody string, wantedStatusCode int testReq := &test.Request{ Req: req, WantedStatusCode: wantedStatusCode, - WantedBody: wantedBody, + WantedBody: test.CanonicalJSONInput([]string{wantedBody})[0], } - testReq.Run("media-api", timeout, serverCmdChan) + testReq.Run(fmt.Sprintf("download mxc://%v/%v from %v", origin, mediaID, host), timeout, serverCmdChan) +} + +func testThumbnail(width, height int, resizeMethod, host, origin, mediaID, wantedBody string, wantedStatusCode int, serverCmdChan chan error) { + query := fmt.Sprintf("?width=%v&height=%v", width, height) + if resizeMethod != "" { + query += "&method=" + resizeMethod + } + req, err := http.NewRequest( + "GET", + getMediaURI("http://", host, "thumbnail", query, []string{ + origin, + mediaID, + }), + nil, + ) + if err != nil { + panic(err) + } + testReq := &test.Request{ + Req: req, + WantedStatusCode: wantedStatusCode, + WantedBody: test.CanonicalJSONInput([]string{wantedBody})[0], + } + testReq.Run(fmt.Sprintf("thumbnail mxc://%v/%v from %v", origin, mediaID, host), timeout, serverCmdChan) }