Skip to content

Commit

Permalink
Sort map keys for QS args for GET requests (#1157)
Browse files Browse the repository at this point in the history
* Sort map keys for QS args for GET requests
* make coverage hit
  • Loading branch information
nfx authored Mar 7, 2022
1 parent ba6e949 commit e2203b1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion common/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/url"
"reflect"
"regexp"
"sort"
"strings"

"github.com/google/go-querystring/query"
Expand Down Expand Up @@ -523,7 +524,12 @@ func makeRequestBody(method string, requestURL *string, data interface{}, marsha
switch inputType.Kind() {
case reflect.Map:
s := []string{}
for _, k := range inputVal.MapKeys() {
keys := inputVal.MapKeys()
// sort map keys by their string repr, so that tests can be deterministic
sort.Slice(keys, func(i, j int) bool {
return keys[i].String() < keys[j].String()
})
for _, k := range keys {
v := inputVal.MapIndex(k)
if v.IsZero() {
continue
Expand Down
16 changes: 16 additions & 0 deletions common/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,22 @@ func TestMakeRequestBody(t *testing.T) {
assert.Equal(t, []byte("abc"), body)
}

func TestMakeRequestBodyForMap(t *testing.T) {
requestURL := "/a"
_, err := makeRequestBody("GET", &requestURL, map[string]int{
// i hope this will not trigger false positives too often
"e": 1,
"a": 2,
"f": 3,
"g": 4,
"c": 5,
"b": 6,
"d": 7,
}, true)
require.NoError(t, err)
assert.Equal(t, "/a?a=2&b=6&c=5&d=7&e=1&f=3&g=4", requestURL)
}

func TestClient_HandleErrors(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit e2203b1

Please sign in to comment.