From 15916e80e6661ed29039114fde78ae3a9852bd05 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Wed, 4 Apr 2018 18:10:18 +0200 Subject: [PATCH] tests for http package --- app/http/http.go | 2 +- app/http/http_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 app/http/http_test.go diff --git a/app/http/http.go b/app/http/http.go index 6775c94d..adf0630d 100644 --- a/app/http/http.go +++ b/app/http/http.go @@ -33,7 +33,7 @@ func (e *notAuthorizedContentTypeError) Error() string { // FindAuthorizedContentType tries to return a content type according to a request header. // If no authorized content type found, throws an error. func FindAuthorizedContentType(h http.Header) (ContentType, error) { - ct := findContentType(h.Get("Content-Type"), HTMLContentType, OctetStreamContentType, MultipartFormDataContentType) + ct := findContentType(h.Get("Content-Type"), OctetStreamContentType, MultipartFormDataContentType) if ct == "" { return "", ¬AuthorizedContentTypeError{} } diff --git a/app/http/http_test.go b/app/http/http_test.go new file mode 100644 index 00000000..92bc54b0 --- /dev/null +++ b/app/http/http_test.go @@ -0,0 +1,42 @@ +package http + +import ( + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" +) + +func TestFindAuthorizedContentType(t *testing.T) { + req := httptest.NewRequest(http.MethodPost, "/", nil) + + // case 1: uses a request without a content type entry in its header. + if _, err := FindAuthorizedContentType(req.Header); err == nil { + t.Error("It should not have been able to retrieve an authorized content type from header!") + } + + // case 2: uses a request with a content type entry in its header. + req.Header.Set("Content-Type", string(MultipartFormDataContentType)) + if _, err := FindAuthorizedContentType(req.Header); err != nil { + t.Error("It should have been able to retrieve an authorized content type from header!") + } +} + +func TestSniffContentType(t *testing.T) { + // case 1: uses a file with a wrong content type. + path, _ := filepath.Abs("../../_tests/configurations/gotenberg.yml") + f, _ := os.Open(path) + defer f.Close() + if _, err := SniffContentType(f); err == nil { + t.Error("It should not have been able to retrieve an authorized content type from an YAML file!") + } + + // case 2: uses a file with a correct content type. + path, _ = filepath.Abs("../../_tests/file.pdf") + f, _ = os.Open(path) + defer f.Close() + if _, err := SniffContentType(f); err != nil { + t.Error("It should have been able to retrieve an authorized content type from a PDF file!") + } +}