Merge pull request #31 from blunket/master

add content type test
This commit is contained in:
George Shaw 2017-10-30 15:34:23 -05:00 committed by GitHub
commit c29e105501
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 36 deletions

View file

@ -9,13 +9,11 @@ import (
)
const (
PRIVATE_PREFIX string = "PRIVATE::"
PUBLIC_PREFIX string = "PUBLIC::"
)
PRIVATE_PREFIX = "PRIVATE::"
PUBLIC_PREFIX = "PUBLIC::"
const (
NORMAL_LOG int = 1
FATAL_LOG int = 2
NORMAL_LOG = 1
FATAL_LOG = 2
)
// Console logs a prefix, IP, and message all appeneded together to the console.

View file

@ -4,13 +4,11 @@ package logging
import "os"
const (
PRIVATE_LOG_FOLDER string = "logs/private/"
PUBLIC_LOG_FOLDER string = "logs/public/"
)
PRIVATE_LOG_FOLDER = "logs/private/"
PUBLIC_LOG_FOLDER = "logs/public/"
const (
ERROR string = "error.log"
WARNING string = "warning.log"
ERROR = "error.log"
WARNING = "warning.log"
)
// File logs a given message to a log file

View file

@ -3,38 +3,30 @@ 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
fileType := filepath.Ext(path)
var ct string
if fileType == "" {
return contentType, errors.New("Invalid path, contained no period-separated content-type")
path = filepath.Base(path)
ext := filepath.Ext(path)
fn := strings.TrimSuffix(path, ext)
if ext == "" || fn == "" {
return "", errors.New("Invalid file path")
}
switch fileType {
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
}

View file

@ -0,0 +1,35 @@
package routing
import (
"testing"
)
func TestContentType(t *testing.T) {
cases := []struct {
input string
ok bool
}{
{"/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): Expected OK, got error: %s", tc.input, err)
}
if err == nil && tc.ok == false {
t.Fatalf("\nGetContentType(%s): Expected error, got OK: %s", tc.input, mime)
}
}
}