mirror-yaf/extdetect/extension_detection_test.go
Leon Richardt 2dc5c1b011
feat: improve file extension detection
If a file extension is explicitly specified in the upload name, it is
always used directly. Detection of common file extension combinations is
also performed. Currently, only ".tar.gz" and ".tar.xz" are detected. If
you would like to add support for more common combinations, please open
an issue or pull request.

If no file extension is explicitly specified, jaf falls back to MIME
type detection via the github.com/gabriel-vasile/mimetype library.
2022-10-17 20:36:24 +02:00

65 lines
1.6 KiB
Go

package extdetect
import (
"os"
"testing"
)
func TestDetectedExtensions(t *testing.T) {
const fixturePath = "../fixtures/gps.png"
type tType struct {
name string
fileData []byte
expectedOutput string
}
pngFile, err := os.ReadFile(fixturePath)
if err != nil {
t.Fatalf("Could not open \"%s\" which is required for the test. Error: %s", fixturePath,
err)
}
tests := []tType{
{ // extension is detected correctly from file when not specified explicitly
name: "foo",
fileData: pngFile,
expectedOutput: ".png",
},
{
name: "foo.txt",
expectedOutput: ".txt",
},
{ // simple extension that's the last part of a known combination is detected correctly
name: "foo.gz",
expectedOutput: ".gz",
},
{ // simple extension that's the first part of a known combination is detected correctly
name: "foo.tar",
expectedOutput: ".tar",
},
{ // combined extension is detected correctly
name: "foo.tar.gz",
expectedOutput: ".tar.gz",
},
{
name: "foo.tar.xz",
expectedOutput: ".tar.xz",
},
{ // combined extension that is NOT known only returns the last part
name: "foo.jpg.zip",
expectedOutput: ".zip",
},
{ // combined extension is detected correctly even with many "." in the name
name: "foo.jpg.zip.tar.gz",
expectedOutput: ".tar.gz",
},
}
for _, test := range tests {
output := BuildFileExtension(test.fileData, test.name)
if output != test.expectedOutput {
t.Fatalf("got output '%s', expected '%s'", output, test.expectedOutput)
}
}
}