mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 13:47:12 +00:00
Test custom headers
This commit is contained in:
parent
b8d7db9311
commit
384d8f20f3
@ -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", () => {
|
||||
|
@ -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<any> {
|
||||
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)
|
||||
|
@ -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"],
|
||||
}),
|
||||
|
Loading…
x
Reference in New Issue
Block a user