2021-09-14 05:35:12 +08:00
|
|
|
import { logger } from "@coder/logger"
|
2021-12-18 03:06:52 +08:00
|
|
|
import { promises } from "fs"
|
2021-09-04 02:41:42 +08:00
|
|
|
import * as http from "http"
|
2021-09-14 05:35:12 +08:00
|
|
|
import * as https from "https"
|
|
|
|
import * as path from "path"
|
2022-05-11 06:44:54 +08:00
|
|
|
import { createApp, ensureAddress, handleArgsSocketCatchError, listen } from "../../../src/node/app"
|
2021-09-14 05:35:12 +08:00
|
|
|
import { OptionalString, setDefaults } from "../../../src/node/cli"
|
|
|
|
import { generateCertificate } from "../../../src/node/util"
|
2021-12-18 03:06:52 +08:00
|
|
|
import { clean, mockLogger, getAvailablePort, tmpdir } from "../../utils/helpers"
|
2021-09-14 05:35:12 +08:00
|
|
|
|
|
|
|
describe("createApp", () => {
|
|
|
|
let unlinkSpy: jest.SpyInstance
|
|
|
|
let port: number
|
|
|
|
let tmpDirPath: string
|
|
|
|
let tmpFilePath: string
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2021-12-18 03:06:52 +08:00
|
|
|
mockLogger()
|
|
|
|
|
2023-06-15 05:32:07 +08:00
|
|
|
const testName = "app"
|
2021-12-18 03:06:52 +08:00
|
|
|
await clean(testName)
|
|
|
|
tmpDirPath = await tmpdir(testName)
|
2021-09-14 05:35:12 +08:00
|
|
|
tmpFilePath = path.join(tmpDirPath, "unlink-socket-file")
|
|
|
|
})
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
// NOTE:@jsjoeio
|
|
|
|
// Be mindful when spying.
|
|
|
|
// You can't spy on fs functions if you do import * as fs
|
|
|
|
// You have to import individually, like we do here with promises
|
|
|
|
// then you can spy on those modules methods, like unlink.
|
|
|
|
// See: https://github.com/aelbore/esbuild-jest/issues/26#issuecomment-893763840
|
|
|
|
unlinkSpy = jest.spyOn(promises, "unlink")
|
|
|
|
port = await getAvailablePort()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should return an Express app, a WebSockets Express app and an http server", async () => {
|
|
|
|
const defaultArgs = await setDefaults({
|
|
|
|
port,
|
|
|
|
})
|
2021-10-29 04:27:17 +08:00
|
|
|
const app = await createApp(defaultArgs)
|
2021-09-14 05:35:12 +08:00
|
|
|
|
|
|
|
// This doesn't check much, but it's a good sanity check
|
|
|
|
// to ensure we actually get back values from createApp
|
2021-10-29 04:27:17 +08:00
|
|
|
expect(app.router).not.toBeNull()
|
|
|
|
expect(app.wsRouter).not.toBeNull()
|
|
|
|
expect(app.server).toBeInstanceOf(http.Server)
|
2021-09-14 05:35:12 +08:00
|
|
|
|
|
|
|
// Cleanup
|
2021-10-29 04:27:17 +08:00
|
|
|
app.dispose()
|
2021-09-14 05:35:12 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should handle error events on the server", async () => {
|
|
|
|
const defaultArgs = await setDefaults({
|
|
|
|
port,
|
|
|
|
})
|
|
|
|
|
|
|
|
const app = await createApp(defaultArgs)
|
|
|
|
|
|
|
|
const testError = new Error("Test error")
|
|
|
|
// We can easily test how the server handles errors
|
|
|
|
// By emitting an error event
|
|
|
|
// Ref: https://stackoverflow.com/a/33872506/3015595
|
2021-10-29 04:27:17 +08:00
|
|
|
app.server.emit("error", testError)
|
2021-12-18 03:06:52 +08:00
|
|
|
expect(logger.error).toHaveBeenCalledTimes(1)
|
|
|
|
expect(logger.error).toHaveBeenCalledWith(`http server error: ${testError.message} ${testError.stack}`)
|
2021-09-14 05:35:12 +08:00
|
|
|
|
|
|
|
// Cleanup
|
2021-10-29 04:27:17 +08:00
|
|
|
app.dispose()
|
2021-09-14 05:35:12 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should reject errors that happen before the server can listen", async () => {
|
|
|
|
// We listen on an invalid port
|
|
|
|
// causing the app to reject the Promise called at startup
|
|
|
|
const port = 2
|
|
|
|
const defaultArgs = await setDefaults({
|
|
|
|
port,
|
|
|
|
})
|
|
|
|
|
|
|
|
async function masterBall() {
|
|
|
|
const app = await createApp(defaultArgs)
|
|
|
|
|
|
|
|
const testError = new Error("Test error")
|
|
|
|
|
2021-10-29 04:27:17 +08:00
|
|
|
app.server.emit("error", testError)
|
2021-09-14 05:35:12 +08:00
|
|
|
|
|
|
|
// Cleanup
|
2021-10-29 04:27:17 +08:00
|
|
|
app.dispose()
|
2021-09-14 05:35:12 +08:00
|
|
|
}
|
|
|
|
|
2023-09-22 08:13:34 +08:00
|
|
|
expect(() => masterBall()).rejects.toThrow("listen EACCES: permission denied")
|
2021-09-14 05:35:12 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should unlink a socket before listening on the socket", async () => {
|
|
|
|
await promises.writeFile(tmpFilePath, "")
|
|
|
|
const defaultArgs = await setDefaults({
|
|
|
|
socket: tmpFilePath,
|
|
|
|
})
|
|
|
|
|
|
|
|
const app = await createApp(defaultArgs)
|
|
|
|
|
2023-06-15 05:32:07 +08:00
|
|
|
expect(unlinkSpy).toHaveBeenCalledWith(tmpFilePath)
|
2021-10-29 04:27:17 +08:00
|
|
|
app.dispose()
|
2021-09-14 05:35:12 +08:00
|
|
|
})
|
|
|
|
|
2022-03-04 00:54:35 +08:00
|
|
|
it("should change the file mode of a socket", async () => {
|
|
|
|
const defaultArgs = await setDefaults({
|
|
|
|
socket: tmpFilePath,
|
|
|
|
"socket-mode": "777",
|
|
|
|
})
|
|
|
|
|
|
|
|
const app = await createApp(defaultArgs)
|
|
|
|
|
|
|
|
expect((await promises.stat(tmpFilePath)).mode & 0o777).toBe(0o777)
|
|
|
|
app.dispose()
|
|
|
|
})
|
|
|
|
|
2021-09-14 05:35:12 +08:00
|
|
|
it("should create an https server if args.cert exists", async () => {
|
|
|
|
const testCertificate = await generateCertificate("localhost")
|
|
|
|
const cert = new OptionalString(testCertificate.cert)
|
|
|
|
const defaultArgs = await setDefaults({
|
|
|
|
port,
|
|
|
|
cert,
|
|
|
|
["cert-key"]: testCertificate.certKey,
|
|
|
|
})
|
|
|
|
const app = await createApp(defaultArgs)
|
|
|
|
|
|
|
|
// This doesn't check much, but it's a good sanity check
|
|
|
|
// to ensure we actually get an https.Server
|
2021-10-29 04:27:17 +08:00
|
|
|
expect(app.server).toBeInstanceOf(https.Server)
|
2021-09-14 05:35:12 +08:00
|
|
|
|
|
|
|
// Cleanup
|
2021-10-29 04:27:17 +08:00
|
|
|
app.dispose()
|
2021-09-14 05:35:12 +08:00
|
|
|
})
|
|
|
|
})
|
2021-09-04 02:41:42 +08:00
|
|
|
|
|
|
|
describe("ensureAddress", () => {
|
|
|
|
let mockServer: http.Server
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockServer = http.createServer()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mockServer.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should throw and error if no address", () => {
|
2021-10-29 04:27:17 +08:00
|
|
|
expect(() => ensureAddress(mockServer, "http")).toThrow("Server has no address")
|
2021-09-04 02:41:42 +08:00
|
|
|
})
|
2022-04-27 03:33:51 +08:00
|
|
|
it("should return the address if it's a string", async () => {
|
|
|
|
mockServer.address = () => "/path/to/unix.sock"
|
2021-10-29 04:27:17 +08:00
|
|
|
const address = ensureAddress(mockServer, "http")
|
2022-04-27 03:33:51 +08:00
|
|
|
expect(address.toString()).toBe(`/path/to/unix.sock`)
|
|
|
|
})
|
|
|
|
it("should construct URL with an IPv4 address", async () => {
|
|
|
|
mockServer.address = () => ({ address: "1.2.3.4", port: 5678, family: "IPv4" })
|
|
|
|
const address = ensureAddress(mockServer, "http")
|
|
|
|
expect(address.toString()).toBe(`http://1.2.3.4:5678/`)
|
|
|
|
})
|
|
|
|
it("should construct URL with an IPv6 address", async () => {
|
|
|
|
mockServer.address = () => ({ address: "a:b:c:d::1234", port: 5678, family: "IPv6" })
|
|
|
|
const address = ensureAddress(mockServer, "http")
|
|
|
|
expect(address.toString()).toBe(`http://[a:b:c:d::1234]:5678/`)
|
2021-09-04 02:41:42 +08:00
|
|
|
})
|
|
|
|
})
|
2021-09-14 05:35:12 +08:00
|
|
|
|
|
|
|
describe("handleArgsSocketCatchError", () => {
|
2021-12-18 03:06:52 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
mockLogger()
|
2021-09-14 05:35:12 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should log an error if its not an NodeJS.ErrnoException", () => {
|
2022-03-12 07:54:59 +08:00
|
|
|
const message = "other message"
|
|
|
|
const error = new Error(message)
|
2021-09-14 05:35:12 +08:00
|
|
|
|
2022-03-12 07:54:59 +08:00
|
|
|
expect(() => {
|
|
|
|
handleArgsSocketCatchError(error)
|
|
|
|
}).toThrowError(error)
|
2021-09-14 05:35:12 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should log an error if its not an NodeJS.ErrnoException (and the error has a message)", () => {
|
|
|
|
const errorMessage = "handleArgsSocketCatchError Error"
|
|
|
|
const error = new Error(errorMessage)
|
|
|
|
|
2022-03-12 07:54:59 +08:00
|
|
|
expect(() => {
|
|
|
|
handleArgsSocketCatchError(error)
|
|
|
|
}).toThrowError(error)
|
2021-09-14 05:35:12 +08:00
|
|
|
})
|
|
|
|
|
2022-03-12 07:54:59 +08:00
|
|
|
it("should not log an error if its a NodeJS.ErrnoException", () => {
|
|
|
|
const code = "ENOENT"
|
|
|
|
const error: NodeJS.ErrnoException = new Error(code)
|
|
|
|
error.code = code
|
2021-09-14 05:35:12 +08:00
|
|
|
|
|
|
|
handleArgsSocketCatchError(error)
|
|
|
|
|
2022-03-12 07:54:59 +08:00
|
|
|
expect(() => {
|
|
|
|
handleArgsSocketCatchError(error)
|
|
|
|
}).not.toThrowError()
|
2021-09-14 05:35:12 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should log an error if the code is not ENOENT (and the error has a message)", () => {
|
|
|
|
const errorMessage = "no access"
|
|
|
|
const error: NodeJS.ErrnoException = new Error()
|
|
|
|
error.code = "EACCESS"
|
|
|
|
error.message = errorMessage
|
|
|
|
|
2022-03-12 07:54:59 +08:00
|
|
|
expect(() => {
|
|
|
|
handleArgsSocketCatchError(error)
|
|
|
|
}).toThrowError(error)
|
2021-09-14 05:35:12 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should log an error if the code is not ENOENT", () => {
|
2022-03-12 07:54:59 +08:00
|
|
|
const code = "EACCESS"
|
|
|
|
const error: NodeJS.ErrnoException = new Error(code)
|
|
|
|
error.code = code
|
2021-09-14 05:35:12 +08:00
|
|
|
|
2022-03-12 07:54:59 +08:00
|
|
|
expect(() => {
|
|
|
|
handleArgsSocketCatchError(error)
|
|
|
|
}).toThrowError(error)
|
|
|
|
})
|
|
|
|
})
|
2021-09-14 05:35:12 +08:00
|
|
|
|
2022-03-12 07:54:59 +08:00
|
|
|
describe("listen", () => {
|
|
|
|
let tmpDirPath: string
|
|
|
|
let mockServer: http.Server
|
|
|
|
|
|
|
|
const testName = "listen"
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
await clean(testName)
|
|
|
|
mockLogger()
|
|
|
|
tmpDirPath = await tmpdir(testName)
|
|
|
|
mockServer = http.createServer()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mockServer.close()
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should throw an error if a directory is passed in instead of a file", async () => {
|
|
|
|
const errorMessage = "EISDIR: illegal operation on a directory, unlink"
|
|
|
|
const port = await getAvailablePort()
|
|
|
|
const mockArgs = { port, host: "0.0.0.0", socket: tmpDirPath }
|
|
|
|
|
|
|
|
try {
|
|
|
|
await listen(mockServer, mockArgs)
|
|
|
|
} catch (error) {
|
|
|
|
expect(error).toBeInstanceOf(Error)
|
|
|
|
expect((error as any).message).toMatch(errorMessage)
|
|
|
|
}
|
2021-09-14 05:35:12 +08:00
|
|
|
})
|
|
|
|
})
|