From e8443f44f91d1183f377db36da424c3b99b7f3c4 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 18 Aug 2020 14:51:32 +0200 Subject: [PATCH] Add QueryClient tests (WebSocket and Http) --- .../stargate/src/queries/queryclient.spec.ts | 80 +++++++++++++++++++ packages/stargate/src/testutils.spec.ts | 2 + 2 files changed, 82 insertions(+) create mode 100644 packages/stargate/src/queries/queryclient.spec.ts diff --git a/packages/stargate/src/queries/queryclient.spec.ts b/packages/stargate/src/queries/queryclient.spec.ts new file mode 100644 index 00000000..3b453bcd --- /dev/null +++ b/packages/stargate/src/queries/queryclient.spec.ts @@ -0,0 +1,80 @@ +import { toAscii } from "@cosmjs/encoding"; +import { Client as TendermintClient } from "@cosmjs/tendermint-rpc"; + +import { cosmos } from "../codec"; +import { nonNegativeIntegerMatcher, pendingWithoutSimapp, simapp, unused } from "../testutils.spec"; +import { QueryClient } from "./queryclient"; +import { toAccAddress } from "./utils"; + +async function makeClient(rpcUrl: string): Promise<[QueryClient, TendermintClient]> { + const tmClient = await TendermintClient.connect(rpcUrl); + return [QueryClient.withExtensions(tmClient), tmClient]; +} + +describe("QueryClient", () => { + describe("queryVerified", () => { + it("works via WebSockets", async () => { + pendingWithoutSimapp(); + const [client, tmClient] = await makeClient(simapp.tendermintUrlWs); + + const key = Uint8Array.from([ + ...toAscii("balances"), + ...toAccAddress(unused.address), + ...toAscii(simapp.denomFee), + ]); + const data = await client.queryVerified("bank", key); + const response = cosmos.Coin.decode(data); + expect(response.amount).toMatch(nonNegativeIntegerMatcher); + expect(response.denom).toEqual(simapp.denomFee); + + tmClient.disconnect(); + }); + + it("works via http", async () => { + pendingWithoutSimapp(); + const [client, tmClient] = await makeClient(simapp.tendermintUrlHttp); + + const key = Uint8Array.from([ + ...toAscii("balances"), + ...toAccAddress(unused.address), + ...toAscii(simapp.denomFee), + ]); + const data = await client.queryVerified("bank", key); + const response = cosmos.Coin.decode(data); + expect(response.amount).toMatch(nonNegativeIntegerMatcher); + expect(response.denom).toEqual(simapp.denomFee); + + tmClient.disconnect(); + }); + }); + + describe("queryUnverified", () => { + it("works via WebSockets", async () => { + pendingWithoutSimapp(); + const [client, tmClient] = await makeClient(simapp.tendermintUrlWs); + + const requestData = Uint8Array.from( + cosmos.bank.QueryAllBalancesRequest.encode({ address: toAccAddress(unused.address) }).finish(), + ); + const data = await client.queryUnverified(`/cosmos.bank.Query/AllBalances`, requestData); + const response = cosmos.bank.QueryAllBalancesResponse.decode(data); + expect(response.balances.length).toEqual(2); + + tmClient.disconnect(); + }); + + it("works via http", async () => { + pendingWithoutSimapp(); + const [client, tmClient] = await makeClient(simapp.tendermintUrlHttp); + + const requestData = Uint8Array.from( + cosmos.bank.QueryAllBalancesRequest.encode({ address: toAccAddress(unused.address) }).finish(), + ); + const data = await client.queryUnverified(`/cosmos.bank.Query/AllBalances`, requestData); + const response = cosmos.bank.QueryAllBalancesResponse.decode(data); + expect(response.balances.length).toEqual(2); + + tmClient.disconnect(); + }); + }); +}); diff --git a/packages/stargate/src/testutils.spec.ts b/packages/stargate/src/testutils.spec.ts index 4bfbfafa..fc106336 100644 --- a/packages/stargate/src/testutils.spec.ts +++ b/packages/stargate/src/testutils.spec.ts @@ -21,6 +21,8 @@ export function makeRandomAddress(): string { export const simapp = { tendermintUrl: "localhost:26657", + tendermintUrlWs: "ws://localhost:26657", + tendermintUrlHttp: "http://localhost:26657", chainId: "simd-testing", denomStaking: "ustake", denomFee: "ucosm",