Add a test for issue #66

Together with af6d7623, this commit solves issue #66.

Fixes #66.
This commit is contained in:
Daan Sprenkels 2020-07-06 18:02:57 +02:00
parent af6d762378
commit 03a04389ae
2 changed files with 24 additions and 3 deletions

View File

@ -219,7 +219,7 @@ func resolveResponseContentType(r *http.Request, types []string) (string, error)
if len(types) == 0 { if len(types) == 0 {
return "", nil return "", nil
} }
acceptHeader := r.Header.Get("Accept") acceptHeader := strings.TrimSpace(r.Header.Get("Accept"))
if acceptHeader == "" { if acceptHeader == "" {
return types[0], nil return types[0], nil
} }
@ -237,7 +237,7 @@ func resolveResponseContentType(r *http.Request, types []string) (string, error)
choiceParts := strings.Split(avString, ";") choiceParts := strings.Split(avString, ";")
mediaRange := acceptHeaderMediaRangeRegex.FindStringSubmatch(choiceParts[0]) mediaRange := acceptHeaderMediaRangeRegex.FindStringSubmatch(choiceParts[0])
if mediaRange == nil { if mediaRange == nil {
return "", fmt.Errorf("bad media-range (\"%v\")", choiceParts[0]) return "", fmt.Errorf("bad media-range ('%v')", choiceParts[0])
} }
av.Type = mediaRange[1] av.Type = mediaRange[1]
av.Subtype = mediaRange[2] av.Subtype = mediaRange[2]
@ -262,7 +262,7 @@ func resolveResponseContentType(r *http.Request, types []string) (string, error)
// Check if this parameter is still invalid in any case // Check if this parameter is still invalid in any case
acceptParams := acceptHeaderAcceptParamsRegex.FindStringSubmatchIndex(choiceParts[0]) acceptParams := acceptHeaderAcceptParamsRegex.FindStringSubmatchIndex(choiceParts[0])
if acceptParams == nil { if acceptParams == nil {
return "", fmt.Errorf("bad accept-params (\"%v\")", choiceParts[0]) return "", fmt.Errorf("bad accept-params ('%v')", choiceParts[0])
} }
} }
avs[i] = av avs[i] = av

View File

@ -20,6 +20,24 @@ func resolveResponseContentTypeSuccess(t *testing.T, expected string, types []st
} }
} }
func resolveResponseContentTypeError(t *testing.T, expected string, types []string, acceptVal string) {
r, err := http.NewRequest("HEAD", "", nil)
if err != nil {
panic(err)
}
r.Header.Set("Accept", acceptVal)
got, err := resolveResponseContentType(r, types)
if err == nil {
t.Errorf("expected an error, but got a success: '%v'", got)
}
if got != "" {
t.Errorf("error: return value should be empty, not '%v'", got)
}
if err.Error() != expected {
t.Errorf("wrong error value error: got '%v', want '%v'\n", got, expected)
}
}
func TestResolveResponseContentType(t *testing.T) { func TestResolveResponseContentType(t *testing.T) {
resolveResponseContentTypeSuccess(t, "", []string{}, "text/html") resolveResponseContentTypeSuccess(t, "", []string{}, "text/html")
resolveResponseContentTypeSuccess(t, "text/html", []string{"text/html"}, "") resolveResponseContentTypeSuccess(t, "text/html", []string{"text/html"}, "")
@ -30,4 +48,7 @@ func TestResolveResponseContentType(t *testing.T) {
// Issue #17 // Issue #17
resolveResponseContentTypeSuccess(t, "*/*", []string{"*/*"}, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3") resolveResponseContentTypeSuccess(t, "*/*", []string{"*/*"}, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
// Issue #66
resolveResponseContentTypeError(t, "bad media-range ('*')", []string{"text/plain"}, " *")
} }