aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-css-plugin/css-bundle/tests/server.test.ts
diff options
context:
space:
mode:
authorElena Vilchik2019-12-18 17:10:10 +0100
committerAlban Auzeill2019-12-18 17:10:10 +0100
commitc8f0071c4f5336dfe0efc5d3c218ab49f2401264 (patch)
tree254cd5ed9531d7c62bab4f8ec082e085795ecb8f /sonar-css-plugin/css-bundle/tests/server.test.ts
parent13fe08e87c8a70ffe6e248b774ef826bbe1f779d (diff)
downloadsonar-css-c8f0071c4f5336dfe0efc5d3c218ab49f2401264.tar.bz2
Rely on NodeJS API of Stylelint to execute CSS rules (#221)
Diffstat (limited to 'sonar-css-plugin/css-bundle/tests/server.test.ts')
-rw-r--r--sonar-css-plugin/css-bundle/tests/server.test.ts153
1 files changed, 153 insertions, 0 deletions
diff --git a/sonar-css-plugin/css-bundle/tests/server.test.ts b/sonar-css-plugin/css-bundle/tests/server.test.ts
new file mode 100644
index 0000000..8465810
--- /dev/null
+++ b/sonar-css-plugin/css-bundle/tests/server.test.ts
@@ -0,0 +1,153 @@
+import { start, setLogHandlersForTests } from "../src/server";
+import * as http from "http";
+import { Server } from "http";
+import { promisify } from "util";
+import { AddressInfo } from "net";
+import { postToServer } from "./utils";
+import * as path from "path";
+
+const configFile = path.join(__dirname, "fixtures", "stylelintconfig.json");
+
+describe("server", () => {
+ let server: Server;
+ let close: () => Promise<void>;
+ const logSpy = jest.fn();
+ const errorSpy = jest.fn();
+
+ beforeAll(async () => {
+ setLogHandlersForTests(logSpy, errorSpy);
+ server = await start();
+ close = promisify(server.close.bind(server));
+ });
+
+ afterAll(async () => {
+ jest.restoreAllMocks();
+ await close();
+ });
+
+ it("should log with debug server start", async () => {
+ expect(server.listening).toEqual(true);
+ expect(logSpy).toBeCalledTimes(2);
+ expect(logSpy).toBeCalledWith(
+ "DEBUG starting stylelint-bridge server at port",
+ 0
+ );
+ expect(logSpy).toBeCalledWith(
+ "DEBUG stylelint-bridge server is running at port",
+ (<AddressInfo>server.address()).port
+ );
+ expect(errorSpy).toBeCalledTimes(0);
+ });
+
+ it("should respond to analysis request", async () => {
+ const request = JSON.stringify({
+ filePath: path.join(__dirname, "fixtures", "file.css"),
+ configFile
+ });
+ const response = await post(request, "/analyze");
+ expect(JSON.parse(response)).toEqual([
+ {
+ line: 1,
+ rule: "block-no-empty",
+ text: "Unexpected empty block (block-no-empty)"
+ }
+ ]);
+ });
+
+ it("should respond to analysis request for php", async () => {
+ const requestPhp = JSON.stringify({
+ filePath: path.join(__dirname, "fixtures", "file.php"),
+ configFile
+ });
+ const responsePhp = await post(requestPhp, "/analyze");
+ expect(JSON.parse(responsePhp)).toEqual([
+ {
+ line: 7,
+ rule: "block-no-empty",
+ text: "Unexpected empty block (block-no-empty)"
+ }
+ ]);
+ });
+
+ it("should respond to analysis request for html", async () => {
+ const requestHtml = JSON.stringify({
+ filePath: path.join(__dirname, "fixtures", "file.html"),
+ configFile
+ });
+ const responseHtml = await post(requestHtml, "/analyze");
+ expect(JSON.parse(responseHtml)).toEqual([
+ {
+ line: 6,
+ rule: "block-no-empty",
+ text: "Unexpected empty block (block-no-empty)"
+ }
+ ]);
+ });
+
+ it("should cut BOM", async () => {
+ const response = await post(
+ JSON.stringify({
+ filePath: path.join(__dirname, "fixtures", "file-bom.css"),
+ configFile
+ }),
+ "/analyze"
+ );
+ expect(JSON.parse(response)).toEqual([
+ {
+ line: 1,
+ rule: "block-no-empty",
+ text: "Unexpected empty block (block-no-empty)"
+ }
+ ]);
+ });
+
+ it("should respond OK! when started", done => {
+ const req = http.request(
+ {
+ host: "localhost",
+ port: (<AddressInfo>server.address()).port,
+ path: "/status",
+ method: "GET"
+ },
+ res => {
+ let data = "";
+ res.on("data", chunk => {
+ data += chunk;
+ });
+ res.on("end", () => {
+ expect(data).toEqual("OK!");
+ done();
+ });
+ }
+ );
+ req.end();
+ });
+
+ it("should return empty list of issues when request not json", async () => {
+ const response = await post("invalid json", "/analyze");
+ expect(JSON.parse(response)).toEqual([]);
+ expect(errorSpy).toHaveBeenCalledWith(
+ expect.objectContaining({
+ message: expect.stringContaining(
+ "Unexpected token i in JSON at position 0"
+ )
+ })
+ );
+ });
+
+ it("should return empty list of issues when invalid request", async () => {
+ const response = await post("{}", "/analyze");
+ expect(JSON.parse(response)).toEqual([]);
+ expect(errorSpy).toHaveBeenCalledWith(
+ expect.objectContaining({
+ message: expect.stringContaining(
+ "Unexpected token i in JSON at position 0"
+ )
+ })
+ );
+ });
+
+ function post(data: string, endpoint: string): Promise<string> {
+ return postToServer(data, endpoint, server);
+ }
+});