Fixed tests

This commit is contained in:
Marc Ole Bulling
2024-08-05 17:39:07 +02:00
parent 5ef0e89297
commit d6b0670c18
5 changed files with 26 additions and 19 deletions
+1 -1
View File
@@ -544,7 +544,7 @@ func ServeFile(file models.File, w http.ResponseWriter, r *http.Request, forceDo
}
}
statusId := downloadstatus.SetDownload(file)
headers.WriteDownloadHeaders(file, w, forceDownload)
headers.Write(file, w, forceDownload)
if file.Encryption.IsEncrypted && !file.RequiresClientDecryption() {
err := encryption.DecryptReader(file.Encryption, fileData, w)
if err != nil {
-15
View File
@@ -776,18 +776,3 @@ func TestDeleteAllEncrypted(t *testing.T) {
test.IsEqualBool(t, ok, true)
test.IsEqualBool(t, data.UnlimitedTime, true)
}
func TestWriteDownloadHeaders(t *testing.T) {
file := models.File{Name: "testname", ContentType: "testtype"}
w, _ := test.GetRecorder("GET", "/test", nil, nil, nil)
writeDownloadHeaders(file, w, true)
test.IsEqualString(t, w.Result().Header.Get("Content-Disposition"), "attachment; filename=\"testname\"")
w, _ = test.GetRecorder("GET", "/test", nil, nil, nil)
writeDownloadHeaders(file, w, false)
test.IsEqualString(t, w.Result().Header.Get("Content-Disposition"), "inline; filename=\"testname\"")
test.IsEqualString(t, w.Result().Header.Get("Content-Type"), "testtype")
file.Encryption.IsEncrypted = true
w, _ = test.GetRecorder("GET", "/test", nil, nil, nil)
writeDownloadHeaders(file, w, false)
test.IsEqualString(t, w.Result().Header.Get("Accept-Ranges"), "bytes")
}
@@ -173,7 +173,7 @@ func proxyDownload(w http.ResponseWriter, file models.File, forceDownload bool)
return err
}
defer resp.Body.Close()
headers.WriteDownloadHeaders(file, w, forceDownload)
headers.Write(file, w, forceDownload)
_, _ = io.Copy(w, resp.Body)
return nil
}
+2 -2
View File
@@ -6,9 +6,9 @@ import (
"time"
)
// WriteDownloadHeaders sets headers to either display the file inline or to force download, the content type
// Write sets headers to either display the file inline or to force download, the content type
// and if the file is encrypted, the creation timestamp to now
func WriteDownloadHeaders(file models.File, w http.ResponseWriter, forceDownload bool) {
func Write(file models.File, w http.ResponseWriter, forceDownload bool) {
if forceDownload {
w.Header().Set("Content-Disposition", "attachment; filename=\""+file.Name+"\"")
} else {
@@ -0,0 +1,22 @@
package headers
import (
"github.com/forceu/gokapi/internal/models"
"github.com/forceu/gokapi/internal/test"
"testing"
)
func TestWriteDownloadHeaders(t *testing.T) {
file := models.File{Name: "testname", ContentType: "testtype"}
w, _ := test.GetRecorder("GET", "/test", nil, nil, nil)
Write(file, w, true)
test.IsEqualString(t, w.Result().Header.Get("Content-Disposition"), "attachment; filename=\"testname\"")
w, _ = test.GetRecorder("GET", "/test", nil, nil, nil)
Write(file, w, false)
test.IsEqualString(t, w.Result().Header.Get("Content-Disposition"), "inline; filename=\"testname\"")
test.IsEqualString(t, w.Result().Header.Get("Content-Type"), "testtype")
file.Encryption.IsEncrypted = true
w, _ = test.GetRecorder("GET", "/test", nil, nil, nil)
Write(file, w, false)
test.IsEqualString(t, w.Result().Header.Get("Accept-Ranges"), "bytes")
}