mirror of
https://github.com/donl/gPanel.git
synced 2026-05-26 06:12:20 -06:00
use mime package, update tests
This commit is contained in:
parent
e098a7a743
commit
c3d0aaa714
2 changed files with 22 additions and 35 deletions
|
|
@ -3,17 +3,16 @@ package routing
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"mime"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// getContentType returns the http header safe content-type attribute for the
|
||||
// requested path. If the requested path is not formatted correctly with an extension
|
||||
// getContentType returns the http header safe Content-Type for the requested
|
||||
// path. If the requested path is not formatted correctly with an extension
|
||||
// then the function will return the zero-value for a string and an error.
|
||||
// BUG(george-e-shaw-iv) Cannot handle interpreted files such as .php files
|
||||
// BUG(george-e-shaw-iv) Does not cover the bulk of encountered content types on the web
|
||||
func GetContentType(path string) (string, error) {
|
||||
var contentType string
|
||||
var ct string
|
||||
|
||||
path = filepath.Base(path)
|
||||
ext := filepath.Ext(path)
|
||||
|
|
@ -23,22 +22,11 @@ func GetContentType(path string) (string, error) {
|
|||
return "", errors.New("Invalid file path")
|
||||
}
|
||||
|
||||
switch ext {
|
||||
case ".html":
|
||||
contentType = "text/html"
|
||||
case ".css":
|
||||
contentType = "text/css"
|
||||
case ".js":
|
||||
contentType = "text/javascript"
|
||||
case ".png":
|
||||
contentType = "image/png"
|
||||
case ".jpg":
|
||||
fallthrough
|
||||
case ".jpeg":
|
||||
contentType = "image/jpeg"
|
||||
default:
|
||||
contentType = "text/plain"
|
||||
ct = mime.TypeByExtension(ext)
|
||||
|
||||
if ct == "" {
|
||||
return "", errors.New("Could not get content type")
|
||||
}
|
||||
|
||||
return contentType, nil
|
||||
return ct, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,29 +7,28 @@ import (
|
|||
func TestContentType(t *testing.T) {
|
||||
cases := []struct {
|
||||
input string
|
||||
mime string
|
||||
ok bool
|
||||
}{
|
||||
{"/path/to/index.html", "text/html", true},
|
||||
{"somepath/file.css", "text/css", true},
|
||||
{"test.js", "text/javascript", true},
|
||||
{"asdf.png", "image/png", true},
|
||||
{"asdfpng", "", false},
|
||||
{"/something.jpg", "image/jpeg", true},
|
||||
{"/something.jpeg", "image/jpeg", true},
|
||||
{"this/should/fail/.png", "", false},
|
||||
{"/path/to/index.html", true},
|
||||
{"somepath/file.css", true},
|
||||
{"test.js", true},
|
||||
{"./relative/asdf.png", true},
|
||||
{"/something.jpg", true},
|
||||
{"/something.jpeg", true},
|
||||
{"doc.pdf", true},
|
||||
{"this/should/fail/.png", false},
|
||||
{"php/doesnt/have/a/contenttype.php", false},
|
||||
{".pdf", false},
|
||||
{"asdfpng", false},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
mime, err := GetContentType(tc.input)
|
||||
if err != nil && tc.ok == true {
|
||||
t.Fatalf("\nGetContentType(%s): Got an error, expected OK.", tc.input)
|
||||
t.Fatalf("\nGetContentType(%s): Expected OK, got error: %s", tc.input, err)
|
||||
}
|
||||
if err == nil && tc.ok == false {
|
||||
t.Fatalf("\nGetContentType(%s): Expected error, got: %s", tc.input, tc.mime)
|
||||
}
|
||||
if mime != tc.mime {
|
||||
t.Fatalf("\nGetContentType(%s):\nWant: %s\nGot: %s", tc.input, tc.mime, mime)
|
||||
t.Fatalf("\nGetContentType(%s): Expected error, got OK: %s", tc.input, mime)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue