Skip to content

Commit

Permalink
Fixed PR Review comments - #2813
Browse files Browse the repository at this point in the history
- ordered response to always show after request (even if response body is blank)
- used byteLength to determine accurate string length
- reordered the variables up top (it was and still is messy)
  • Loading branch information
shamasis committed Aug 17, 2021
1 parent 852b360 commit fcd18a9
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions lib/reporters/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var _ = require('lodash'),
process: 'process',
total: 'total'
},
BODY_CLIP_SIZE = 2048,

PostmanCLIReporter,
timestamp,
Expand Down Expand Up @@ -213,14 +214,18 @@ PostmanCLIReporter = function (emitter, reporterOptions, options) {
code = res.code,
reason = res.reason(),
mime = res.contentInfo() || {},
timings = _.last(_.get(o, 'history.execution.data')),

reqHeadersLen = _.get(req, 'headers.members.length'),
resHeadersLen = _.get(res, 'headers.members.length'),

resTime = util.prettyms(res.responseTime || 0),
timings = _.last(_.get(o, 'history.execution.data')),
resText = options.verbose ? res.text() : E,

reqText = (options.verbose && req.body) ? req.body.toString() : E,
reqTextLen = req.size().body || Buffer.byteLength(reqText),

resText = options.verbose ? res.text() : E,
resTextLen = res.size().body || Buffer.byteLength(resText),

reqBodyMode = _.get(req, 'body.mode', ''),
resSummary = [
Expand All @@ -245,43 +250,48 @@ PostmanCLIReporter = function (emitter, reporterOptions, options) {
`${_.get(res, 'cookies.members.length')} ${colors.gray('cookies')}`
].join(` ${colors.gray(symbols.star)} `));


// print the line of response body meta one liner if there is no response body
// if there is one, we would print it across the body braces
if (!resText) {
print.lf(SPC + SPC + resSummary);
}

// print request body
if (reqText) {
if (reqTextLen) {
// truncate very large request (is 2048 large enough?)
if (reqText.length > 2048) {
reqText = reqText.substr(0, 2000) + colors.brightWhite(' (truncated)');
if (reqTextLen > BODY_CLIP_SIZE) {
reqText = reqText.substr(0, BODY_CLIP_SIZE) +
colors.brightWhite(`\n(showing ${util.filesize(BODY_CLIP_SIZE)}/${util.filesize(reqTextLen)})`);
}

reqText = wrap(reqText, ` ${colors.white(symbols.console.middle)} `);
print.buffer(` ${colors.white(symbols.console.top)} ${colors.white(symbols.up)} ${reqBodyMode}\n`,
// eslint-disable-next-line max-len
print.buffer(` ${colors.white(symbols.console.top)} ${colors.white(symbols.up)} ${reqBodyMode} ${colors.gray(symbols.star)} ${util.filesize(reqTextLen)}\n`,
colors.white(` ${symbols.console.bottom}`))
// tweak the message to ensure that its surrounding is not brightly coloured.
// also ensure to remove any blank lines generated due to util.inspect
.nobuffer(colors.gray(reqText.replace(/\n\s*\n/g, LF) + LF));

print.lf(SPC); // visual tweak: flushes out the buffer of wrapping body above
}

// print response body
if (resText) {
print(LF);
if (resTextLen) {
// truncate very large response (is 2048 large enough?)
if (resText.length > 2048) {
resText = resText.substr(0, 2000) + colors.brightWhite(' (truncated)');
if (resTextLen > BODY_CLIP_SIZE) {
resText = resText.substr(0, BODY_CLIP_SIZE) +
colors.brightWhite(`\n(showing ${util.filesize(BODY_CLIP_SIZE)}/${util.filesize(resTextLen)})`);
}

resText = wrap(resText, ` ${colors.white(symbols.console.middle)} `);
print.buffer(` ${colors.white(symbols.console.top)} ${colors.white(symbols.down)} ${resSummary}\n`,
// eslint-disable-next-line max-len
print.buffer(` ${colors.white(symbols.console.top)} ${colors.white(symbols.down)} ${resSummary} ${colors.gray(symbols.star)} ${util.filesize(resTextLen)}\n`,
colors.white(` ${symbols.console.bottom}`))
// tweak the message to ensure that its surrounding is not brightly coloured.
// also ensure to remove any blank lines generated due to util.inspect
.nobuffer(colors.gray(resText.replace(/\n\s*\n/g, LF) + LF));
}
// print the line of response body meta one liner if there is no response body
// if there is one, we would already print it across the body braces above.
else {
// we need to do some newline related shenanigans here so that the output looks clean
// in the absence of the request body block
print.lf(` ${symbols.down} ${resSummary}`);
}

// print timing info of the request
timings = timings && timings.timings; // if there are redirects, get timings for the last request sent
Expand Down

0 comments on commit fcd18a9

Please sign in to comment.