mirror of
https://github.com/Forceu/Gokapi.git
synced 2026-05-03 21:10:50 -05:00
Fixed tests
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user