added ?download file serve query param support to force file download

This commit is contained in:
Gani Georgiev
2023-07-20 15:04:26 +03:00
parent 7e0a4e61b4
commit 50d7df45eb
3 changed files with 42 additions and 3 deletions
+30 -2
View File
@@ -257,7 +257,8 @@ func TestFileSystemServe(t *testing.T) {
scenarios := []struct {
path string
name string
customHeaders map[string]string
query map[string]string
headers map[string]string
expectError bool
expectHeaders map[string]string
}{
@@ -266,6 +267,7 @@ func TestFileSystemServe(t *testing.T) {
"missing.txt",
"test_name.txt",
nil,
nil,
true,
nil,
},
@@ -274,6 +276,7 @@ func TestFileSystemServe(t *testing.T) {
"test/sub1.txt",
"test_name.txt",
nil,
nil,
false,
map[string]string{
"Content-Disposition": "attachment; filename=test_name.txt",
@@ -288,6 +291,7 @@ func TestFileSystemServe(t *testing.T) {
"image.png",
"test_name.png",
nil,
nil,
false,
map[string]string{
"Content-Disposition": "inline; filename=test_name.png",
@@ -297,11 +301,27 @@ func TestFileSystemServe(t *testing.T) {
"Cache-Control": cacheControl,
},
},
{
// png with forced attachment
"image.png",
"test_name_download.png",
map[string]string{"download": "12"},
nil,
false,
map[string]string{
"Content-Disposition": "attachment; filename=test_name_download.png",
"Content-Type": "image/png",
"Content-Length": "73",
"Content-Security-Policy": csp,
"Cache-Control": cacheControl,
},
},
{
// svg exception
"image.svg",
"test_name.svg",
nil,
nil,
false,
map[string]string{
"Content-Disposition": "attachment; filename=test_name.svg",
@@ -316,6 +336,7 @@ func TestFileSystemServe(t *testing.T) {
"style.css",
"test_name.css",
nil,
nil,
false,
map[string]string{
"Content-Disposition": "attachment; filename=test_name.css",
@@ -329,6 +350,7 @@ func TestFileSystemServe(t *testing.T) {
// custom header
"test/sub2.txt",
"test_name.txt",
nil,
map[string]string{
"Content-Disposition": "1",
"Content-Type": "2",
@@ -353,7 +375,13 @@ func TestFileSystemServe(t *testing.T) {
res := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
for k, v := range s.customHeaders {
query := req.URL.Query()
for k, v := range s.query {
query.Set(k, v)
}
req.URL.RawQuery = query.Encode()
for k, v := range s.headers {
res.Header().Set(k, v)
}