Skip to content

Commit

Permalink
🔊[e2e] output server response in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan committed Jan 8, 2020
1 parent 23669d0 commit d87e457
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
11 changes: 8 additions & 3 deletions test/server/fake-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ module.exports = (app) => {
req.body.split('\n').forEach((log) => logs.push(JSON.parse(log)))
res.send('ok')
})
app.get('/logs', (req, res) => res.send(logs))
app.get('/logs', (req, res) => send(res, logs))

app.post('/rum', (req, res) => {
req.body.split('\n').forEach((rumEvent) => rumEvents.push(JSON.parse(rumEvent)))
res.send('ok')
})
app.get('/rum', (req, res) => res.send(rumEvents))
app.get('/rum', (req, res) => send(res, rumEvents))

app.post('/monitoring', (req, res) => {
monitoring.push(JSON.parse(req.body))
res.send('ok')
})
app.get('/monitoring', (req, res) => res.send(monitoring))
app.get('/monitoring', (req, res) => send(res, monitoring))

app.get('/reset', (req, res) => {
logs = []
Expand All @@ -36,3 +36,8 @@ module.exports = (app) => {
res.send('ok')
})
}

function send(res, data) {
res.body = JSON.stringify(data)
res.send(data)
}
15 changes: 12 additions & 3 deletions test/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if (process.env.ENV === 'development') {
app.use(middleware(webpack(withBuildEnv(logsConfig(null, { mode: 'development' })))))
} else {
// e2e tests
morgan.token('body', (req) => hasValidBody(req) && `\n${req.body}`)
morgan.token('body', (req, res) => extractBody(req, res))
const stream = fs.createWriteStream(path.join(__dirname, 'test-server.log'))
app.use(morgan(':method :url :body', { stream }))
app.use(express.static(path.join(__dirname, '../../packages/logs/bundle')))
Expand Down Expand Up @@ -53,6 +53,15 @@ function withBuildEnv(webpackConf) {
return webpackConf
}

function hasValidBody(req) {
return req.body && JSON.stringify(req.body) !== '{}'
function extractBody(req, res) {
if (isValidBody(req.body)) {
return `\n${req.body}`
}
if (isValidBody(res.body)) {
return `\n${res.body}`
}
}

function isValidBody(body) {
return body && JSON.stringify(body) !== '{}'
}

0 comments on commit d87e457

Please sign in to comment.