Handle comma-separated query parameters in API requests (#9102)

* return empty array when no peers found

* add missing query params to endpoint factory

* fix validator endpoints in endpoint factory

* peers test

* Move API param handling to separate file

* Handle comma-separated query parameters
This commit is contained in:
Radosław Kapka
2021-06-28 17:37:14 +02:00
committed by GitHub
parent 940ce0c292
commit 43623e1089
2 changed files with 27 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package gateway
import (
"encoding/base64"
"net/http"
"net/url"
"strings"
"github.com/gorilla/mux"
@@ -54,6 +55,8 @@ segmentsLoop:
func HandleQueryParameters(req *http.Request, params []QueryParam) ErrorJson {
queryParams := req.URL.Query()
normalizeQueryValues(queryParams)
for key, vals := range queryParams {
for _, p := range params {
if key == p.Name {
@@ -93,3 +96,14 @@ func HandleQueryParameters(req *http.Request, params []QueryParam) ErrorJson {
func isRequestParam(s string) bool {
return len(s) > 2 && s[0] == '{' && s[len(s)-1] == '}'
}
func normalizeQueryValues(queryParams url.Values) {
// Replace comma-separated values with individual values.
for key, vals := range queryParams {
splitVals := make([]string, 0)
for _, v := range vals {
splitVals = append(splitVals, strings.Split(v, ",")...)
}
queryParams[key] = splitVals
}
}

View File

@@ -109,3 +109,16 @@ func TestIsRequestParam(t *testing.T) {
assert.Equal(t, tt.b, b)
}
}
func TestNormalizeQueryValues(t *testing.T) {
input := make(map[string][]string)
input["key"] = []string{"value1", "value2,value3,value4", "value5"}
normalizeQueryValues(input)
require.Equal(t, 5, len(input["key"]))
assert.Equal(t, "value1", input["key"][0])
assert.Equal(t, "value2", input["key"][1])
assert.Equal(t, "value3", input["key"][2])
assert.Equal(t, "value4", input["key"][3])
assert.Equal(t, "value5", input["key"][4])
}