Merge pull request #606 from ReneWerner87/fix_append_bug

🐞 prevent duplicates in ctx.Append for similar wording of the value
pull/601/head
fenny 2020-07-14 08:02:12 -04:00 committed by GitHub
commit 155e6a0673
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

4
ctx.go
View File

@ -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
}
}

View File

@ -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")))
}

View File

@ -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)))