Verified Commit 775919ad authored by Aral Balkan's avatar Aral Balkan
Browse files

Add default 500 error test

parent 151de552
...@@ -42,27 +42,35 @@ test('createServer method', t => { ...@@ -42,27 +42,35 @@ test('createServer method', t => {
}) })
test('serve method default 404', t => { test('serve method default 404 and 500 responses', t => {
// //
// Test default 404 response of the serve method // Test the default 404 and 500 responses of the serve method.
// //
// We rename the /404/ folder so that the custom 404 message is not used // We rename the folders of the custom messages so that they are not used
// and then we rename it back once we’re done. // and we rename them back once we’re done.
// //
t.plan(2) t.plan(4)
const custom404Folder = path.join(__dirname, 'site', '404') const custom404Folder = path.join(__dirname, 'site', '404')
const backup404Folder = path.join(__dirname, 'site', 'backup-404') const backup404Folder = path.join(__dirname, 'site', 'backup-404')
const custom500Folder = path.join(__dirname, 'site', '500')
const backup500Folder = path.join(__dirname, 'site', 'backup-500')
fs.renameSync(custom404Folder, backup404Folder) fs.renameSync(custom404Folder, backup404Folder)
fs.renameSync(custom500Folder, backup500Folder)
const server = webServer.serve({path: 'test/site', callback: async () => { const server = webServer.serve({path: 'test/site', callback: async () => {
// We no longer need to hide the 404 folder. Rename it before // The server is initialised with the default messages. We can now
// more things can go wrong so it doesn’t stay renamed. // rename the folders back.
fs.renameSync(backup404Folder, custom404Folder) fs.renameSync(backup404Folder, custom404Folder)
fs.renameSync(backup500Folder, custom500Folder)
//
// Test default 404 error.
//
let responseDefault404 let responseDefault404
try { try {
responseDefault404 = await secureGet('https://localhost/this-page-does-not-exist') responseDefault404 = await secureGet('https://localhost/this-page-does-not-exist')
...@@ -74,7 +82,24 @@ test('serve method default 404', t => { ...@@ -74,7 +82,24 @@ test('serve method default 404', t => {
const expectedDefault404ResponseBodyDeflated = '<!doctype html><html lang="en" style="font-family: sans-serif; background-color: #eae7e1"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Error 404: Not found</title></head><body style="display: grid; align-items: center; justify-content: center; height: 100vh; vertical-align: top; margin: 0;"><main><h1 style="font-size: 16vw; color: black; text-align:center; line-height: 0.25">4🤭4</h1><p style="font-size: 4vw; text-align: center; padding-left: 2vw; padding-right: 2vw;"><span>Could not find</span> <span style="color: grey;">/this-page-does-not-exist</span></p></main></body></html>'.replace(/\s/g, '') const expectedDefault404ResponseBodyDeflated = '<!doctype html><html lang="en" style="font-family: sans-serif; background-color: #eae7e1"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Error 404: Not found</title></head><body style="display: grid; align-items: center; justify-content: center; height: 100vh; vertical-align: top; margin: 0;"><main><h1 style="font-size: 16vw; color: black; text-align:center; line-height: 0.25">4🤭4</h1><p style="font-size: 4vw; text-align: center; padding-left: 2vw; padding-right: 2vw;"><span>Could not find</span> <span style="color: grey;">/this-page-does-not-exist</span></p></main></body></html>'.replace(/\s/g, '')
t.equal(responseDefault404.statusCode, 404, 'response status code is 404') t.equal(responseDefault404.statusCode, 404, 'response status code is 404')
t.equal(responseDefault404.body.replace(/\s/g, ''), expectedDefault404ResponseBodyDeflated, 'default 404 response body') t.equal(responseDefault404.body.replace(/\s/g, ''), expectedDefault404ResponseBodyDeflated, 'default 404 response body is as expected')
//
// Test default 500 error.
//
let responseDefault500
try {
responseDefault500 = await secureGet('https://localhost/test-500-error')
} catch (error) {
console.log(error)
process.exit(1)
}
const expectedDefault500ResponseBodyDeflated = '<!doctype html><html lang="en" style="font-family: sans-serif; background-color: #eae7e1"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Error 500: Internal Server Error</title></head><body style="display: grid; align-items: center; justify-content: center; height: 100vh; vertical-align: top; margin: 0;"><main><h1 style="font-size: 16vw; color: black; text-align:center; line-height: 0.25">5🔥😱</h1><p style="font-size: 4vw; text-align: center; padding-left: 2vw; padding-right: 2vw;"><span>Internal Server Error</span><br><br><span style="color: grey;">Bad things have happened.</span></p></main></body></html>'.replace(/\s/g, '')
t.equal(responseDefault500.statusCode, 500, 'response status code is 500')
t.equal(responseDefault500.body.replace(/\s/g, ''), expectedDefault500ResponseBodyDeflated, 'default 500 response body is as expected')
t.end() t.end()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment