Skip to content

Commit

Permalink
Add test for getRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
amishas157 committed Nov 15, 2024
1 parent 483f28f commit 7fc2c62
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
28 changes: 22 additions & 6 deletions utils/apiclient/client.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
package apiclient

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"

"github.com/pkg/errors"
)

func (c *APIClient) getRequest(endpoint string, queryParams url.Values) error {
func (c *APIClient) getRequest(endpoint string, queryParams url.Values) (interface{}, error) {

Check failure on line 13 in utils/apiclient/client.go

View workflow job for this annotation

GitHub Actions / check (ubuntu-22.04, 1.22.1)

func (*APIClient).getRequest is unused (U1000)
client := c.HTTP
if client == nil {
client = &http.Client{}
}

fullURL := c.url(endpoint, queryParams)
req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
return errors.Wrap(err, "http GET request creation failed")
return nil, errors.Wrap(err, "http GET request creation failed")
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return errors.Wrap(err, "http GET request failed")
return nil, errors.Wrap(err, "http GET request failed")
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("API request failed with status %d", resp.StatusCode)
return nil, fmt.Errorf("API request failed with status %d", resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}

var result interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON: %w", err)
}

return nil
return result, nil
}

func (c *APIClient) url(endpoint string, qstr url.Values) string {

Check failure on line 48 in utils/apiclient/client.go

View workflow job for this annotation

GitHub Actions / check (ubuntu-22.04, 1.22.1)

func (*APIClient).url is unused (U1000)
Expand Down
28 changes: 28 additions & 0 deletions utils/apiclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/url"
"testing"

"github.com/stellar/go/support/http/httptest"
"github.com/stretchr/testify/assert"
)

Expand All @@ -20,3 +21,30 @@ func Test_url(t *testing.T) {
furl := c.url("federation", qstr)
assert.Equal(t, "https://stellar.org/federation?acct=2382376&federation_type=bank_account&swift=BOPBPHMM&type=forward", furl)
}

func Test_getRequest(t *testing.T) {
friendbotFundResponse := `{"key": "value"}`

hmock := httptest.NewClient()
c := &APIClient{
BaseURL: "https://stellar.org",
HTTP: hmock,
}
hmock.On(
"GET",
"https://stellar.org/federation?acct=2382376&federation_type=bank_account&swift=BOPBPHMM&type=forward",
).ReturnString(200, friendbotFundResponse)
qstr := url.Values{}

qstr.Add("type", "forward")
qstr.Add("federation_type", "bank_account")
qstr.Add("swift", "BOPBPHMM")
qstr.Add("acct", "2382376")

result, err := c.getRequest("federation", qstr)
if err != nil {
t.Fatal(err)
}
expected := map[string]interface{}{"key": "value"}
assert.Equal(t, expected, result)
}
12 changes: 12 additions & 0 deletions utils/apiclient/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package apiclient

import (
"net/http"
"net/url"
)

type HTTP interface {
Do(req *http.Request) (resp *http.Response, err error)
Get(url string) (resp *http.Response, err error)
PostForm(url string, data url.Values) (resp *http.Response, err error)
}

type APIClient struct {
BaseURL string
AuthToken string
HTTP HTTP
}

0 comments on commit 7fc2c62

Please sign in to comment.