Skip to content

Commit

Permalink
Upgrade test to use JSON response:
Browse files Browse the repository at this point in the history
It'd be nice if we had POST param unit tests but we don't :(
  • Loading branch information
Shaptic committed Aug 19, 2024
1 parent e94e43e commit 264f017
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
14 changes: 9 additions & 5 deletions clients/stellarcore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
Expand All @@ -22,7 +21,7 @@ import (
// Client represents a client that is capable of communicating with a
// stellar-core server using HTTP
type Client struct {
// HTTP is the client to use when communicating with stellar-core. If nil,
// HTTP is the client to use when communicating with stellar-core. If nil,
// http.DefaultClient will be used.
HTTP HTTP

Expand All @@ -37,7 +36,7 @@ type Client struct {
// in case an error was encountered during either the draining or closing of the
// stream, that error would be returned.
func drainReponse(hresp *http.Response, close bool, err *error) (outerror error) {
_, err2 := io.Copy(ioutil.Discard, hresp.Body)
_, err2 := io.Copy(io.Discard, hresp.Body)
if err2 != nil {
if err != nil && *err == nil {
*err = errors.Wrap(err2, "unable to read excess data from response")
Expand Down Expand Up @@ -320,7 +319,7 @@ func (c *Client) rawPost(

// makeLedgerKeyRequest is a generic method to perform a request in the form
// `key=...&key=...&ledgerSeq=...` which is useful because three Stellar Core
// endpoints all use this request format. Be sure to pass `target by reference.
// endpoints all use this request format. Be sure to pass `target` by reference.
func (c *Client) makeLedgerKeyRequest(
ctx context.Context,
target interface{},
Expand Down Expand Up @@ -372,10 +371,15 @@ func (c *Client) getResponse(req *http.Request) (*http.Response, error) {
func buildMultiKeyRequest(keys ...xdr.LedgerKey) (string, error) {
// The average ledger key length, according to a simple
//
// SELECT AVG(LENGTH(HEX(key))) / 2 FROM ledger_entries;
// SELECT AVG(LENGTH(HEX(key))) / 2 FROM ledger_entries;
//
// on a pubnet RPC instance is ~57.6. We can use this to preallocate a
// string buffer for performance.
//
// We know that these endpoints will almost exclusively be used for
// ContractData and the like, so we could optimize the buffer further for
// that, but that data is harder to query since it'd involve parsing the XDR
// from the DB to check the key type.
q := strings.Builder{}
q.Grow(50 * len(keys))

Expand Down
24 changes: 19 additions & 5 deletions clients/stellarcore/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,24 @@ func TestGetLedgerEntries(t *testing.T) {
hmock := httptest.NewClient()
c := &Client{HTTP: hmock, URL: "http://localhost:11626"}

// build a fake response body
mockResp := proto.GetLedgerEntriesResponse{
Ledger: 1215, // checkpoint align on expected request
Entries: []proto.LedgerEntryResponse{
{
Entry: "pretend this is XDR lol",
State: proto.DeadState,
},
{
Entry: "pretend this is another XDR lol",
State: proto.ArchivedStateNoProof,
},
},
}

// happy path - fetch an entry
hmock.On("POST", "http://localhost:11626/getledgerentry").
ReturnString(http.StatusOK,
`{"ledger": 1234, "entries": [ {"e": "pretend it's xdr lol", "state": "dead" }]}`,
)
ReturnJSON(http.StatusOK, &mockResp)

var key xdr.LedgerKey
acc, err := xdr.AddressToAccountId(keypair.MustRandom().Address())
Expand All @@ -98,7 +111,8 @@ func TestGetLedgerEntries(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, resp)

require.EqualValues(t, 1234, resp.Ledger)
require.Len(t, resp.Entries, 1)
require.EqualValues(t, 1215, resp.Ledger)
require.Len(t, resp.Entries, 2)
require.Equal(t, resp.Entries[0].State, proto.DeadState)
require.Equal(t, resp.Entries[1].State, proto.ArchivedStateNoProof)
}

0 comments on commit 264f017

Please sign in to comment.