Web: Socket: added tests, added getting port from config

This commit is contained in:
Nikita Gopienko 2021-12-09 17:38:57 +03:00
parent ed41326acc
commit 9aff9d8f30
3 changed files with 47 additions and 6 deletions

View File

@ -11,7 +11,7 @@ const requestManager = require("./requestManager");
const config = require("./config"); const config = require("./config");
const auth = require("./middleware/auth.js"); const auth = require("./middleware/auth.js");
const port = 9899; const port = config.get("port") || 3000;
const app = express(); const app = express();
const secret = config.get("core.machinekey") + new Date().getTime(); const secret = config.get("core.machinekey") + new Date().getTime();
@ -74,8 +74,6 @@ io.on("connection", (socket) => {
// console.log("socket.handshake", socket.user); //TODO: // console.log("socket.handshake", socket.user); //TODO:
// const request = socket.client.request; // const request = socket.client.request;
console.log("connection success");
socket.on("startFileEdit", async (fileId) => { socket.on("startFileEdit", async (fileId) => {
const url = `files/file/${fileId}`; const url = `files/file/${fileId}`;
const options = { method: "GET", headers: tempHeaders }; //{ method: "POST", body: {} }; const options = { method: "GET", headers: tempHeaders }; //{ method: "POST", body: {} };
@ -84,7 +82,6 @@ io.on("connection", (socket) => {
}); });
socket.on("reportFileCreation", (fileId) => { socket.on("reportFileCreation", (fileId) => {
console.log("reportFileCreation");
io.emit("getFileCreation", fileId); io.emit("getFileCreation", fileId);
}); });
}); });
@ -117,3 +114,5 @@ app.get("/", (req, res) => {
}); });
httpServer.listen(port, () => console.log(`Server started on port: ${port}`)); httpServer.listen(port, () => console.log(`Server started on port: ${port}`));
module.exports = io;

View File

@ -5,7 +5,7 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "nodemon index", "start": "nodemon index",
"test": "echo \"Error: no test specified\" && exit 1" "test": "jest"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
@ -17,9 +17,12 @@
"express-socket.io-session": "^1.3.5", "express-socket.io-session": "^1.3.5",
"memorystore": "^1.6.6", "memorystore": "^1.6.6",
"nconf": "^0.11.3", "nconf": "^0.11.3",
"nodemon": "^2.0.15",
"path": "^0.12.7", "path": "^0.12.7",
"request": "^2.88.2", "request": "^2.88.2",
"socket.io": "^4.4.0" "socket.io": "^4.4.0"
},
"devDependencies": {
"jest": "^27.4.3",
"nodemon": "^2.0.15"
} }
} }

View File

@ -0,0 +1,39 @@
const io = require("../index");
const Client = require("socket.io-client");
describe("Jest tests", () => {
let clientSocket, serverSocket;
beforeAll((done) => {
const port = io.engine.port || 9899;
clientSocket = new Client(`http://localhost:${port}`);
io.on("connection", (socket) => {
serverSocket = socket;
});
clientSocket.on("connect", done);
});
afterAll(() => {
io.close();
clientSocket.close();
});
test("Test socket connect", (done) => {
expect(clientSocket.connected).toEqual(true);
done();
});
test("Test startFileEdit", (done) => {
if (clientSocket.connected) {
const fileId = "12345";
clientSocket.emit("reportFileCreation", fileId);
clientSocket.on("getFileCreation", (file) => {
expect(file).toEqual(fileId);
done();
});
}
});
//TODO: Need test for authentication
});