mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-16 04:11:29 +00:00
* Fix: Revert selective JSON parsing where string response is not parsed - Revert "Merge pull request #3706 from Pragadesh-45/fix/response-format-updates" -e897dc1eb0- Revert "Merge pull request #3676 from pooja-bruno/fix/string-json-response" -1f2bee1f90* Fix: Revert interpreting Assert RHS-value wrapped in quotes literally - Revert "Merge pull request #3806 from Pragadesh-45/fix/handle-assert-results" -63d3cb380d- Revert "Merge pull request #3805 from Pragadesh-45/fix/handle-assert-results" -6abd063749* Fix: Inconsistent JSON formatting in preview when encoded value is a string * Fix: Prettify JSON for Res-preview without parsing to avoid JS specific roundings * Fix(testbench): req.body is always Buffer after the binary req body related changes * Added `/api/echo/custom` where response can be configured using request itself * Added tests for validating Assert and Response-preview Co-authored-by: Pragadesh-45 <temporaryg7904@gmail.com> * Handle char-encoding in Response-preview and added more tests * Updated API endpoint in tests to use httpfaker api * QuickJS (Safe Mode) exec logic to handle template literals similar to Developer Mode * Safe Mode bru.runRequest to return statusText similar to Developer Mode --------- Co-authored-by: ramki-bruno <ramki@usebruno.com> Co-authored-by: Anoop M D <anoop.md1421@gmail.com>
50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
const iconv = require('iconv-lite');
|
|
|
|
const lpad = (str, width) => {
|
|
let paddedStr = str;
|
|
while (paddedStr.length < width) {
|
|
paddedStr = ' ' + paddedStr;
|
|
}
|
|
return paddedStr;
|
|
};
|
|
|
|
const rpad = (str, width) => {
|
|
let paddedStr = str;
|
|
while (paddedStr.length < width) {
|
|
paddedStr = paddedStr + ' ';
|
|
}
|
|
return paddedStr;
|
|
};
|
|
|
|
const parseDataFromResponse = (response, disableParsingResponseJson = false) => {
|
|
// Parse the charset from content type: https://stackoverflow.com/a/33192813
|
|
const charsetMatch = /charset=([^()<>@,;:"/[\]?.=\s]*)/i.exec(response.headers['content-type'] || '');
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec#using_exec_with_regexp_literals
|
|
const charsetValue = charsetMatch?.[1];
|
|
const dataBuffer = Buffer.from(response.data);
|
|
// Overwrite the original data for backwards compatibility
|
|
let data;
|
|
if (iconv.encodingExists(charsetValue)) {
|
|
data = iconv.decode(dataBuffer, charsetValue);
|
|
} else {
|
|
data = iconv.decode(dataBuffer, 'utf-8');
|
|
}
|
|
// Try to parse response to JSON, this can quietly fail
|
|
try {
|
|
// Filter out ZWNBSP character
|
|
// https://gist.github.com/antic183/619f42b559b78028d1fe9e7ae8a1352d
|
|
data = data.replace(/^\uFEFF/, '');
|
|
if (!disableParsingResponseJson) {
|
|
data = JSON.parse(data);
|
|
}
|
|
} catch { }
|
|
|
|
return { data, dataBuffer };
|
|
};
|
|
|
|
module.exports = {
|
|
lpad,
|
|
rpad,
|
|
parseDataFromResponse
|
|
};
|