Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cli/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ func MakeRequest(req *http.Request, options ...requestOption) (*http.Response, e
return resp, nil
}


// isRetryable returns true if a request should be retried.
func isRetryable(code int) bool {
if code == /* 408 */ http.StatusRequestTimeout ||
Expand Down Expand Up @@ -581,6 +580,11 @@ func MakeRequestAndFormat(req *http.Request) {
}
panic(err)
}
transformed, err := transformResponseForCommand(currentCommand, parsed.Body)
if err != nil {
panic(err)
}
parsed.Body = transformed

if err := Formatter.Format(parsed); err != nil {
if e, ok := err.(shorthand.Error); ok {
Expand Down
59 changes: 59 additions & 0 deletions cli/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ import (

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/h2non/gock.v1"
)

type captureFormatter struct {
resp Response
}

func (f *captureFormatter) Format(resp Response) error {
f.resp = resp
return nil
}

func TestFixAddress(t *testing.T) {
reset(false)
assert.Equal(t, "https://example.com", fixAddress("example.com"))
Expand Down Expand Up @@ -61,6 +71,55 @@ func TestRequestPagination(t *testing.T) {
assert.Equal(t, []any{1.0, 2.0, 3.0, 4.0, 5.0, 6.0}, resp.Body)
}

func TestMakeRequestAndFormatTransformsOnchainTxResponse(t *testing.T) {
defer gock.Off()

reset(false)

oldFormatter := Formatter
oldCommand := currentCommand
defer func() {
Formatter = oldFormatter
currentCommand = oldCommand
}()

capture := &captureFormatter{}
Formatter = capture
currentCommand = "onchain-tx"
viper.Set("rsh-agent-view", "")
viper.Set("rsh-shape", false)

gock.New("http://example.com").
Get("/tx").
Reply(http.StatusOK).
JSON(map[string]any{
"data": []any{
map[string]any{
"hash": "0xabc",
"blockNumber": "0x157f411",
"value": "0xde0b6b3a7640000",
},
},
})

req, _ := http.NewRequest(http.MethodGet, "http://example.com/tx", nil)
MakeRequestAndFormat(req)

require.True(t, gock.IsDone())
body, ok := capture.resp.Body.(map[string]any)
require.True(t, ok)
data, ok := body["data"].([]any)
require.True(t, ok)
require.Len(t, data, 1)
tx, ok := data[0].(map[string]any)
require.True(t, ok)

assert.Equal(t, "0x157f411", tx["blockNumber"])
assert.Equal(t, "22541329", tx["blockNumberDecimal"])
assert.Equal(t, "1000000000000000000", tx["valueDecimal"])
assert.Equal(t, "1", tx["valueNativeDecimal"])
}

func TestGetStatus(t *testing.T) {
defer gock.Off()

Expand Down
Loading
Loading