From 8e8d729019c2816f1006e9132b3b7a23c87206da Mon Sep 17 00:00:00 2001 From: liushuyu Date: Mon, 9 Dec 2019 21:08:49 -0700 Subject: [PATCH] mock-server: can now emulate errors --- ui/mock-server.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ui/mock-server.js b/ui/mock-server.js index a82cbb4..3b8e19b 100644 --- a/ui/mock-server.js +++ b/ui/mock-server.js @@ -4,7 +4,15 @@ const express = require('express') const app = express() const port = 3000 +let showError = false + function progressSimulation (res) { + if (showError) { + var resp = JSON.stringify({ Error: 'Simulated error.' }) + '\n' + res.write(resp) + res.status(200).end() + return + } var progress = 0.0 var timer = setInterval(() => { var resp = JSON.stringify({ Status: ['Processing...', progress] }) + '\n' @@ -18,6 +26,10 @@ function progressSimulation (res) { } function returnConfig (res) { + if (showError) { + res.status(500).json({}) + return + } res.json({ installing_message: 'Test Banner Bold 
Code block
 Italic Strike', @@ -52,6 +64,7 @@ function returnConfig (res) { } app.get('/api/attrs', (req, res) => { + console.log('-- Get attrs') res.send( { name: 'yuzu', target_url: 'https://raw.githubusercontent.com/j-selby/test-installer/master/config.linux.v2.toml' } ) @@ -88,8 +101,18 @@ app.post('/api/start-install', (req, res) => { app.get('/api/exit', (req, res) => { console.log('-- Exit') + if (showError) { + res.status(500).send('Simulated error: Nothing to see here.') + return + } res.status(204) }) +app.get('/api/mock_error', (req, res) => { + console.log('-- Toggle error emulation') + showError = !showError + res.status(200).send(`Error emulation: ${showError}\n`) +}) + console.log(`Listening on ${port}...`) app.listen(port)