mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
Fix NegotiateFormat
This commit is contained in:
parent
b57163a0e4
commit
b593c6ea2a
@ -1131,7 +1131,8 @@ func (c *Context) NegotiateFormat(offered ...string) string {
|
|||||||
// According to RFC 2616 and RFC 2396, non-ASCII characters are not allowed in headers,
|
// 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
|
// therefore we can just iterate over the string without casting it into []rune
|
||||||
i := 0
|
i := 0
|
||||||
for ; i < len(accepted); i++ {
|
minLen := min(len(accepted), len(offer))
|
||||||
|
for ; i < minLen; i++ {
|
||||||
if accepted[i] == '*' || offer[i] == '*' {
|
if accepted[i] == '*' || offer[i] == '*' {
|
||||||
return offer
|
return offer
|
||||||
}
|
}
|
||||||
|
@ -1311,6 +1311,15 @@ func TestContextNegotiationFormatCustom(t *testing.T) {
|
|||||||
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON))
|
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) {
|
func TestContextIsAborted(t *testing.T) {
|
||||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
assert.False(t, c.IsAborted())
|
assert.False(t, c.IsAborted())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user