Skip to content

Commit

Permalink
Merge 9c9690c into 96c6b5a
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed Jul 3, 2020
2 parents 96c6b5a + 9c9690c commit e003102
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
6 changes: 5 additions & 1 deletion proxy/balancing.go
Expand Up @@ -65,7 +65,11 @@ func newLoadBalancedMiddleware(lb sd.Balancer) Middleware {
return nil, err
}
if len(r.Query) > 0 {
r.URL.RawQuery += "&" + r.Query.Encode()
if len(r.URL.RawQuery) > 0 {
r.URL.RawQuery += "&" + r.Query.Encode()
} else {
r.URL.RawQuery += r.Query.Encode()
}
}

return next[0](ctx, &r)
Expand Down
70 changes: 34 additions & 36 deletions proxy/balancing_test.go
Expand Up @@ -58,44 +58,42 @@ func TestNewRandomLoadBalancedMiddleware(t *testing.T) {
}

func testLoadBalancedMw(t *testing.T, lb Middleware) {
want := "http://127.0.0.1:8080/tupu"
assertion := func(ctx context.Context, request *Request) (*Response, error) {
if request.URL.String() != want {
t.Errorf("The middleware did not update the request URL! want [%s], have [%s]\n", want, request.URL)
}
return nil, nil
}
if _, err := lb(assertion)(context.Background(), &Request{
Path: "/tupu",
}); err != nil {
t.Errorf("The middleware propagated an unexpected error: %s\n", err.Error())
}

want = "http://127.0.0.1:8080/tupu?extra=true"
assertion = func(ctx context.Context, request *Request) (*Response, error) {
if request.URL.String() != want {
t.Errorf("The middleware did not update the request URL! want [%s], have [%s]\n", want, request.URL)
for _, tc := range []struct {
path string
query url.Values
expected string
}{
{
path: "/tupu",
expected: "http://127.0.0.1:8080/tupu",
},
{
path: "/tupu?extra=true",
expected: "http://127.0.0.1:8080/tupu?extra=true",
},
{
path: "/tupu?extra=true",
query: url.Values{"some": []string{"none"}},
expected: "http://127.0.0.1:8080/tupu?extra=true&some=none",
},
{
path: "/tupu",
query: url.Values{"some": []string{"none"}},
expected: "http://127.0.0.1:8080/tupu?some=none",
},
} {
assertion := func(ctx context.Context, request *Request) (*Response, error) {
if request.URL.String() != tc.expected {
t.Errorf("The middleware did not update the request URL! want [%s], have [%s]\n", tc.expected, request.URL)
}
return nil, nil
}
return nil, nil
}
if _, err := lb(assertion)(context.Background(), &Request{
Path: "/tupu?extra=true",
}); err != nil {
t.Errorf("The middleware propagated an unexpected error: %s\n", err.Error())
}

want = "http://127.0.0.1:8080/tupu?extra=true&some=none"
assertion = func(ctx context.Context, request *Request) (*Response, error) {
if request.URL.String() != want {
t.Errorf("The middleware did not update the request URL! want [%s], have [%s]\n", want, request.URL)
if _, err := lb(assertion)(context.Background(), &Request{
Path: tc.path,
Query: tc.query,
}); err != nil {
t.Errorf("The middleware propagated an unexpected error: %s\n", err.Error())
}
return nil, nil
}
if _, err := lb(assertion)(context.Background(), &Request{
Path: "/tupu?extra=true",
Query: url.Values{"some": []string{"none"}},
}); err != nil {
t.Errorf("The middleware propagated an unexpected error: %s\n", err.Error())
}
}

Expand Down

0 comments on commit e003102

Please sign in to comment.