diff --git a/packages/tendermint-rpc/src/rpcclients/httpclient.spec.ts b/packages/tendermint-rpc/src/rpcclients/httpclient.spec.ts index 66295e3379..1c32650843 100644 --- a/packages/tendermint-rpc/src/rpcclients/httpclient.spec.ts +++ b/packages/tendermint-rpc/src/rpcclients/httpclient.spec.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/naming-convention */ import { createJsonRpcRequest } from "../jsonrpc"; import { defaultInstance } from "../testutil.spec"; import { http, HttpClient } from "./httpclient"; @@ -8,7 +9,14 @@ function pendingWithoutTendermint(): void { } } +function pendingWithoutHttpServer(): void { + if (!process.env.HTTPSERVER_ENABLED) { + pending("Set HTTPSERVER_ENABLED to enable HTTP tests"); + } +} + const tendermintUrl = defaultInstance.url; +const echoUrl = "http://localhost:5555/echo_headers"; describe("http", () => { it("can send a health request", async () => { @@ -22,6 +30,42 @@ describe("http", () => { http("POST", `http://localhost:56745`, undefined, createJsonRpcRequest("health")), ).toBeRejectedWithError(/(ECONNREFUSED|Failed to fetch)/i); }); + + it("can send custom headers", async () => { + pendingWithoutHttpServer(); + // Without custom headers + const response1 = await http("POST", echoUrl, undefined, createJsonRpcRequest("health")); + expect(response1).toEqual({ + request_headers: jasmine.objectContaining({ + // Basic headers from http client + Accept: jasmine.any(String), + "Content-Length": jasmine.any(String), + "Content-Type": "application/json", + Host: jasmine.any(String), + "User-Agent": jasmine.any(String), + }), + }); + + // With custom headers + const response2 = await http( + "POST", + echoUrl, + { foo: "bar123", Authorization: "Basic Z3Vlc3Q6bm9QYXNzMTIz" }, + createJsonRpcRequest("health"), + ); + expect(response2).toEqual({ + request_headers: jasmine.objectContaining({ + // Basic headers from http client + "Content-Length": jasmine.any(String), + "Content-Type": "application/json", + Host: jasmine.any(String), + "User-Agent": jasmine.any(String), + // Custom headers + foo: "bar123", + Authorization: "Basic Z3Vlc3Q6bm9QYXNzMTIz", + }), + }); + }); }); describe("HttpClient", () => { diff --git a/packages/tendermint-rpc/src/rpcclients/httpclient.ts b/packages/tendermint-rpc/src/rpcclients/httpclient.ts index 83dd81bb93..0b7dedacb4 100644 --- a/packages/tendermint-rpc/src/rpcclients/httpclient.ts +++ b/packages/tendermint-rpc/src/rpcclients/httpclient.ts @@ -11,8 +11,6 @@ import { hasProtocol, RpcClient } from "./rpcclient"; // Global symbols in some environments // https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch declare const fetch: any | undefined; -// eslint-disable-next-line @typescript-eslint/naming-convention -declare const Headers: any | undefined; function filterBadStatus(res: any): any { if (res.status >= 400) { @@ -34,11 +32,14 @@ export async function http( request?: any, ): Promise { if (typeof fetch !== "undefined") { - const body = request ? JSON.stringify(request) : undefined; const settings = { method: method, - body: body, - headers: headers ? new Headers(headers) : undefined, + body: request ? JSON.stringify(request) : undefined, + headers: { + // eslint-disable-next-line @typescript-eslint/naming-convention + "Content-Type": "application/json", + ...headers, + }, }; return fetch(url, settings) .then(filterBadStatus) diff --git a/packages/tendermint-rpc/webpack.web.config.js b/packages/tendermint-rpc/webpack.web.config.js index 260f315444..d72c8f897f 100644 --- a/packages/tendermint-rpc/webpack.web.config.js +++ b/packages/tendermint-rpc/webpack.web.config.js @@ -16,7 +16,10 @@ module.exports = [ filename: "tests.js", }, plugins: [ - new webpack.EnvironmentPlugin({ TENDERMINT_ENABLED: "" }), + new webpack.EnvironmentPlugin({ + HTTPSERVER_ENABLED: "", + TENDERMINT_ENABLED: "", + }), new webpack.ProvidePlugin({ Buffer: ["buffer", "Buffer"], }),