mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-15 20:02:15 +01:00
improving tests coverage of http package + using a real word request when testing enforceContentTypeHandler
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user