Merge b593c6ea2a18f8879c22efdff00cdc61c02e41c6 into cc367f9125516313a4b8d2da1eed5527b5420dbc

This commit is contained in:
eleven26 2022-12-02 10:22:47 +08:00 committed by GitHub
commit ebe3969e0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -1147,7 +1147,8 @@ func (c *Context) NegotiateFormat(offered ...string) string {
// According to RFC 2616 and RFC 2396, non-ASCII characters are not allowed in headers,
// therefore we can just iterate over the string without casting it into []rune
i := 0
for ; i < len(accepted); i++ {
minLen := min(len(accepted), len(offer))
for ; i < minLen; i++ {
if accepted[i] == '*' || offer[i] == '*' {
return offer
}

View File

@ -1311,6 +1311,15 @@ func TestContextNegotiationFormatCustom(t *testing.T) {
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON))
}
func TestContextNegotiationFormatWithShorterOfferLength(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", nil)
c.Request.Header.Add("Accept", "text/html")
assert.Equal(t, "", c.NegotiateFormat("text/htm"))
assert.Equal(t, "text/*", c.NegotiateFormat("text/*"))
}
func TestContextIsAborted(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
assert.False(t, c.IsAborted())