11import express from "express"
2+ import { UserProvidedArgs , setDefaults } from "../../../../src/node/cli"
23import { errorHandler } from "../../../../src/node/routes/errors"
34
45describe ( "error page is rendered for text/html requests" , ( ) => {
@@ -9,7 +10,7 @@ describe("error page is rendered for text/html requests", () => {
910 statusCode : 404 ,
1011 message : ";>hello<script>alert(1)</script>" ,
1112 }
12- const req = createRequest ( )
13+ const req = await createRequest ( )
1314 const res = {
1415 status : jest . fn ( ) . mockReturnValue ( this ) ,
1516 send : jest . fn ( ) . mockReturnValue ( this ) ,
@@ -20,9 +21,41 @@ describe("error page is rendered for text/html requests", () => {
2021 expect ( res . status ) . toHaveBeenCalledWith ( 404 )
2122 expect ( res . send ) . toHaveBeenCalledWith ( expect . not . stringContaining ( "<script>" ) )
2223 } )
24+
25+ it ( "should use custom app-name in error page title" , async ( ) => {
26+ const err = {
27+ statusCode : 404 ,
28+ message : "Not found" ,
29+ }
30+ const req = await createRequest ( { "app-name" : "MyCodeServer" } )
31+ const res = {
32+ status : jest . fn ( ) . mockReturnValue ( this ) ,
33+ send : jest . fn ( ) . mockReturnValue ( this ) ,
34+ set : jest . fn ( ) . mockReturnValue ( this ) ,
35+ } as unknown as express . Response
36+
37+ await errorHandler ( err , req , res , jest . fn ( ) )
38+ expect ( res . send ) . toHaveBeenCalledWith ( expect . stringContaining ( "<title>404 - MyCodeServer</title>" ) )
39+ } )
40+
41+ it ( "should use default 'code-server' when app-name is not set" , async ( ) => {
42+ const err = {
43+ statusCode : 500 ,
44+ message : "Internal error" ,
45+ }
46+ const req = await createRequest ( )
47+ const res = {
48+ status : jest . fn ( ) . mockReturnValue ( this ) ,
49+ send : jest . fn ( ) . mockReturnValue ( this ) ,
50+ set : jest . fn ( ) . mockReturnValue ( this ) ,
51+ } as unknown as express . Response
52+
53+ await errorHandler ( err , req , res , jest . fn ( ) )
54+ expect ( res . send ) . toHaveBeenCalledWith ( expect . stringContaining ( "<title>500 - code-server</title>" ) )
55+ } )
2356} )
2457
25- function createRequest ( ) : express . Request {
58+ async function createRequest ( args : UserProvidedArgs = { } ) : Promise < express . Request > {
2659 return {
2760 headers : {
2861 accept : [ "text/html" ] ,
@@ -31,5 +64,6 @@ function createRequest(): express.Request {
3164 query : {
3265 to : "test" ,
3366 } ,
67+ args : await setDefaults ( args ) ,
3468 } as unknown as express . Request
3569}
0 commit comments