diff --git a/client/allocdir/alloc_dir.go b/client/allocdir/alloc_dir.go index 08c979471..ac57def65 100644 --- a/client/allocdir/alloc_dir.go +++ b/client/allocdir/alloc_dir.go @@ -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 diff --git a/client/allocdir/alloc_dir_test.go b/client/allocdir/alloc_dir_test.go index e7103cc58..451ae32e6 100644 --- a/client/allocdir/alloc_dir_test.go +++ b/client/allocdir/alloc_dir_test.go @@ -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) + } } diff --git a/client/allocdir/input/happy.gif b/client/allocdir/input/happy.gif new file mode 100644 index 000000000..13aa264ce Binary files /dev/null and b/client/allocdir/input/happy.gif differ diff --git a/client/allocdir/input/nomad.jpg b/client/allocdir/input/nomad.jpg new file mode 100644 index 000000000..f07871e2e Binary files /dev/null and b/client/allocdir/input/nomad.jpg differ diff --git a/client/allocdir/input/test.go b/client/allocdir/input/test.go new file mode 100644 index 000000000..8fd43ed1e --- /dev/null +++ b/client/allocdir/input/test.go @@ -0,0 +1,9 @@ +package main + +import ( + "fmt" +) + +func main() { + fmt.Println("Hello, playground") +} diff --git a/client/allocdir/input/test.txt b/client/allocdir/input/test.txt new file mode 100644 index 000000000..c638ce611 --- /dev/null +++ b/client/allocdir/input/test.txt @@ -0,0 +1 @@ +hello world