Skip to content

Commit

Permalink
add some easy tests for utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Aug 21, 2024
1 parent e80c08a commit b0428e0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
29 changes: 15 additions & 14 deletions tests/FaucetHttpServer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { FaucetHttpResponse, FaucetHttpServer } from '../src/webserv/FaucetHttpS
import { EthClaimManager } from '../src/eth/EthClaimManager.js';
import { sha256 } from '../src/utils/CryptoUtils.js';
import { FaucetProcess } from '../src/common/FaucetProcess.js';
import { FetchUtil } from '../src/utils/FetchUtil.js';

describe("Faucet Web Server", () => {
let globalStubs;
Expand Down Expand Up @@ -75,7 +76,7 @@ describe("Faucet Web Server", () => {
let webServer = ServiceManager.GetService(FaucetHttpServer);
webServer.initialize();
let listenPort = webServer.getListenPort();
let indexData = await fetch("http://localhost:" + listenPort, {method: "GET"}).then((rsp) => rsp.text());
let indexData = await FetchUtil.fetch("http://localhost:" + listenPort, {method: "GET"}).then((rsp) => rsp.text());
expect(indexData).contains(faucetConfig.faucetTitle, "not index contents");
});

Expand All @@ -89,7 +90,7 @@ describe("Faucet Web Server", () => {
let webServer = ServiceManager.GetService(FaucetHttpServer);
webServer.initialize();
let listenPort = webServer.getListenPort();
let indexData = await fetch("http://localhost:" + listenPort, {method: "GET"}).then((rsp) => rsp.text());
let indexData = await FetchUtil.fetch("http://localhost:" + listenPort, {method: "GET"}).then((rsp) => rsp.text());
expect(indexData).contains("<!-- pow-faucet-header -->", "not index contents");
});

Expand All @@ -100,7 +101,7 @@ describe("Faucet Web Server", () => {
let webServer = ServiceManager.GetService(FaucetHttpServer);
webServer.initialize();
let listenPort = webServer.getListenPort();
let configData = await fetch("http://localhost:" + listenPort + "/api/getFaucetConfig", {method: "GET"}).then((rsp) => rsp.json());
let configData = await FetchUtil.fetch("http://localhost:" + listenPort + "/api/getFaucetConfig", {method: "GET"}).then((rsp) => rsp.json());
expect(!!configData).equals(true, "no api response");
expect((configData as any).faucetTitle).equals(faucetConfig.faucetTitle, "api response mismatch");
});
Expand All @@ -116,7 +117,7 @@ describe("Faucet Web Server", () => {
return sha256(body.toString());
});
let listenPort = webServer.getListenPort();
let responseData = await fetch("http://localhost:" + listenPort + "/api/testEndpoint", {
let responseData = await FetchUtil.fetch("http://localhost:" + listenPort + "/api/testEndpoint", {
method: 'POST',
body: JSON.stringify({test: 1}),
headers: {'Content-Type': 'application/json'}
Expand All @@ -138,7 +139,7 @@ describe("Faucet Web Server", () => {
let error: Error = null as any;
try {
let testData = "0123456789".repeat(1024 * 1024);
await fetch("http://localhost:" + listenPort + "/api/testEndpoint", {
await FetchUtil.fetch("http://localhost:" + listenPort + "/api/testEndpoint", {
method: 'POST',
body: JSON.stringify({test: testData}),
headers: {'Content-Type': 'application/json'}
Expand All @@ -159,7 +160,7 @@ describe("Faucet Web Server", () => {
return new FaucetHttpResponse(500, "Test Error 4135");
});
let listenPort = webServer.getListenPort();
let testRsp = await fetch("http://localhost:" + listenPort + "/api/testEndpoint", {method: "GET"});
let testRsp = await FetchUtil.fetch("http://localhost:" + listenPort + "/api/testEndpoint", {method: "GET"});
expect(testRsp.status).to.equal(500, "unexpected http response code");
expect(testRsp.statusText).to.matches(/Test Error 4135/, "unexpected http response code");
});
Expand All @@ -173,7 +174,7 @@ describe("Faucet Web Server", () => {
return Promise.reject("Test Error 3672");
});
let listenPort = webServer.getListenPort();
let testRsp = await fetch("http://localhost:" + listenPort + "/api/testEndpoint", {method: "GET"});
let testRsp = await FetchUtil.fetch("http://localhost:" + listenPort + "/api/testEndpoint", {method: "GET"});
let testRspText = await testRsp.text();
expect(testRsp.status).to.equal(500, "unexpected http response code");
expect(testRspText).to.matches(/Test Error 3672/, "unexpected http response code");
Expand All @@ -188,7 +189,7 @@ describe("Faucet Web Server", () => {
throw new FaucetHttpResponse(500, "Test Error 4267");
});
let listenPort = webServer.getListenPort();
let testRsp = await fetch("http://localhost:" + listenPort + "/api/testEndpoint", {method: "GET"});
let testRsp = await FetchUtil.fetch("http://localhost:" + listenPort + "/api/testEndpoint", {method: "GET"});
expect(testRsp.status).to.equal(500, "unexpected http response code");
expect(testRsp.statusText).to.matches(/Test Error 4267/, "unexpected http response code");
});
Expand All @@ -202,7 +203,7 @@ describe("Faucet Web Server", () => {
throw "unexpected error";
});
let listenPort = webServer.getListenPort();
let testRsp = await fetch("http://localhost:" + listenPort + "/api/testEndpoint", {method: "GET"});
let testRsp = await FetchUtil.fetch("http://localhost:" + listenPort + "/api/testEndpoint", {method: "GET"});
expect(testRsp.status).to.equal(500, "unexpected http response code");
expect(testRsp.statusText).to.matches(/Internal Server Error/, "unexpected http response code");
});
Expand Down Expand Up @@ -258,7 +259,7 @@ describe("Faucet Web Server", () => {
let webServer = ServiceManager.GetService(FaucetHttpServer);
webServer.initialize();
let listenPort = webServer.getListenPort();
let configOptionsRsp = await fetch(
let configOptionsRsp = await FetchUtil.fetch(
"http://localhost:" + listenPort + "/api/getFaucetConfig",
{
method: "OPTIONS",
Expand All @@ -270,7 +271,7 @@ describe("Faucet Web Server", () => {
expect(configOptionsRsp.headers.get("access-control-allow-origin")).equals("https://example.com", "access-control-allow-origin mismatch");
expect(configOptionsRsp.headers.get("access-control-allow-methods")).equals("GET, POST", "access-control-allow-methods mismatch");

let configRsp = await fetch(
let configRsp = await FetchUtil.fetch(
"http://localhost:" + listenPort + "/api/getFaucetConfig",
{
method: "GET",
Expand All @@ -294,7 +295,7 @@ describe("Faucet Web Server", () => {
let webServer = ServiceManager.GetService(FaucetHttpServer);
webServer.initialize();
let listenPort = webServer.getListenPort();
let configOptionsRsp = await fetch(
let configOptionsRsp = await FetchUtil.fetch(
"http://localhost:" + listenPort + "/api/getFaucetConfig",
{
method: "OPTIONS",
Expand Down Expand Up @@ -336,7 +337,7 @@ describe("Faucet Web Server", () => {
if(!fs.existsSync(resourcePath))
fs.writeFileSync(resourcePath, "test");

let optionsRsp = await fetch(
let optionsRsp = await FetchUtil.fetch(
"http://localhost:" + listenPort + resource,
{
method: "OPTIONS",
Expand All @@ -348,7 +349,7 @@ describe("Faucet Web Server", () => {
expect(optionsRsp.headers.get("access-control-allow-origin")).equals("https://example.com", "access-control-allow-origin mismatch");
expect(optionsRsp.headers.get("access-control-allow-methods")).equals("GET, POST", "access-control-allow-methods mismatch");

let dataRsp = await fetch(
let dataRsp = await FetchUtil.fetch(
"http://localhost:" + listenPort + resource,
{
method: "GET",
Expand Down
38 changes: 37 additions & 1 deletion tests/Utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
import 'mocha';
import sinon from 'sinon';
import { expect } from 'chai';
import { renderTimespan } from '../src/utils/DateUtils.js';
import { renderDate, renderTimespan } from '../src/utils/DateUtils.js';
import { timeoutPromise } from '../src/utils/PromiseUtils.js';
import { getHashedIp } from '../src/utils/HashedInfo.js';
import { isVersionLower } from '../src/utils/VersionCompare.js';
import { strFormatPlaceholder } from '../src/utils/StringUtils.js';


describe("Utility Functions", () => {

it("PromiseUtils.timeoutPromise", async () => {
let now = new Date().getTime();
let err = false;
try {
await timeoutPromise<void>(new Promise<void>((resolve, reject) => {
setTimeout(() => resolve(), 1000);
}), 100);
} catch(e) {
err = true;
}

expect(new Date().getTime() - now).to.be.lessThan(200, "unexpected result");
expect(err).to.equal(true, "no timeout error thrown")
});

it("DateUtils.renderTimespan", async () => {
expect(renderTimespan(130, 2)).to.equal("2min 10sec", "unexpected result");
expect(renderTimespan(439964, 5)).to.equal("5d 2h 12min 44sec", "unexpected result");
expect(renderTimespan(439964, 2)).to.equal("5d 2h", "unexpected result");
});

it("DateUtils.renderDate", async () => {
expect(renderDate(new Date(12000), true)).to.equal("1970-01-01 01:00", "unexpected result 1");
expect(renderDate(new Date(12000), false)).to.equal("1970-01-01", "unexpected result 2");
expect(renderDate(new Date(12000), true, true)).to.equal("1970-01-01 01:00:12", "unexpected result 3");
});

it("HashedInfo.getHashedIp", async () => {
expect(getHashedIp("1.2.3.4", "test")).to.equal("df6.60b.ef9.b3e", "unexpected result");
expect(getHashedIp("2003:DE:C711::ECFF:FE0E:21F1", "test")).to.equal("f84:d47:e32:0:0:dc0:d1e:d8c", "unexpected result");
});

it("VersionCompare.isVersionLower", async () => {
expect(isVersionLower(null as any, "1.2")).to.equal(null, "unexpected result 1")
expect(isVersionLower("1.2.3", "1.2.4")).to.equal(true, "unexpected result 2")
expect(isVersionLower("1.2", "1.2.4")).to.equal(true, "unexpected result 3")
expect(isVersionLower("1.2.3", "1.2")).to.equal(false, "unexpected result 4")
expect(isVersionLower("1.2.3", "1.2.3")).to.equal(undefined, "unexpected result 5")
});

it("StringUtils.strFormatPlaceholder", async () => {
expect(strFormatPlaceholder("1: {1}, 2: {2}", "A")).to.equal("1: A, 2: {2}", "unexpected result 1")
});

});

0 comments on commit b0428e0

Please sign in to comment.