diff --git a/ctx.go b/ctx.go index f68eda34b2..fec7badaf0 100644 --- a/ctx.go +++ b/ctx.go @@ -181,8 +181,8 @@ func (ctx *Ctx) Append(field string, values ...string) { for _, value := range values { if len(h) == 0 { h = value - } else if h != value && !strings.HasSuffix(h, " "+value) && - !strings.Contains(h, value+",") { + } else if h != value && !strings.HasPrefix(h, value+",") && !strings.HasSuffix(h, " "+value) && + !strings.Contains(h, " "+value+",") { h += ", " + value } } diff --git a/ctx_test.go b/ctx_test.go index 7507211405..a71f590666 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -172,8 +172,30 @@ func Test_Ctx_Append(t *testing.T) { ctx.Append("X-Test", "Hello") ctx.Append("X-Test", "World") ctx.Append("X-Test", "Hello", "World") + // similar value in the middle + ctx.Append("X2-Test", "World") + ctx.Append("X2-Test", "XHello") + ctx.Append("X2-Test", "Hello", "World") + // similar value at the start + ctx.Append("X3-Test", "XHello") + ctx.Append("X3-Test", "World") + ctx.Append("X3-Test", "Hello", "World") + // try it with multiple similar values + ctx.Append("X4-Test", "XHello") + ctx.Append("X4-Test", "Hello") + ctx.Append("X4-Test", "HelloZ") + ctx.Append("X4-Test", "YHello") + ctx.Append("X4-Test", "Hello") + ctx.Append("X4-Test", "YHello") + ctx.Append("X4-Test", "HelloZ") + ctx.Append("X4-Test", "XHello") + // without append value ctx.Append("X-Custom-Header") + utils.AssertEqual(t, "Hello, World", string(ctx.Fasthttp.Response.Header.Peek("X-Test"))) + utils.AssertEqual(t, "World, XHello, Hello", string(ctx.Fasthttp.Response.Header.Peek("X2-Test"))) + utils.AssertEqual(t, "XHello, World, Hello", string(ctx.Fasthttp.Response.Header.Peek("X3-Test"))) + utils.AssertEqual(t, "XHello, Hello, HelloZ, YHello", string(ctx.Fasthttp.Response.Header.Peek("X4-Test"))) utils.AssertEqual(t, "", string(ctx.Fasthttp.Response.Header.Peek("x-custom-header"))) } diff --git a/utils.go b/utils.go index 0cbe54be54..797041ab98 100644 --- a/utils.go +++ b/utils.go @@ -17,6 +17,7 @@ import ( fasthttp "github.com/valyala/fasthttp" ) +// quoteString escape special characters in a given string func quoteString(raw string) string { bb := bytebufferpool.Get() quoted := string(fasthttp.AppendQuotedArg(bb.B, getBytes(raw)))