diff --git a/common/http.go b/common/http.go index 236487d447..74adac0778 100644 --- a/common/http.go +++ b/common/http.go @@ -12,6 +12,7 @@ import ( "net/url" "reflect" "regexp" + "sort" "strings" "github.com/google/go-querystring/query" @@ -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 diff --git a/common/http_test.go b/common/http_test.go index 42d64a0451..c0a04e34a8 100644 --- a/common/http_test.go +++ b/common/http_test.go @@ -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