Write tests for http function

This commit is contained in:
Simon Warta 2021-10-12 00:35:24 +02:00
parent aee0714133
commit 11ec6f1393
2 changed files with 19 additions and 4 deletions

View File

@ -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);

View File

@ -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 })