Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐞 prevent duplicates in ctx.Append for similar wording of the value #606

Merged
merged 2 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
22 changes: 22 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
}

Expand Down
1 change: 1 addition & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down