From 8d6a97100b1fd509f87b8721cb7fdee681f8b50f Mon Sep 17 00:00:00 2001 From: Alban Mouton Date: Wed, 11 Jan 2023 15:39:43 +0100 Subject: [PATCH] feat: simple contains assertions --- config/development.js | 5 ++++- server/index.js | 13 ++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/config/development.js b/config/development.js index d92e98b..d60406c 100644 --- a/config/development.js +++ b/config/development.js @@ -1,6 +1,9 @@ module.exports = { interval: 10, reqs: { - test: 'http://localhost:9090/mocks/1' + test: { + url: 'http://localhost:9090/mocks/1', + contains: ['ok'] + } } } diff --git a/server/index.js b/server/index.js index 71ffe3b..c80a354 100644 --- a/server/index.js +++ b/server/index.js @@ -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) @@ -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 @@ -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) }