mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
Added Args.PeekMulti for obtaining multiple query arg values for the given key
This commit is contained in:
@@ -156,6 +156,22 @@ func (a *Args) PeekBytes(key []byte) []byte {
|
||||
return peekArgBytes(a.args, key)
|
||||
}
|
||||
|
||||
// PeekMulti returns all the arg values for the given key.
|
||||
func (a *Args) PeekMulti(key string) [][]byte {
|
||||
var values [][]byte
|
||||
a.VisitAll(func(k, v []byte) {
|
||||
if string(k) == key {
|
||||
values = append(values, v)
|
||||
}
|
||||
})
|
||||
return values
|
||||
}
|
||||
|
||||
// PeekMultiBytes returns all the arg values for the given key.
|
||||
func (a *Args) PeekMultiBytes(key []byte) [][]byte {
|
||||
return a.PeekMulti(unsafeBytesToStr(key))
|
||||
}
|
||||
|
||||
// Has returns true if the given key exists in Args.
|
||||
func (a *Args) Has(key string) bool {
|
||||
a.bufKV.key = append(a.bufKV.key[:0], key...)
|
||||
|
||||
@@ -2,10 +2,37 @@ package fasthttp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPeekMulti(t *testing.T) {
|
||||
var a Args
|
||||
a.Parse("foo=123&bar=121&foo=321&foo=&barz=sdf")
|
||||
|
||||
vv := a.PeekMulti("foo")
|
||||
expectedVV := [][]byte{
|
||||
[]byte("123"),
|
||||
[]byte("321"),
|
||||
[]byte(nil),
|
||||
}
|
||||
if !reflect.DeepEqual(vv, expectedVV) {
|
||||
t.Fatalf("unexpected vv\n%#v\nExpecting\n%#v\n", vv, expectedVV)
|
||||
}
|
||||
|
||||
vv = a.PeekMulti("aaaa")
|
||||
if len(vv) > 0 {
|
||||
t.Fatalf("expecting empty result for non-existing key. Got %#v", vv)
|
||||
}
|
||||
|
||||
vv = a.PeekMulti("bar")
|
||||
expectedVV = [][]byte{[]byte("121")}
|
||||
if !reflect.DeepEqual(vv, expectedVV) {
|
||||
t.Fatalf("unexpected vv\n%#v\nExpecting\n%#v\n", vv, expectedVV)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArgsEscape(t *testing.T) {
|
||||
testArgsEscape(t, "foo", "bar", "foo=bar")
|
||||
testArgsEscape(t, "f.o,1:2/4", "~`!@#$%^&*()_-=+\\|/[]{};:'\"<>,./?",
|
||||
|
||||
Reference in New Issue
Block a user