Skip to content

Commit 87f3f0c

Browse files
authored
🐛 bug: fix client iterators when using break statement (#3357)
* 🐛 bug: fix client iterators when using break statement * fix linter
1 parent 395c8fa commit 87f3f0c

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

client/request.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,12 @@ func (r *Request) Cookie(key string) string {
298298
// Use maps.Collect() to gather them into a map if needed.
299299
func (r *Request) Cookies() iter.Seq2[string, string] {
300300
return func(yield func(string, string) bool) {
301-
r.cookies.VisitAll(func(key, val string) {
302-
if !yield(key, val) {
301+
for k, v := range *r.cookies {
302+
res := yield(k, v)
303+
if !res {
303304
return
304305
}
305-
})
306+
}
306307
}
307308
}
308309

@@ -343,11 +344,11 @@ func (r *Request) PathParam(key string) string {
343344
// Use maps.Collect() to gather them into a map if needed.
344345
func (r *Request) PathParams() iter.Seq2[string, string] {
345346
return func(yield func(string, string) bool) {
346-
r.path.VisitAll(func(key, val string) {
347-
if !yield(key, val) {
347+
for k, v := range *r.path {
348+
if !yield(k, v) {
348349
return
349350
}
350-
})
351+
}
351352
}
352353
}
353354

client/request_test.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//nolint:goconst // Much easier to just ignore memory leaks in tests
12
package client
23

34
import (
@@ -451,6 +452,14 @@ func Test_Request_Cookies(t *testing.T) {
451452
require.Equal(t, "bar", cookies["foo"])
452453
require.Equal(t, "foo", cookies["bar"])
453454

455+
require.NotPanics(t, func() {
456+
for _, v := range req.Cookies() {
457+
if v == "bar" {
458+
break
459+
}
460+
}
461+
})
462+
454463
require.Len(t, cookies, 2)
455464
}
456465

@@ -564,6 +573,14 @@ func Test_Request_PathParams(t *testing.T) {
564573
require.Equal(t, "foo", pathParams["bar"])
565574

566575
require.Len(t, pathParams, 2)
576+
577+
require.NotPanics(t, func() {
578+
for _, v := range req.PathParams() {
579+
if v == "bar" {
580+
break
581+
}
582+
}
583+
})
567584
}
568585

569586
func Benchmark_Request_PathParams(b *testing.B) {
@@ -1579,7 +1596,7 @@ func Test_SetValWithStruct(t *testing.T) {
15791596

15801597
require.True(t, func() bool {
15811598
for _, v := range p.PeekMulti("TSlice") {
1582-
if string(v) == "bar" { //nolint:goconst // test
1599+
if string(v) == "bar" {
15831600
return true
15841601
}
15851602
}

0 commit comments

Comments
 (0)