summaryrefslogtreecommitdiffstats
path: root/api4/file_test.go
diff options
context:
space:
mode:
authorHarrison Healey <harrisonmhealey@gmail.com>2017-08-08 11:47:17 -0400
committerSaturnino Abril <saturnino.abril@gmail.com>2017-08-08 23:47:17 +0800
commit6964ae61dc2f2c6a9b6c515c1a3b204d2c8e2e5f (patch)
tree63f8da7656ab75414077fb1e59cdb69dbf74878e /api4/file_test.go
parent2105b10ccdff58a6d1986776c37fc179249f369f (diff)
downloadchat-6964ae61dc2f2c6a9b6c515c1a3b204d2c8e2e5f.tar.gz
chat-6964ae61dc2f2c6a9b6c515c1a3b204d2c8e2e5f.tar.bz2
chat-6964ae61dc2f2c6a9b6c515c1a3b204d2c8e2e5f.zip
Added unit tests for getFile headers (#7045)
* Added unit tests for getFile headers * Fixed exe type test to run correctly on multiple platforms * Make sure we close the body on all Client4 calls * Changed Response.Response field to Response.Header * Clarified type of Response.Header
Diffstat (limited to 'api4/file_test.go')
-rw-r--r--api4/file_test.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/api4/file_test.go b/api4/file_test.go
index 92f275d85..c2bc926e1 100644
--- a/api4/file_test.go
+++ b/api4/file_test.go
@@ -165,6 +165,71 @@ func TestGetFile(t *testing.T) {
CheckNoError(t, resp)
}
+func TestGetFileHeaders(t *testing.T) {
+ th := Setup().InitBasic()
+ defer TearDown()
+
+ Client := th.Client
+ channel := th.BasicChannel
+
+ if utils.Cfg.FileSettings.DriverName == "" {
+ t.Skip("skipping because no file driver is enabled")
+ }
+
+ testHeaders := func(data []byte, filename string, expectedContentType string, getInline bool) func(*testing.T) {
+ return func(t *testing.T) {
+ fileResp, resp := Client.UploadFile(data, channel.Id, filename)
+ CheckNoError(t, resp)
+
+ fileId := fileResp.FileInfos[0].Id
+
+ _, resp = Client.GetFile(fileId)
+ CheckNoError(t, resp)
+
+ if contentType := resp.Header.Get("Content-Type"); !strings.HasPrefix(contentType, expectedContentType) {
+ t.Fatal("returned incorrect Content-Type", contentType)
+ }
+
+ if getInline {
+ if contentDisposition := resp.Header.Get("Content-Disposition"); !strings.HasPrefix(contentDisposition, "inline") {
+ t.Fatal("returned incorrect Content-Disposition", contentDisposition)
+ }
+ } else {
+ if contentDisposition := resp.Header.Get("Content-Disposition"); !strings.HasPrefix(contentDisposition, "attachment") {
+ t.Fatal("returned incorrect Content-Disposition", contentDisposition)
+ }
+ }
+
+ _, resp = Client.DownloadFile(fileId, true)
+ CheckNoError(t, resp)
+
+ if contentType := resp.Header.Get("Content-Type"); !strings.HasPrefix(contentType, expectedContentType) {
+ t.Fatal("returned incorrect Content-Type", contentType)
+ }
+
+ if contentDisposition := resp.Header.Get("Content-Disposition"); !strings.HasPrefix(contentDisposition, "attachment") {
+ t.Fatal("returned incorrect Content-Disposition", contentDisposition)
+ }
+ }
+ }
+
+ data := []byte("ABC")
+
+ t.Run("png", testHeaders(data, "test.png", "image/png", true))
+ t.Run("gif", testHeaders(data, "test.gif", "image/gif", true))
+ t.Run("mp4", testHeaders(data, "test.mp4", "video/mp4", true))
+ t.Run("mp3", testHeaders(data, "test.mp3", "audio/mpeg", true))
+ t.Run("pdf", testHeaders(data, "test.pdf", "application/pdf", false))
+ t.Run("txt", testHeaders(data, "test.txt", "text/plain", false))
+ t.Run("html", testHeaders(data, "test.html", "text/plain", false))
+ t.Run("js", testHeaders(data, "test.js", "text/plain", false))
+ t.Run("go", testHeaders(data, "test.go", "application/octet-stream", false))
+ t.Run("zip", testHeaders(data, "test.zip", "application/zip", false))
+ t.Run("exe", testHeaders(data, "test.exe", "application/x-ms", false))
+ t.Run("no extension", testHeaders(data, "test", "application/octet-stream", false))
+ t.Run("no extension 2", testHeaders([]byte("<html></html>"), "test", "application/octet-stream", false))
+}
+
func TestGetFileThumbnail(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()