improving tests coverage of http package + using a real word request when testing enforceContentTypeHandler

This commit is contained in:
Julien Neuhart
2018-04-11 17:38:34 +02:00
parent 158bc9b3ea
commit d43fb62cd8
3 changed files with 15 additions and 10 deletions

View File

@@ -12,7 +12,6 @@ import (
"github.com/thecodingmachine/gotenberg/app/config" "github.com/thecodingmachine/gotenberg/app/config"
"github.com/thecodingmachine/gotenberg/app/context" "github.com/thecodingmachine/gotenberg/app/context"
"github.com/thecodingmachine/gotenberg/app/converter/process" "github.com/thecodingmachine/gotenberg/app/converter/process"
ghttp "github.com/thecodingmachine/gotenberg/app/http"
"github.com/justinas/alice" "github.com/justinas/alice"
) )
@@ -73,7 +72,7 @@ func TestEnforceContentLengthHandler(t *testing.T) {
t.Errorf("Handler returned a wrong status code: got '%v' want '%v'", status, http.StatusBadRequest) t.Errorf("Handler returned a wrong status code: got '%v' want '%v'", status, http.StatusBadRequest)
} }
// case 2: sends a body. // case 2: sends a real body.
path, _ := filepath.Abs("../_tests/file.docx") path, _ := filepath.Abs("../_tests/file.docx")
rr = httptest.NewRecorder() rr = httptest.NewRecorder()
h.ServeHTTP(rr, makeRequest(path)) h.ServeHTTP(rr, makeRequest(path))
@@ -100,10 +99,9 @@ func TestEnforceContentTypeHandler(t *testing.T) {
} }
// case 2: sends a good content type. // case 2: sends a good content type.
req = httptest.NewRequest(http.MethodPost, "/", nil) path, _ := filepath.Abs("../_tests/file.docx")
req.Header.Set("Content-Type", string(ghttp.MultipartFormDataContentType))
rr = httptest.NewRecorder() rr = httptest.NewRecorder()
h.ServeHTTP(rr, req) h.ServeHTTP(rr, makeRequest(path))
if status := rr.Code; status != http.StatusOK { if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong a status code: got '%v' want '%v'", status, http.StatusOK) t.Errorf("Handler returned wrong a status code: got '%v' want '%v'", status, http.StatusOK)
} }

View File

@@ -19,7 +19,7 @@ func (e *notAuthorizedContentTypeError) Error() string {
return fmt.Sprintf("Accepted value for 'Content-Type': %s", MultipartFormDataContentType) return fmt.Sprintf("Accepted value for 'Content-Type': %s", MultipartFormDataContentType)
} }
// CheckAuthorizedContentType check if the request header header has an authorized content type. // CheckAuthorizedContentType checks if the request header header has an authorized content type.
// If no authorized content type found, throws an error. // If no authorized content type found, throws an error.
func CheckAuthorizedContentType(h http.Header) error { func CheckAuthorizedContentType(h http.Header) error {
ct := findContentType(h.Get("Content-Type"), MultipartFormDataContentType) ct := findContentType(h.Get("Content-Type"), MultipartFormDataContentType)
@@ -32,13 +32,13 @@ func CheckAuthorizedContentType(h http.Header) error {
// findContentType parses a string representing a content type and tries to find // findContentType parses a string representing a content type and tries to find
// one of the given content types. // one of the given content types.
func findContentType(contentType string, contentTypes ...ContentType) ContentType { func findContentType(requestContentType string, contentTypes ...ContentType) ContentType {
for _, ct := range contentTypes { for _, ct := range contentTypes {
if i := strings.IndexRune(contentType, ';'); i != -1 { if i := strings.IndexRune(requestContentType, ';'); i != -1 {
contentType = contentType[0:i] requestContentType = requestContentType[0:i]
} }
if contentType == string(ct) { if requestContentType == string(ct) {
return ct return ct
} }
} }

View File

@@ -20,6 +20,13 @@ func TestCheckAuthorizedContentType(t *testing.T) {
if err := CheckAuthorizedContentType(req.Header); err != nil { if err := CheckAuthorizedContentType(req.Header); err != nil {
t.Error("Function should have been able to retrieve an authorized content type from request's header") t.Error("Function should have been able to retrieve an authorized content type from request's header")
} }
// case 3: uses a request with a composed content type entry in its header.
req.Header.Set("Content-Type", "multipart/form-data; boundary=—-WebKitFormBoundary7MA4YWxkTrZu0gW")
if err := CheckAuthorizedContentType(req.Header); err != nil {
t.Error("Function should have been able to retrieve an authorized content type from request's header")
}
} }
func TestNotAuthorizedContentTypeError(t *testing.T) { func TestNotAuthorizedContentTypeError(t *testing.T) {