Skip to content

Commit

Permalink
feat(usebruno#2187): Add Headers and Response to JUnit Reports
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinfrankeengram committed Apr 27, 2024
1 parent c17e4ef commit 96f576c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
25 changes: 24 additions & 1 deletion packages/bruno-cli/src/reporters/junit.js
Expand Up @@ -23,9 +23,32 @@ const makeJUnitOutput = async (results, outputPath) => {
'@timestamp': new Date().toISOString().split('Z')[0],
'@hostname': os.hostname(),
'@time': result.runtime.toFixed(3),
'system-out': { '#cdata': `> ${result.request.method} ${result.request.url}\n` },
testcase: []
};

if (result.request.headers) {
Object.entries(result.request.headers).forEach((header) => {
suite['system-out']['#cdata'] += `> ${header[0]}: ${header[1]}\n`;
});
}
suite['system-out']['#cdata'] += '>\n';

if (result.response && result.response.status) {
suite['system-out']['#cdata'] += `< ${result.response.status} ${result.response.statusText}\n`;
if (result.response.headers) {
Object.entries(result.response.headers).forEach((header) => {
suite['system-out']['#cdata'] += `< ${header[0]}: ${header[1]}\n`;
});
}
suite['system-out']['#cdata'] += '<\n';

if (result.response.data) {
suite['system-out']['#cdata'] +=
typeof result.response.data === 'string' ? result.response.data : JSON.stringify(result.response.data);
}
}

result.assertionResults &&
result.assertionResults.forEach((assertion) => {
const testcase = {
Expand Down Expand Up @@ -79,7 +102,7 @@ const makeJUnitOutput = async (results, outputPath) => {
output.testsuites.testsuite.push(suite);
});

fs.writeFileSync(outputPath, xmlbuilder.create(output).end({ pretty: true }));
fs.writeFileSync(outputPath, xmlbuilder.create(output, { invalidCharReplacement: ' ' }).end({ pretty: true }));
};

module.exports = makeJUnitOutput;
22 changes: 21 additions & 1 deletion packages/bruno-cli/tests/reporters/junit.spec.js
Expand Up @@ -25,7 +25,18 @@ describe('makeJUnitOutput', () => {
suitename: 'Tests/Suite A',
request: {
method: 'GET',
url: 'https://ima.test'
url: 'https://ima.test',
headers: {
Authorization: 'Bearer your_secret_token'
}
},
response: {
status: 200,
statusText: 'OK',
headers: {
'Content-Type': 'text/plain; charset=utf-8'
},
data: 'I am the response content'
},
assertionResults: [
{
Expand Down Expand Up @@ -73,6 +84,15 @@ describe('makeJUnitOutput', () => {

expect(junit.testsuites).toBeDefined;
expect(junit.testsuites.testsuite.length).toBe(2);
expect(junit.testsuites.testsuite[0]['system-out']['#cdata']).toContain('> GET https://ima.test');
expect(junit.testsuites.testsuite[0]['system-out']['#cdata']).toContain(
'> Authorization: Bearer your_secret_token'
);
expect(junit.testsuites.testsuite[0]['system-out']['#cdata']).toContain('< 200 OK');
expect(junit.testsuites.testsuite[0]['system-out']['#cdata']).toContain(
'< Content-Type: text/plain; charset=utf-8'
);
expect(junit.testsuites.testsuite[0]['system-out']['#cdata']).toContain('I am the response content');
expect(junit.testsuites.testsuite[0].testcase.length).toBe(2);
expect(junit.testsuites.testsuite[1].testcase.length).toBe(2);

Expand Down

0 comments on commit 96f576c

Please sign in to comment.