mirror of
https://github.com/kemko/nomad.git
synced 2026-01-04 09:25:46 +03:00
Improve test cases for detecting content type
This commit is contained in:
@@ -410,7 +410,7 @@ func (d *AllocDir) Stat(path string) (*cstructs.AllocFileInfo, error) {
|
||||
// detectContentType tries to infer the file type by reading the first
|
||||
// 512 bytes of the file. Json file extensions are special cased.
|
||||
func detectContentType(fileInfo os.FileInfo, path string) string {
|
||||
contentType := "unknown"
|
||||
contentType := "application/octet-stream"
|
||||
if !fileInfo.IsDir() {
|
||||
f, err := os.Open(path)
|
||||
// Best effort content type detection
|
||||
|
||||
@@ -475,15 +475,28 @@ func TestPathFuncs(t *testing.T) {
|
||||
|
||||
func TestAllocDir_DetectContentType(t *testing.T) {
|
||||
require := require.New(t)
|
||||
imgPath := "input/image.png"
|
||||
fileInfo, err := os.Stat(imgPath)
|
||||
inputPath := "input/"
|
||||
var testFiles []string
|
||||
err := filepath.Walk(inputPath, func(path string, info os.FileInfo, err error) error {
|
||||
if !info.IsDir() {
|
||||
testFiles = append(testFiles, path)
|
||||
}
|
||||
return err
|
||||
})
|
||||
require.Nil(err)
|
||||
res := detectContentType(fileInfo, imgPath)
|
||||
require.Equal("image/png", res)
|
||||
|
||||
jsonPath := "input/test.json"
|
||||
fileInfo, err = os.Stat(jsonPath)
|
||||
require.Nil(err)
|
||||
res = detectContentType(fileInfo, jsonPath)
|
||||
require.Equal("application/json", res)
|
||||
expectedEncodings := map[string]string{
|
||||
"input/happy.gif": "image/gif",
|
||||
"input/image.png": "image/png",
|
||||
"input/nomad.jpg": "image/jpeg",
|
||||
"input/test.go": "application/octet-stream",
|
||||
"input/test.json": "application/json",
|
||||
"input/test.txt": "text/plain; charset=utf-8",
|
||||
}
|
||||
for _, file := range testFiles {
|
||||
fileInfo, err := os.Stat(file)
|
||||
require.Nil(err)
|
||||
res := detectContentType(fileInfo, file)
|
||||
require.Equal(expectedEncodings[file], res)
|
||||
}
|
||||
}
|
||||
|
||||
BIN
client/allocdir/input/happy.gif
Normal file
BIN
client/allocdir/input/happy.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 475 KiB |
BIN
client/allocdir/input/nomad.jpg
Normal file
BIN
client/allocdir/input/nomad.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 72 KiB |
9
client/allocdir/input/test.go
Normal file
9
client/allocdir/input/test.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, playground")
|
||||
}
|
||||
1
client/allocdir/input/test.txt
Normal file
1
client/allocdir/input/test.txt
Normal file
@@ -0,0 +1 @@
|
||||
hello world
|
||||
Reference in New Issue
Block a user