diff --git a/app/handlers_test.go b/app/handlers_test.go index cb4c4cfa..1e150625 100644 --- a/app/handlers_test.go +++ b/app/handlers_test.go @@ -12,7 +12,6 @@ import ( "github.com/thecodingmachine/gotenberg/app/config" "github.com/thecodingmachine/gotenberg/app/context" "github.com/thecodingmachine/gotenberg/app/converter/process" - ghttp "github.com/thecodingmachine/gotenberg/app/http" "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) } - // case 2: sends a body. + // case 2: sends a real body. path, _ := filepath.Abs("../_tests/file.docx") rr = httptest.NewRecorder() h.ServeHTTP(rr, makeRequest(path)) @@ -100,10 +99,9 @@ func TestEnforceContentTypeHandler(t *testing.T) { } // case 2: sends a good content type. - req = httptest.NewRequest(http.MethodPost, "/", nil) - req.Header.Set("Content-Type", string(ghttp.MultipartFormDataContentType)) + path, _ := filepath.Abs("../_tests/file.docx") rr = httptest.NewRecorder() - h.ServeHTTP(rr, req) + h.ServeHTTP(rr, makeRequest(path)) if status := rr.Code; status != http.StatusOK { t.Errorf("Handler returned wrong a status code: got '%v' want '%v'", status, http.StatusOK) } diff --git a/app/http/http.go b/app/http/http.go index 2b5babde..31a4a2ba 100644 --- a/app/http/http.go +++ b/app/http/http.go @@ -19,7 +19,7 @@ func (e *notAuthorizedContentTypeError) Error() string { 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. func CheckAuthorizedContentType(h http.Header) error { 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 // one of the given content types. -func findContentType(contentType string, contentTypes ...ContentType) ContentType { +func findContentType(requestContentType string, contentTypes ...ContentType) ContentType { for _, ct := range contentTypes { - if i := strings.IndexRune(contentType, ';'); i != -1 { - contentType = contentType[0:i] + if i := strings.IndexRune(requestContentType, ';'); i != -1 { + requestContentType = requestContentType[0:i] } - if contentType == string(ct) { + if requestContentType == string(ct) { return ct } } diff --git a/app/http/http_test.go b/app/http/http_test.go index e5bdbd3a..16b60212 100644 --- a/app/http/http_test.go +++ b/app/http/http_test.go @@ -20,6 +20,13 @@ func TestCheckAuthorizedContentType(t *testing.T) { if err := CheckAuthorizedContentType(req.Header); err != nil { 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) {