|
1 | 1 | // Import Node.js Dependencies |
2 | 2 | import fs from "node:fs"; |
3 | 3 | import { fileURLToPath } from "node:url"; |
4 | | -import { after, before, describe, test } from "node:test"; |
| 4 | +import { after, before, describe, test, mock } from "node:test"; |
5 | 5 | import { once } from "node:events"; |
6 | 6 | import path from "node:path"; |
7 | 7 | import assert from "node:assert"; |
| 8 | +import stream from "node:stream"; |
8 | 9 |
|
9 | 10 | // Import Third-party Dependencies |
10 | 11 | import { get, post, MockAgent, getGlobalDispatcher, setGlobalDispatcher } from "@myunisoft/httpie"; |
11 | 12 | import * as i18n from "@nodesecure/i18n"; |
12 | 13 | import * as flags from "@nodesecure/flags"; |
13 | 14 | import enableDestroy from "server-destroy"; |
14 | | -import esmock from "esmock"; |
15 | 15 | import cacache from "cacache"; |
16 | 16 |
|
17 | 17 | // Import Internal Dependencies |
18 | | -import { buildServer } from "../src/http-server/index.js"; |
| 18 | +import { buildServer, BROWSER } from "../src/http-server/index.js"; |
19 | 19 | import { CACHE_PATH } from "../src/cache.js"; |
| 20 | +import * as rootEndpoint from "../src/http-server/endpoints/root.js"; |
| 21 | +import * as flagsEndpoint from "../src/http-server/endpoints/flags.js"; |
20 | 22 |
|
21 | 23 | // CONSTANTS |
22 | 24 | const kHttpPort = 17049; |
@@ -69,20 +71,35 @@ describe("httpServer", { concurrency: 1 }, () => { |
69 | 71 | assert.equal(result.headers["content-type"], "text/html"); |
70 | 72 | }); |
71 | 73 |
|
72 | | - test("'/' should fail", async() => { |
73 | | - const errors = []; |
74 | | - const module = await esmock("../src/http-server/endpoints/root.js", { |
75 | | - "@polka/send-type": { |
76 | | - default: (_res, _status, { error }) => errors.push(error) |
| 74 | + test("'/' should fail", async(ctx) => { |
| 75 | + class Response { |
| 76 | + constructor() { |
| 77 | + this.body = ""; |
| 78 | + this.headers = {}; |
| 79 | + this.statusCode = 200; |
77 | 80 | } |
78 | | - }); |
79 | | - |
80 | | - await module.get({}, ({ |
81 | | - writeHead: () => { |
82 | | - throw new Error("fake error"); |
| 81 | + end(str) { |
| 82 | + this.body = str; |
| 83 | + } |
| 84 | + writeHead(int) { |
| 85 | + this.statusCode = int; |
| 86 | + } |
| 87 | + getHeader(key) { |
| 88 | + return this.headers[key]; |
83 | 89 | } |
84 | | - })); |
85 | | - assert.deepEqual(errors, ["fake error"]); |
| 90 | + } |
| 91 | + |
| 92 | + const fakeError = "fake error"; |
| 93 | + function toThrow() { |
| 94 | + throw new Error(fakeError); |
| 95 | + } |
| 96 | + ctx.mock.method(Response.prototype, "writeHead", toThrow, { times: 1 }); |
| 97 | + |
| 98 | + const response = new Response(); |
| 99 | + await rootEndpoint.get({}, response); |
| 100 | + |
| 101 | + assert.strictEqual(response.body, JSON.stringify({ error: fakeError })); |
| 102 | + assert.strictEqual(response.statusCode, 500); |
86 | 103 | }); |
87 | 104 |
|
88 | 105 | test("'/flags' should return the flags list as JSON", async() => { |
@@ -111,19 +128,12 @@ describe("httpServer", { concurrency: 1 }, () => { |
111 | 128 | }); |
112 | 129 | }); |
113 | 130 |
|
114 | | - test("'/flags/description/:title' should fail", async() => { |
115 | | - const module = await esmock("../src/http-server/endpoints/flags.js", { |
116 | | - stream: { |
117 | | - pipeline: (_stream, _res, err) => err("fake error") |
118 | | - }, |
119 | | - fs: { |
120 | | - createReadStream: () => "foo" |
121 | | - } |
122 | | - }); |
| 131 | + test("'/flags/description/:title' should fail", async(ctx) => { |
| 132 | + ctx.mock.method(stream, "pipeline", (_stream, _res, err) => err("fake error")); |
123 | 133 | const logs = []; |
124 | 134 | console.error = (data) => logs.push(data); |
125 | 135 |
|
126 | | - await module.get({ params: { title: "hasWarnings" } }, ({ writeHead: () => true })); |
| 136 | + await flagsEndpoint.get({ params: { title: "hasWarnings" } }, ({ writeHead: () => true })); |
127 | 137 | assert.deepEqual(logs, ["fake error"]); |
128 | 138 | }); |
129 | 139 |
|
@@ -312,30 +322,25 @@ describe("httpServer", { concurrency: 1 }, () => { |
312 | 322 |
|
313 | 323 | describe("httpServer without options", () => { |
314 | 324 | let httpServer; |
315 | | - let opened = false; |
| 325 | + let spawnOpen; |
316 | 326 | // We want to disable WS |
317 | 327 | process.env.NODE_ENV = "test"; |
318 | 328 |
|
319 | 329 | before(async() => { |
320 | | - const module = await esmock("../src/http-server/index.js", { |
321 | | - open: () => (opened = true) |
322 | | - }); |
323 | | - |
324 | | - httpServer = module.buildServer(JSON_PATH); |
| 330 | + spawnOpen = mock.method(BROWSER, "open", () => void 0); |
| 331 | + httpServer = buildServer(JSON_PATH); |
325 | 332 | await once(httpServer.server, "listening"); |
326 | 333 | enableDestroy(httpServer.server); |
327 | 334 | }); |
328 | 335 |
|
329 | 336 | after(async() => { |
330 | 337 | httpServer.server.destroy(); |
| 338 | + spawnOpen.mock.restore(); |
331 | 339 | }); |
332 | 340 |
|
333 | | - test("should listen on random port", () => { |
| 341 | + test("should listen on random port and call childProcess.spawn method ('open' pkg) to open link", async() => { |
334 | 342 | assert.ok(httpServer.server.address().port > 0); |
335 | | - }); |
336 | | - |
337 | | - test("should have openLink to true", () => { |
338 | | - assert.equal(opened, true); |
| 343 | + assert.strictEqual(spawnOpen.mock.callCount(), 1); |
339 | 344 | }); |
340 | 345 | }); |
341 | 346 |
|
|
0 commit comments