Skip to content

Commit

Permalink
Improve test cases for detecting content type
Browse files Browse the repository at this point in the history
  • Loading branch information
Preetha Appan committed Jul 1, 2019
1 parent f7f41c4 commit de8ae8b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion client/allocdir/alloc_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 22 additions & 9 deletions client/allocdir/alloc_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 added client/allocdir/input/happy.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added client/allocdir/input/nomad.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions client/allocdir/input/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"fmt"
)

func main() {
fmt.Println("Hello, playground")
}
1 change: 1 addition & 0 deletions client/allocdir/input/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world

0 comments on commit de8ae8b

Please sign in to comment.