54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package uploadpolicy
|
|
|
|
import "testing"
|
|
|
|
func TestValidateFileName(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
value string
|
|
wantErr bool
|
|
}{
|
|
{name: "known extension", value: "photo.PNG"},
|
|
{name: "document", value: "report.pdf"},
|
|
{name: "empty", value: "", wantErr: true},
|
|
{name: "unknown extension", value: "payload.exe", wantErr: true},
|
|
{name: "path", value: "../photo.png", wantErr: true},
|
|
{name: "backslash path", value: `dir\photo.png`, wantErr: true},
|
|
{name: "surrounding whitespace", value: "photo.png ", wantErr: true},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if err := ValidateFileName(tt.value); (err != nil) != tt.wantErr {
|
|
t.Fatalf("ValidateFileName(%q) error = %v, wantErr %v", tt.value, err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCanServeInline(t *testing.T) {
|
|
for _, filename := range []string{"photo.png", "track.MP3", "movie.webm"} {
|
|
if !CanServeInline(filename) {
|
|
t.Errorf("CanServeInline(%q) = false", filename)
|
|
}
|
|
}
|
|
for _, filename := range []string{"report.pdf", "archive.zip", "payload.exe", "../photo.png"} {
|
|
if CanServeInline(filename) {
|
|
t.Errorf("CanServeInline(%q) = true", filename)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMIMEByExtension(t *testing.T) {
|
|
tests := map[string]string{
|
|
".JSON": "application/json",
|
|
".mp3": "audio/mpeg",
|
|
".csv": "text/csv",
|
|
".exe": "",
|
|
}
|
|
for ext, want := range tests {
|
|
if got := MIMEByExtension(ext); got != want {
|
|
t.Errorf("MIMEByExtension(%q) = %q, want %q", ext, got, want)
|
|
}
|
|
}
|
|
}
|