Skip to content

Commit

Permalink
expose GetParams
Browse files Browse the repository at this point in the history
  • Loading branch information
aviau committed Sep 14, 2024
1 parent 4e46faa commit bef8fd5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ func (client *ApiClient) Get(path string) (*http.Response, error) {
return client.do(request)
}

func (client *ApiClient) GetParams(path string, params *url.Values) (*http.Response, error) {
if params != nil {
path = path + "?" + params.Encode()
}
return client.Get(path)
}

func (client *ApiClient) Post(path, contentType string, body io.Reader) (*http.Response, error) {
destUrl, err := url.JoinPath(client.baseUrl, path)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions api_client_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -106,3 +107,25 @@ func TestPost(t *testing.T) {
assert.NoError(t, err, "failed to read response body")
assert.Equal(t, `"ho"`, string(body), "Didn't get expected response")
}

func TestGetParams(t *testing.T) {
ct := newClientTest(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/some-path?some-param=some-value", r.URL.Path, "didn't get the expected path")
}),
)
defer ct.Close()

resp, err := ct.apiClient.GetParams(
"/some-path",
&url.Values{
"some-param": []string{"some-value"},
},
)
assert.NoError(t, err, "failed to make get request")
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
assert.NoError(t, err, "failed to read response body")
assert.Equal(t, []byte{}, body)
}

0 comments on commit bef8fd5

Please sign in to comment.