mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
Write tests for http function
This commit is contained in:
parent
aee0714133
commit
11ec6f1393
@ -1,6 +1,6 @@
|
||||
import { createJsonRpcRequest } from "../jsonrpc";
|
||||
import { defaultInstance } from "../testutil.spec";
|
||||
import { HttpClient } from "./httpclient";
|
||||
import { http, HttpClient } from "./httpclient";
|
||||
|
||||
function pendingWithoutTendermint(): void {
|
||||
if (!process.env.TENDERMINT_ENABLED) {
|
||||
@ -8,9 +8,23 @@ function pendingWithoutTendermint(): void {
|
||||
}
|
||||
}
|
||||
|
||||
describe("HttpClient", () => {
|
||||
const tendermintUrl = defaultInstance.url;
|
||||
const tendermintUrl = defaultInstance.url;
|
||||
|
||||
describe("http", () => {
|
||||
it("can send a health request", async () => {
|
||||
pendingWithoutTendermint();
|
||||
const response = await http("POST", `http://${tendermintUrl}`, createJsonRpcRequest("health"));
|
||||
expect(response).toEqual(jasmine.objectContaining({ jsonrpc: "2.0" }));
|
||||
});
|
||||
|
||||
it("errors for non-open port", async () => {
|
||||
await expectAsync(
|
||||
http("POST", `http://localhost:56745`, createJsonRpcRequest("health")),
|
||||
).toBeRejectedWithError(/(ECONNREFUSED|Failed to fetch)/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe("HttpClient", () => {
|
||||
it("can make a simple call", async () => {
|
||||
pendingWithoutTendermint();
|
||||
const client = new HttpClient(tendermintUrl);
|
||||
|
@ -24,7 +24,8 @@ function filterBadStatus(res: any): any {
|
||||
*
|
||||
* For some reason, fetch does not complain about missing server-side CORS support.
|
||||
*/
|
||||
async function http(method: "POST", url: string, request?: any): Promise<any> {
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
export async function http(method: "POST", url: string, request?: any): Promise<any> {
|
||||
if (typeof fetch !== "undefined") {
|
||||
const body = request ? JSON.stringify(request) : undefined;
|
||||
return fetch(url, { method: method, body: body })
|
||||
|
Loading…
x
Reference in New Issue
Block a user