Skip to content

Commit

Permalink
feat: simple contains assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
albanm committed Jan 11, 2023
1 parent 33fbc8b commit 8d6a971
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion config/development.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
interval: 10,
reqs: {
test: 'http://localhost:9090/mocks/1'
test: {
url: 'http://localhost:9090/mocks/1',
contains: ['ok']
}
}
}
13 changes: 10 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ axios.interceptors.request.use(config => {
config.headers.common.referer = 'http-req-exporter'
return config
})
axios.interceptors.response.use(response => response.status, error => {
axios.interceptors.response.use(response => response, error => {
console.warn('HTTP request error', error)
if (!error.response) {
console.warn('axios error', error)
Expand All @@ -36,7 +36,7 @@ axios.interceptors.response.use(response => response.status, error => {
}
error.response.message = `${error.response.status} - ${messageText}`
console.warn('HTTP error', error.response)
return error.response.status
return { status: error.response.status }
})

// prepare the prometheus client register
Expand All @@ -59,8 +59,15 @@ const iteration = async () => {
if (running) return // prevent overlapping
running = true
for (const name in reqs) {
const req = typeof reqs[name] === 'string' ? { url: reqs[name] } : reqs[name]
const end = httpReqsHistogram.startTimer()
const status = await axios(reqs[name])
let { status, data } = await axios(req.url)
for (const contain of req.contains || []) {
if (!data.includes(contain)) {
console.warn('missing expected content', req.url, contain)
status = 417
}
}
const seconds = end({ name, status })
debug('req', name, reqs[name], status, seconds)
}
Expand Down

0 comments on commit 8d6a971

Please sign in to comment.