mirror of
https://github.com/cdr/code-server.git
synced 2026-04-02 03:23:17 +02:00
feat: use --app-name in error page title
- Replace hardcoded 'code-server' with {{APP_NAME}} template in error.html
- Add APP_NAME replacement in errors.ts using req.args['app-name']
- Add tests for custom app-name and default value
This commit is contained in:
parent
62afaf261b
commit
f3ae74b223
3 changed files with 41 additions and 3 deletions
|
|
@ -11,7 +11,7 @@
|
|||
content="style-src 'self'; manifest-src 'self'; img-src 'self' data:; font-src 'self' data:;"
|
||||
/>
|
||||
<meta name="color-scheme" content="light dark" />
|
||||
<title>{{ERROR_TITLE}} - code-server</title>
|
||||
<title>{{ERROR_TITLE}} - {{APP_NAME}}</title>
|
||||
<link rel="icon" href="{{CS_STATIC_BASE}}/src/browser/media/favicon-dark-support.svg" />
|
||||
<link rel="alternate icon" href="{{CS_STATIC_BASE}}/src/browser/media/favicon.ico" />
|
||||
<link rel="manifest" href="/manifest.json" crossorigin="use-credentials" />
|
||||
|
|
|
|||
|
|
@ -57,7 +57,8 @@ export const errorHandler: express.ErrorRequestHandler = async (err, req, res, n
|
|||
replaceTemplates(req, content)
|
||||
.replace(/{{ERROR_TITLE}}/g, statusCode.toString())
|
||||
.replace(/{{ERROR_HEADER}}/g, statusCode.toString())
|
||||
.replace(/{{ERROR_BODY}}/g, escapeHtml(err.message)),
|
||||
.replace(/{{ERROR_BODY}}/g, escapeHtml(err.message))
|
||||
.replace(/{{APP_NAME}}/g, (req.args && req.args["app-name"]) || "code-server"),
|
||||
)
|
||||
} else {
|
||||
res.json({
|
||||
|
|
|
|||
|
|
@ -20,9 +20,45 @@ describe("error page is rendered for text/html requests", () => {
|
|||
expect(res.status).toHaveBeenCalledWith(404)
|
||||
expect(res.send).toHaveBeenCalledWith(expect.not.stringContaining("<script>"))
|
||||
})
|
||||
|
||||
it("should use custom app-name in error page title", async () => {
|
||||
const err = {
|
||||
statusCode: 404,
|
||||
message: "Not found",
|
||||
}
|
||||
const req = createRequest({ "app-name": "MyCodeServer" })
|
||||
const res = {
|
||||
status: jest.fn().mockReturnValue(this),
|
||||
send: jest.fn().mockReturnValue(this),
|
||||
set: jest.fn().mockReturnValue(this),
|
||||
} as unknown as express.Response
|
||||
|
||||
await errorHandler(err, req, res, jest.fn())
|
||||
expect(res.send).toHaveBeenCalledWith(
|
||||
expect.stringContaining("<title>404 - MyCodeServer</title>"),
|
||||
)
|
||||
})
|
||||
|
||||
it("should use default 'code-server' when app-name is not set", async () => {
|
||||
const err = {
|
||||
statusCode: 500,
|
||||
message: "Internal error",
|
||||
}
|
||||
const req = createRequest()
|
||||
const res = {
|
||||
status: jest.fn().mockReturnValue(this),
|
||||
send: jest.fn().mockReturnValue(this),
|
||||
set: jest.fn().mockReturnValue(this),
|
||||
} as unknown as express.Response
|
||||
|
||||
await errorHandler(err, req, res, jest.fn())
|
||||
expect(res.send).toHaveBeenCalledWith(
|
||||
expect.stringContaining("<title>500 - code-server</title>"),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
function createRequest(): express.Request {
|
||||
function createRequest(args?: Record<string, string>): express.Request {
|
||||
return {
|
||||
headers: {
|
||||
accept: ["text/html"],
|
||||
|
|
@ -31,5 +67,6 @@ function createRequest(): express.Request {
|
|||
query: {
|
||||
to: "test",
|
||||
},
|
||||
args: args,
|
||||
} as unknown as express.Request
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue