mock-server: can now emulate errors

This commit is contained in:
liushuyu 2019-12-09 21:08:49 -07:00 committed by James
parent 9fcfe0c77b
commit 8e8d729019

View file

@ -4,7 +4,15 @@ const express = require('express')
const app = express() const app = express()
const port = 3000 const port = 3000
let showError = false
function progressSimulation (res) { 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 progress = 0.0
var timer = setInterval(() => { var timer = setInterval(() => {
var resp = JSON.stringify({ Status: ['Processing...', progress] }) + '\n' var resp = JSON.stringify({ Status: ['Processing...', progress] }) + '\n'
@ -18,6 +26,10 @@ function progressSimulation (res) {
} }
function returnConfig (res) { function returnConfig (res) {
if (showError) {
res.status(500).json({})
return
}
res.json({ res.json({
installing_message: installing_message:
'Test Banner <strong>Bold</strong>&nbsp;<pre>Code block</pre>&nbsp;<i>Italic</i>&nbsp;<del>Strike</del>', 'Test Banner <strong>Bold</strong>&nbsp;<pre>Code block</pre>&nbsp;<i>Italic</i>&nbsp;<del>Strike</del>',
@ -52,6 +64,7 @@ function returnConfig (res) {
} }
app.get('/api/attrs', (req, res) => { app.get('/api/attrs', (req, res) => {
console.log('-- Get attrs')
res.send( res.send(
{ name: 'yuzu', target_url: 'https://raw.githubusercontent.com/j-selby/test-installer/master/config.linux.v2.toml' } { 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) => { app.get('/api/exit', (req, res) => {
console.log('-- Exit') console.log('-- Exit')
if (showError) {
res.status(500).send('Simulated error: Nothing to see here.')
return
}
res.status(204) 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}...`) console.log(`Listening on ${port}...`)
app.listen(port) app.listen(port)