Improve test cases for detecting content type

This commit is contained in:
Preetha Appan
2019-07-01 16:24:48 -05:00
parent f7f41c42e6
commit de8ae8bcd2
6 changed files with 33 additions and 10 deletions

View File

@@ -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

View File

@@ -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)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 475 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

View File

@@ -0,0 +1,9 @@
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, playground")
}

View File

@@ -0,0 +1 @@
hello world