2020-10-30 08:18:59 +01:00
|
|
|
import { logger } from "@coder/logger"
|
2020-11-06 20:46:49 +01:00
|
|
|
import * as express from "express"
|
|
|
|
import * as fs from "fs"
|
2020-10-30 08:26:30 +01:00
|
|
|
import * as path from "path"
|
2021-07-28 01:52:57 +02:00
|
|
|
import { HttpCode } from "../../../src/common/http"
|
|
|
|
import { AuthType } from "../../../src/node/cli"
|
|
|
|
import { codeServer, PluginAPI } from "../../../src/node/plugin"
|
|
|
|
import * as apps from "../../../src/node/routes/apps"
|
|
|
|
import * as httpserver from "../../utils/httpserver"
|
2020-11-06 16:09:35 +01:00
|
|
|
const fsp = fs.promises
|
2020-10-30 08:18:59 +01:00
|
|
|
|
2021-02-09 22:23:08 +01:00
|
|
|
// Jest overrides `require` so our usual override doesn't work.
|
2021-02-10 20:13:23 +01:00
|
|
|
jest.mock("code-server", () => codeServer, { virtual: true })
|
2021-02-09 22:23:08 +01:00
|
|
|
|
2020-10-30 08:18:59 +01:00
|
|
|
/**
|
|
|
|
* Use $LOG_LEVEL=debug to see debug logs.
|
|
|
|
*/
|
|
|
|
describe("plugin", () => {
|
2020-11-06 15:51:46 +01:00
|
|
|
let papi: PluginAPI
|
2021-01-14 15:53:34 +01:00
|
|
|
let s: httpserver.HttpServer
|
2020-11-06 15:51:46 +01:00
|
|
|
|
2021-01-21 00:37:49 +01:00
|
|
|
beforeAll(async () => {
|
2021-01-19 23:44:42 +01:00
|
|
|
// Only include the test plugin to avoid contaminating results with other
|
|
|
|
// plugins that might be on the filesystem.
|
|
|
|
papi = new PluginAPI(logger, `${path.resolve(__dirname, "test-plugin")}:meow`, "")
|
|
|
|
await papi.loadPlugins(false)
|
2020-10-30 08:18:59 +01:00
|
|
|
|
2021-01-14 15:37:41 +01:00
|
|
|
const app = express.default()
|
2021-01-20 21:11:08 +01:00
|
|
|
const wsApp = express.default()
|
2021-02-12 23:04:48 +01:00
|
|
|
|
|
|
|
const common: express.RequestHandler = (req, _, next) => {
|
|
|
|
// Routes might use these arguments.
|
|
|
|
req.args = {
|
|
|
|
_: [],
|
|
|
|
auth: AuthType.None,
|
|
|
|
host: "localhost",
|
|
|
|
port: 8080,
|
|
|
|
"proxy-domain": [],
|
|
|
|
config: "~/.config/code-server/config.yaml",
|
|
|
|
verbose: false,
|
|
|
|
usingEnvPassword: false,
|
|
|
|
usingEnvHashedPassword: false,
|
|
|
|
"extensions-dir": "",
|
|
|
|
"user-data-dir": "",
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
|
|
|
|
app.use(common)
|
|
|
|
wsApp.use(common)
|
|
|
|
|
2021-01-20 21:11:08 +01:00
|
|
|
papi.mount(app, wsApp)
|
2020-11-06 15:51:46 +01:00
|
|
|
app.use("/api/applications", apps.router(papi))
|
|
|
|
|
2021-01-14 15:53:34 +01:00
|
|
|
s = new httpserver.HttpServer()
|
2021-01-14 15:37:41 +01:00
|
|
|
await s.listen(app)
|
2021-01-20 21:11:08 +01:00
|
|
|
s.listenUpgrade(wsApp)
|
2021-01-14 15:37:41 +01:00
|
|
|
})
|
|
|
|
|
2021-01-21 00:37:49 +01:00
|
|
|
afterAll(async () => {
|
2021-10-28 22:27:17 +02:00
|
|
|
await s.dispose()
|
2020-11-06 15:51:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("/api/applications", async () => {
|
2021-01-14 15:37:41 +01:00
|
|
|
const resp = await s.fetch("/api/applications")
|
2021-01-21 00:37:49 +01:00
|
|
|
expect(resp.status).toBe(200)
|
2021-01-14 15:37:41 +01:00
|
|
|
const body = await resp.json()
|
|
|
|
logger.debug(`${JSON.stringify(body)}`)
|
2021-01-21 00:37:49 +01:00
|
|
|
expect(body).toStrictEqual([
|
2020-11-06 15:51:46 +01:00
|
|
|
{
|
|
|
|
name: "Test App",
|
2022-01-04 18:27:51 +01:00
|
|
|
version: "4.0.1",
|
2020-11-06 15:51:46 +01:00
|
|
|
|
|
|
|
description: "This app does XYZ.",
|
|
|
|
iconPath: "/test-plugin/test-app/icon.svg",
|
|
|
|
homepageURL: "https://example.com",
|
|
|
|
path: "/test-plugin/test-app",
|
2020-10-30 08:18:59 +01:00
|
|
|
|
2020-11-06 15:51:46 +01:00
|
|
|
plugin: {
|
|
|
|
name: "test-plugin",
|
|
|
|
version: "1.0.0",
|
|
|
|
modulePath: path.join(__dirname, "test-plugin"),
|
2020-11-04 03:11:14 +01:00
|
|
|
|
2020-11-06 15:51:46 +01:00
|
|
|
displayName: "Test Plugin",
|
|
|
|
description: "Plugin used in code-server tests.",
|
|
|
|
routerPath: "/test-plugin",
|
2020-11-04 03:45:25 +01:00
|
|
|
homepageURL: "https://example.com",
|
2020-10-30 08:18:59 +01:00
|
|
|
},
|
2020-11-06 15:51:46 +01:00
|
|
|
},
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
it("/test-plugin/test-app", async () => {
|
2020-11-06 16:09:35 +01:00
|
|
|
const indexHTML = await fsp.readFile(path.join(__dirname, "test-plugin/public/index.html"), {
|
|
|
|
encoding: "utf8",
|
|
|
|
})
|
2021-01-14 15:37:41 +01:00
|
|
|
const resp = await s.fetch("/test-plugin/test-app")
|
2021-01-21 00:37:49 +01:00
|
|
|
expect(resp.status).toBe(200)
|
2021-01-14 15:37:41 +01:00
|
|
|
const body = await resp.text()
|
2021-01-21 00:37:49 +01:00
|
|
|
expect(body).toBe(indexHTML)
|
2020-10-30 08:18:59 +01:00
|
|
|
})
|
2021-01-20 21:11:08 +01:00
|
|
|
|
|
|
|
it("/test-plugin/test-app (websocket)", async () => {
|
|
|
|
const ws = s.ws("/test-plugin/test-app")
|
|
|
|
const message = await new Promise((resolve) => {
|
|
|
|
ws.once("message", (message) => resolve(message))
|
|
|
|
})
|
|
|
|
ws.terminate()
|
|
|
|
expect(message).toBe("hello")
|
|
|
|
})
|
2021-01-30 00:42:50 +01:00
|
|
|
|
|
|
|
it("/test-plugin/error", async () => {
|
|
|
|
const resp = await s.fetch("/test-plugin/error")
|
|
|
|
expect(resp.status).toBe(HttpCode.LargePayload)
|
|
|
|
})
|
2020-10-30 08:18:59 +01:00
|
|
|
})
|