Skip to content

Commit

Permalink
Add test for request body
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Sep 24, 2024
1 parent fe204eb commit 4da6f4c
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions clients/stellarcore/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package stellarcore

import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"testing"

"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -96,15 +100,32 @@ func TestGetLedgerEntries(t *testing.T) {
},
}

// happy path - fetch an entry
hmock.On("POST", "http://localhost:11626/getledgerentryraw").
ReturnJSON(http.StatusOK, &mockResp)

var key xdr.LedgerKey
acc, err := xdr.AddressToAccountId(keypair.MustRandom().Address())
require.NoError(t, err)
key.SetAccount(acc)

// happy path - fetch an entry
ce := hmock.On("POST", "http://localhost:11626/getledgerentryraw")
hmock.RegisterResponder(
"POST",
"http://localhost:11626/getledgerentryraw",
func(r *http.Request) (*http.Response, error) {
// Ensure the request has the correct POST body
requestData, err := io.ReadAll(r.Body)

Check failure on line 115 in clients/stellarcore/client_test.go

View workflow job for this annotation

GitHub Actions / check (ubuntu-22.04, 1.22.1)

declaration of "err" shadows declaration at line 104
require.NoError(t, err)

keyB64, err := key.MarshalBinaryBase64()
require.NoError(t, err)
expected := fmt.Sprintf("key=%s&ledgerSeq=1234", url.QueryEscape(keyB64))
require.Equal(t, expected, string(requestData))

resp, err := httpmock.NewJsonResponse(http.StatusOK, &mockResp)
require.NoError(t, err)
ce.Return(httpmock.ResponderFromResponse(resp))
return resp, nil
})

resp, err := c.GetLedgerEntryRaw(context.Background(), 1234, key)
require.NoError(t, err)
require.NotNil(t, resp)
Expand Down

0 comments on commit 4da6f4c

Please sign in to comment.