From f65a5a8c89ad495c21a2f4fcb45bc426db57b9b0 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Fri, 23 Dec 2016 13:50:11 +0200 Subject: [PATCH] Add web server tests --- test/fixtures/.lounge/config.js | 3 + test/fixtures/env.js | 7 +++ test/server.js | 100 ++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 test/server.js diff --git a/test/fixtures/.lounge/config.js b/test/fixtures/.lounge/config.js index 7e5efe76..c04fbb18 100644 --- a/test/fixtures/.lounge/config.js +++ b/test/fixtures/.lounge/config.js @@ -3,5 +3,8 @@ var config = require("../../../defaults/config.js"); config.prefetch = true; +config.host = config.bind = "127.0.0.1"; +config.port = 61337; +config.transports = ["websocket"]; module.exports = config; diff --git a/test/fixtures/env.js b/test/fixtures/env.js index f3c51b0d..f561eafd 100644 --- a/test/fixtures/env.js +++ b/test/fixtures/env.js @@ -1,4 +1,11 @@ "use strict"; +global.log = { + error: () => console.error.apply(console, arguments), + warn: () => {}, + info: () => {}, + debug: () => {}, +}; + var home = require("path").join(__dirname, ".lounge"); require("../../src/helper").setHome(home); diff --git a/test/server.js b/test/server.js new file mode 100644 index 00000000..0b1a3388 --- /dev/null +++ b/test/server.js @@ -0,0 +1,100 @@ +"use strict"; + +const Helper = require("../src/helper"); +const expect = require("chai").expect; +const request = require("request"); +const io = require("socket.io-client"); + +describe("Server", () => { + const server = require("../src/server"); + server(); + + const webURL = `http://${Helper.config.host}:${Helper.config.port}/`; + + describe("Express", () => { + it("should run a web server on " + webURL, done => { + request(webURL, (error, response, body) => { + expect(error).to.be.null; + expect(body).to.include("The Lounge"); + expect(body).to.include("https://thelounge.github.io/"); + + done(); + }); + }); + + it("should serve static content correctly", done => { + request(webURL + "manifest.json", (error, response, body) => { + expect(error).to.be.null; + + body = JSON.parse(body); + expect(body.name).to.equal("The Lounge"); + + done(); + }); + }); + }); + + describe("WebSockets", () => { + let client; + + beforeEach(() => { + client = io(webURL, { + path: "/socket.io/", + autoConnect: false, + reconnection: false, + timeout: 1000, + transports: [ + "websocket" + ] + }); + + // Server emits events faster than the test can bind them + setTimeout(() => { + client.open(); + }, 1); + }); + + afterEach(() => { + client.close(); + }); + + it("should emit authorized message", done => { + client.on("authorized", done); + }); + + it("should create network", done => { + client.on("init", () => { + client.emit("conn", { + username: "test-user", + realname: "The Lounge Test", + nick: "test-user", + join: "#thelounge, #spam", + name: "Test Network", + host: Helper.config.host, + port: 6667, + }); + }); + + client.on("network", data => { + expect(data.networks).to.be.an("array"); + expect(data.networks).to.have.lengthOf(1); + expect(data.networks[0].realname).to.equal("The Lounge Test"); + expect(data.networks[0].channels).to.have.lengthOf(3); + expect(data.networks[0].channels[0].name).to.equal("Test Network"); + expect(data.networks[0].channels[1].name).to.equal("#thelounge"); + done(); + }); + }); + + it("should emit init message", done => { + client.on("init", data => { + expect(data.active).to.equal(-1); + expect(data.networks).to.be.an("array"); + expect(data.networks).to.be.empty; + expect(data.token).to.be.null; + + done(); + }); + }); + }); +});