Skip to content

Commit

Permalink
add grpc request with header method
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilkumarpilli committed Sep 25, 2024
1 parent 2ce8237 commit d602eed
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
27 changes: 8 additions & 19 deletions tests/systemtests/bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"

"github.com/cosmos/cosmos-sdk/testutil"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

Expand Down Expand Up @@ -226,56 +225,46 @@ func TestBankGRPCQueries(t *testing.T) {
supplyUrl := baseurl + "/cosmos/bank/v1beta1/supply"

// as supply might change for each block, can't set complete expected output
expTotalSupplyOutput := `{"supply":[{"denom":"newdenom","amount":"10000000"},{"denom":"stake","amount"`
specificDenomOutput := fmt.Sprintf(`{"denom":"%s","amount":"%s"}`, newDenom, initialAmount)
// below output depends on block height and voting power
expTotalSupplyOutput := fmt.Sprintf(`{"supply":[%s,{"denom":"stake","amount":"2010000191"},{"denom":"testtoken","amount":"4000000000"}],"pagination":{"next_key":null,"total":"3"}}`, specificDenomOutput)
bogusDenomOutput := `{"denom":"foobar","amount":"0"}`

blockHeightHeader := "x-cosmos-block-height"
blockHeight := sut.CurrentHeight()

supplyTestCases := []struct {
name string
url string
headers map[string]string
expOut string
}{
supplyTestCases := []GRPCTestCaseWithHeaders{
{
"test GRPC total supply",
supplyUrl,
map[string]string{
blockHeightHeader: fmt.Sprintf("%d", blockHeight),
blockHeightHeader: "1",
},
expTotalSupplyOutput,
},
{
"test GRPC total supply of a specific denom",
supplyUrl + "/by_denom?denom=" + newDenom,
map[string]string{},
specificDenomOutput,
fmt.Sprintf(`{"amount":%s}`, specificDenomOutput),
},
{
"error when querying supply with height greater than block height",
supplyUrl,
map[string]string{
blockHeightHeader: fmt.Sprintf("%d", blockHeight+5),
},
"invalid height",
`{"code":2, "message":"codespace sdk code 26: invalid height: cannot query with height in the future; please provide a valid height", "details":[]}`,
},
{
"test GRPC total supply of a bogus denom",
supplyUrl + "/by_denom?denom=foobar",
map[string]string{},
bogusDenomOutput,
fmt.Sprintf(`{"amount":%s}`, bogusDenomOutput),
},
}

for _, tc := range supplyTestCases {
t.Run(tc.name, func(t *testing.T) {
resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
require.NoError(t, err)
require.Contains(t, string(resp), tc.expOut)
})
}
RunGRPCQueriesWithHeaders(t, supplyTestCases)

// test denom metadata endpoint
denomMetadataUrl := baseurl + "/cosmos/bank/v1beta1/denoms_metadata"
Expand Down
21 changes: 21 additions & 0 deletions tests/systemtests/rest_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,24 @@ func RunGRPCQueries(t *testing.T, testCases []GRPCTestCase) {
})
}
}

type GRPCTestCaseWithHeaders struct {
name string
url string
headers map[string]string
expOut string
}

// RunGRPCQueriesWithHeaders runs given grpc testcases by making requests with headers and
// checking response with expected output
func RunGRPCQueriesWithHeaders(t *testing.T, testCases []GRPCTestCaseWithHeaders) {
t.Helper()

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resp, err := testutil.GetRequestWithHeaders(tc.url, tc.headers)
require.NoError(t, err)
require.JSONEq(t, tc.expOut, string(resp))
})
}
}

0 comments on commit d602eed

Please sign in to comment.