Skip to content

Commit

Permalink
Merge pull request #412 from devopsfaith/array_params_seq_proxy
Browse files Browse the repository at this point in the history
better formatting of arrays when used as req params
  • Loading branch information
kpacha committed Sep 30, 2020
2 parents a1040a6 + 23fe3e0 commit 7c9d4ac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 11 additions & 0 deletions proxy/merging.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ func sequentialMerge(patterns []string, timeout time.Duration, rc ResponseCombin
continue
}
switch clean := v.(type) {
case []interface{}:
if len(clean) == 0 {
request.Params[key] = ""
continue
}
var b strings.Builder
for i := 0; i < len(clean)-1; i++ {
fmt.Fprintf(&b, "%v,", clean[i])
}
fmt.Fprintf(&b, "%v", clean[len(clean)-1])
request.Params[key] = b.String()
case string:
request.Params[key] = clean
case int:
Expand Down
8 changes: 7 additions & 1 deletion proxy/merging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestNewMergeDataMiddleware_sequential(t *testing.T) {
endpoint := config.EndpointConfig{
Backend: []*config.Backend{
{URLPattern: "/"},
{URLPattern: "/aaa/{{.Resp0_array}}"},
{URLPattern: "/aaa/{{.Resp0_int}}/{{.Resp0_string}}/{{.Resp0_bool}}/{{.Resp0_float}}/{{.Resp0_struct.foo}}"},
{URLPattern: "/aaa/{{.Resp0_int}}/{{.Resp0_string}}/{{.Resp0_bool}}/{{.Resp0_float}}/{{.Resp0_struct.foo}}?x={{.Resp1_tupu}}"},
{URLPattern: "/aaa/{{.Resp0_struct.foo}}/{{.Resp0_struct.struct.foo}}/{{.Resp0_struct.struct.struct.foo}}"},
Expand Down Expand Up @@ -74,7 +75,12 @@ func TestNewMergeDataMiddleware_sequential(t *testing.T) {
},
},
},
"array": []interface{}{"1", "2"},
}, IsComplete: true}),
func(ctx context.Context, r *Request) (*Response, error) {
checkRequestParam(t, r, "Resp0_array", "1,2")
return &Response{Data: map[string]interface{}{"tupu": "foo"}, IsComplete: true}, nil
},
func(ctx context.Context, r *Request) (*Response, error) {
checkRequestParam(t, r, "Resp0_int", "42")
checkRequestParam(t, r, "Resp0_string", "some")
Expand Down Expand Up @@ -112,7 +118,7 @@ func TestNewMergeDataMiddleware_sequential(t *testing.T) {
case <-mustEnd:
t.Errorf("We were expecting a response but we got none\n")
default:
if len(out.Data) != 8 {
if len(out.Data) != 9 {
t.Errorf("We weren't expecting a partial response but we got %v!\n", out)
}
if !out.IsComplete {
Expand Down

0 comments on commit 7c9d4ac

Please sign in to comment.