Files
imgproxy/httpheaders/cdv_test.go
Victor Sokolov 2236bd8170 IMG-23: replace an old imagetype with the new one (#1482)
* imagetype (new) -> imagetype

* Always fallback in C-D, GetTypeMap -> GetTypeByName

* Do not send origin ContentType
2025-08-13 11:36:25 +02:00

128 lines
3.6 KiB
Go

package httpheaders
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestContentDispositionValue(t *testing.T) {
// Test cases for ContentDispositionValue function that generates content-disposition headers
tests := []struct {
name string
url string
filename string
ext string
returnAttachment bool
expected string
contentType string
}{
{
name: "BasicURL",
url: "http://example.com/test.jpg",
filename: "",
ext: "",
contentType: "",
returnAttachment: false,
expected: "inline; filename=\"test.jpg\"",
},
{
name: "EmptyFilename",
url: "http://example.com/path/to/",
filename: "",
ext: "",
contentType: "",
returnAttachment: false,
expected: "inline; filename=\"image\"",
},
{
name: "EmptyFilenameWithExt",
url: "http://example.com/path/to/",
filename: "",
ext: ".png",
contentType: "",
returnAttachment: false,
expected: "inline; filename=\"image.png\"",
},
{
name: "EmptyFilenameWithFilenameAndExt",
url: "http://example.com/path/to/",
filename: "example",
ext: ".png",
contentType: "",
returnAttachment: false,
expected: "inline; filename=\"example.png\"",
},
{
name: "EmptyFilenameWithFilenameOverride",
url: "http://example.com/path/to/",
filename: "example",
ext: ".jpg",
contentType: "",
returnAttachment: false,
expected: "inline; filename=\"example.jpg\"",
},
{
name: "PresentFilenameWithExtOverride",
url: "http://example.com/path/to/face.png",
filename: "",
ext: ".jpg",
contentType: "",
returnAttachment: false,
expected: "inline; filename=\"face.jpg\"",
},
{
name: "PresentFilenameWithFilenameOverride",
url: "http://example.com/path/to/123.png",
filename: "face",
ext: ".jpg",
contentType: "",
returnAttachment: false,
expected: "inline; filename=\"face.jpg\"",
},
{
name: "EmptyFilenameWithFallback",
url: "http://example.com/path/to/",
filename: "",
ext: ".png",
contentType: "",
returnAttachment: false,
expected: "inline; filename=\"image.png\"",
},
{
name: "AttachmentDisposition",
url: "http://example.com/test.jpg",
filename: "",
ext: "",
contentType: "",
returnAttachment: true,
expected: "attachment; filename=\"test.jpg\"",
},
{
name: "FilenameWithQuotes",
url: "http://example.com/test.jpg",
filename: "my\"file",
ext: ".png",
returnAttachment: false,
contentType: "",
expected: "inline; filename=\"my%22file.png\"",
},
{
name: "FilenameWithContentType",
url: "http://example.com/test",
filename: "my\"file",
ext: "",
contentType: "image/png",
returnAttachment: false,
expected: "inline; filename=\"my%22file.png\"",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := ContentDispositionValue(tc.url, tc.filename, tc.ext, tc.contentType, tc.returnAttachment)
require.Equal(t, tc.expected, result)
})
}
}