-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1322 from cosmos/rm-http-tests
Pull out function http and deduplicate tests
- Loading branch information
Showing
6 changed files
with
111 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { createJsonRpcRequest } from "../jsonrpc"; | ||
import { defaultInstance, pendingWithoutTendermint } from "../testutil.spec"; | ||
import { http } from "./http"; | ||
|
||
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 () => { | ||
pendingWithoutTendermint(); | ||
const response = await http("POST", `http://${tendermintUrl}`, undefined, createJsonRpcRequest("health")); | ||
expect(response).toEqual(jasmine.objectContaining({ jsonrpc: "2.0" })); | ||
}); | ||
|
||
it("errors for non-open port", async () => { | ||
await expectAsync( | ||
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", | ||
}), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import axios from "axios"; | ||
|
||
// Global symbols in some environments | ||
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch | ||
declare const fetch: any | undefined; | ||
|
||
function filterBadStatus(res: any): any { | ||
if (res.status >= 400) { | ||
throw new Error(`Bad status on response: ${res.status}`); | ||
} | ||
return res; | ||
} | ||
|
||
/** | ||
* Helper to work around missing CORS support in Tendermint (https://github.com/tendermint/tendermint/pull/2800) | ||
* | ||
* For some reason, fetch does not complain about missing server-side CORS support. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export async function http( | ||
method: "POST", | ||
url: string, | ||
headers: Record<string, string> | undefined, | ||
request?: any, | ||
): Promise<any> { | ||
if (typeof fetch !== "undefined") { | ||
const settings = { | ||
method: method, | ||
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) | ||
.then((res: any) => res.json()); | ||
} else { | ||
return axios | ||
.request({ url: url, method: method, data: request, headers: headers }) | ||
.then((res) => res.data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters