diff --git a/context.go b/context.go index 788b00bb..0e7cb0fa 100644 --- a/context.go +++ b/context.go @@ -1474,7 +1474,7 @@ func (c *Context) NegotiateFormat(offered ...string) string { break } } - if i == len(accepted) { + if i == len(accepted) && i == len(offer) { return offer } } diff --git a/context_test.go b/context_test.go index cef488c9..7fd2ba87 100644 --- a/context_test.go +++ b/context_test.go @@ -4031,3 +4031,23 @@ func BenchmarkGetMapFromFormData(b *testing.B) { }) } } + +func TestContextNegotiateFormatDoesNotMatchPrefixes(t *testing.T) { + for _, tc := range []struct { + accepted string + offered string + want string + }{ + {"application/json", "application/json-seq", ""}, + {"application/json-seq", "application/json", ""}, + {"text/plain", "text/plaintext", ""}, + {"application/json", "application/json", "application/json"}, + {"application/*", "application/json-seq", "application/json-seq"}, + } { + t.Run(tc.accepted+"/"+tc.offered, func(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.SetAccepted(tc.accepted) + assert.Equal(t, tc.want, c.NegotiateFormat(tc.offered)) + }) + } +}