Skip to content

Commit

Permalink
fix(client): pass query string correctly when using Tsuru proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Oct 28, 2021
1 parent e4e6abf commit 5197ecb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/rpaas/client/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (c *client) DeleteCertManager(ctx context.Context, instance, issuer string)
data.Set("issuer", issuer)
}

req, err := c.newRequest("DELETE", fmt.Sprintf("/resources/%s/cert-manager?%s", instance, data.Encode()), nil, instance)
req, err := c.newRequestWithQueryString("DELETE", fmt.Sprintf("/resources/%s/cert-manager", instance), nil, instance, data)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/rpaas/client/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestClientThroughTsuru_DeleteCertificate(t *testing.T) {
}
}

func TestClientThroughTsuru_DeleteUpdateCertManager(t *testing.T) {
func TestClientThroughTsuru_UpdateCertManager(t *testing.T) {
tests := map[string]struct {
args UpdateCertManagerArgs
expectedError string
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestClientThroughTsuru_DeleteCertManager(t *testing.T) {
"when removing a Cert Manager request with no issuer provided": {
instance: "my-instance",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/cert-manager?"), r.URL.RequestURI())
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/cert-manager"), r.URL.RequestURI())
assert.Equal(t, "DELETE", r.Method)
w.WriteHeader(http.StatusOK)
},
Expand All @@ -258,7 +258,7 @@ func TestClientThroughTsuru_DeleteCertManager(t *testing.T) {
instance: "my-instance",
issuer: "lets-encrypt",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/cert-manager?issuer=lets-encrypt"), r.URL.RequestURI())
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/cert-manager&issuer=lets-encrypt"), r.URL.RequestURI())
assert.Equal(t, "DELETE", r.Method)
w.WriteHeader(http.StatusOK)
},
Expand Down
25 changes: 24 additions & 1 deletion pkg/rpaas/client/internal_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"time"

Expand Down Expand Up @@ -188,7 +189,11 @@ func (c *client) SetService(service string) (Client, error) {
}

func (c *client) newRequest(method, pathName string, body io.Reader, instance string) (*http.Request, error) {
url := c.formatURL(pathName, instance)
return c.newRequestWithQueryString(method, pathName, body, instance, nil)
}

func (c *client) newRequestWithQueryString(method, pathName string, body io.Reader, instance string, qs url.Values) (*http.Request, error) {
url := c.formatURLWithQueryString(pathName, instance, qs)
request, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
Expand Down Expand Up @@ -225,6 +230,24 @@ func (c *client) formatURL(pathName, instance string) string {
return fmt.Sprintf("%s/services/%s/proxy/%s?callback=%s", c.tsuruTarget, c.tsuruService, instance, pathName)
}

func (c *client) formatURLWithQueryString(pathName, instance string, qs url.Values) string {
qsData := qs.Encode()

if !c.throughTsuru {
if qsData != "" {
qsData = "?" + qsData
}

return fmt.Sprintf("%s%s%s", c.rpaasAddress, pathName, qsData)
}

if qsData != "" {
qsData = "&" + qsData
}

return fmt.Sprintf("%s/services/%s/proxy/%s?callback=%s%s", c.tsuruTarget, c.tsuruService, instance, pathName, qsData)
}

func unmarshalBody(resp *http.Response, dst interface{}) error {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down

0 comments on commit 5197ecb

Please sign in to comment.