first post test
This commit is contained in:
parent
e2b931d6c5
commit
ab995d8f8b
4 changed files with 152 additions and 5 deletions
|
@ -32,8 +32,7 @@ export const addPostMethods = (server: express.Express) => {
|
|||
debug("POST", "reveived:", req.body);
|
||||
const aws = (state: string, data: any) => {
|
||||
res.status(state == "error" ? 400 : 200);
|
||||
if (typeof data == "string") res.send(data);
|
||||
else res.json(data);
|
||||
res.json({data});
|
||||
};
|
||||
let client: postClient | null = null;
|
||||
try {
|
||||
|
@ -105,7 +104,7 @@ export class postClient {
|
|||
this.client.suspect();
|
||||
return;
|
||||
}
|
||||
if (json.data === null) {
|
||||
if (typeof json.data == "undefined") {
|
||||
aws("error", "data");
|
||||
debug("POST", "send:", "error", "data");
|
||||
return;
|
||||
|
|
130
tests/post.js
130
tests/post.js
|
@ -1,3 +1,131 @@
|
|||
async function postTester(){
|
||||
import WebSocket from 'ws';
|
||||
import { uts, wait } from '../dist/sys/tools.js';
|
||||
import post from "./tests/post.js"
|
||||
import { spawn } from "child_process";
|
||||
import { oConf } from "../dist/sys/config.js"
|
||||
import mariadb from "mariadb";
|
||||
|
||||
let inCI = process.argv.includes("ci");
|
||||
|
||||
oConf.connect(inCI ? 'test.juml' : 'testLocal.juml');
|
||||
|
||||
const connection = await mariadb.createConnection({
|
||||
host: oConf.get("Database", "host"),
|
||||
port: oConf.get("Database", "port"),
|
||||
user: oConf.get("Database", "user"),
|
||||
password: oConf.get("Database", "password"),
|
||||
database: oConf.get("Database", "database"),
|
||||
multipleStatements: true
|
||||
});
|
||||
await wait(100);
|
||||
|
||||
let req = await connection.query("SELECT table_name as n FROM information_schema.tables WHERE table_schema = ?;", [oConf.get("Database", "database")]);
|
||||
let sql = [...req].map(d => "DROP TABLE IF EXISTS `" + d.n + "`;").join("");
|
||||
if (sql.length > 5) await connection.query("SET FOREIGN_KEY_CHECKS = 0;" + sql + "SET FOREIGN_KEY_CHECKS = 1;", []);
|
||||
connection.close();
|
||||
|
||||
function shallowEqual(object1, object2) {
|
||||
const keys1 = Object.keys(object1);
|
||||
const keys2 = Object.keys(object2);
|
||||
if (keys1.length < keys2.length) {
|
||||
return false;
|
||||
}
|
||||
for (let key of keys1) {
|
||||
if (object1[key] != object2[key]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
async function postTester(url) {
|
||||
async function test(header, act, data, expState, expData) {
|
||||
console.log("Testing Act:", act, "with Data:", data);
|
||||
let fetchResp = await fetch(url + "api/" + act, {
|
||||
method: "POST",
|
||||
headers: Object.assign({
|
||||
"Content-Type": "application/json"
|
||||
}, header),
|
||||
body: JSON.stringify({data})
|
||||
});
|
||||
|
||||
let resp = {
|
||||
state: fetchResp.status == 200 ? "ok" : "error",
|
||||
data: (await fetchResp.json()).data
|
||||
}
|
||||
|
||||
if (resp.state != expState) {
|
||||
console.error(`Expected state: '${expState}', but got: '${resp.state}'`);
|
||||
kill();
|
||||
process.exit(1);
|
||||
}
|
||||
if (typeof expData == "object" && expData != null) {
|
||||
if (!shallowEqual(expData, resp.data)) {
|
||||
console.error(`Expected data: '${expData}', but got: '${resp.data}'`);
|
||||
kill();
|
||||
process.exit(1);
|
||||
}
|
||||
} else if (expData != null) {
|
||||
if (expData != resp.data) {
|
||||
console.error(`Expected data: '${expData}', but got: '${resp.data}'`);
|
||||
kill();
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
return resp.data;
|
||||
}
|
||||
|
||||
for (let i = 0; i < post.length; i++) {
|
||||
const currTest = post[i];
|
||||
console.log(`Testing '${currTest[0]}':`);
|
||||
await wait(100);
|
||||
let resp = true;
|
||||
try {
|
||||
resp = await currTest[1](test);
|
||||
} catch (error) {
|
||||
console.error("Test Error: ", error);
|
||||
kill();
|
||||
process.exit(1);
|
||||
}
|
||||
if (resp === false) {
|
||||
console.log("Test respond with error indication!");
|
||||
kill();
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const ls = spawn('node', ['.', '-c', inCI ? 'test.juml' : 'testLocal.juml', '-d']);
|
||||
|
||||
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
|
||||
|
||||
ls.stdout.on('data', (data) => {
|
||||
process.stdout.write(data);
|
||||
if (data.includes("Listening...")) test();
|
||||
});
|
||||
|
||||
ls.stderr.on('data', (data) => {
|
||||
console.error(`stderr: ${data}`);
|
||||
});
|
||||
|
||||
ls.on('close', (code) => {
|
||||
console.log(`child process exited with code ${code}`);
|
||||
process.exit(code);
|
||||
});
|
||||
|
||||
function kill() {
|
||||
ls.kill('SIGINT');
|
||||
}
|
||||
|
||||
let startet = false;
|
||||
|
||||
async function test() {
|
||||
if (startet) return;
|
||||
startet = true;
|
||||
console.log("Start testing POST");
|
||||
await postTester("https://localhost:7224/");
|
||||
kill();
|
||||
process.exit(0);
|
||||
}
|
20
tests/tests/post.js
Normal file
20
tests/tests/post.js
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { generateSigningKey, sign } from '../../dist/sys/crypto.js';
|
||||
|
||||
let name1 = "testUser1";
|
||||
let name2 = "testUser2";
|
||||
let name3 = "testUser3";
|
||||
let accountKey = "123456789";
|
||||
|
||||
let { privateKey, publicKey } = await generateSigningKey();
|
||||
|
||||
const list = [
|
||||
["signup", async (req) => {
|
||||
await req({}, "signup", {
|
||||
name: name1,
|
||||
server: "localhost:7224",
|
||||
accountKey
|
||||
}, "ok", "");
|
||||
}]
|
||||
];
|
||||
|
||||
export default list;
|
|
@ -180,7 +180,7 @@ let startet = false;
|
|||
async function test() {
|
||||
if (startet) return;
|
||||
startet = true;
|
||||
console.log("Start testing");
|
||||
console.log("Start testing WebSocket");
|
||||
await wsTester("wss://localhost:7224/");
|
||||
kill();
|
||||
process.exit(0);
|
||||
|
|
Loading…
Reference in a new issue