Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flag to set verbose output size limit for newman run #2883

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ newman.run({
"_postman_exported_at": "2016-10-17T14:31:26.200Z",
"_postman_exported_using": "Postman/4.8.0"
},
globalVar: [
globalVar: [
{ "key":"glboalSecret", "value":"globalSecretValue" },
{ "key":"globalAnotherSecret", "value":`${process.env.GLOBAL_ANOTHER_SECRET}`}
],
Expand All @@ -367,7 +367,7 @@ newman.run({
"_postman_exported_at": "2016-10-17T14:26:34.940Z",
"_postman_exported_using": "Postman/4.8.0"
},
envVar: [
envVar: [
{ "key":"secret", "value":"secretValue" },
{ "key":"anotherSecret", "value":`${process.env.ANOTHER_SECRET}`}
],
Expand Down Expand Up @@ -445,14 +445,14 @@ such a scenario.
| CLI Option | Description |
|-------------|-------------------|
| `--reporter-cli-silent` | The CLI reporter is internally disabled and you see no output to terminal. |

| `--reporter-cli-show-timestamps` | This prints the local time for each request made. |
| `--reporter-cli-show-timestamps` | This prints the local time for each request made. |
| `--reporter-cli-no-summary` | The statistical summary table is not shown. |
| `--reporter-cli-no-failures` | This prevents the run failures from being separately printed. |
| `--reporter-cli-no-assertions` | This turns off the output for request-wise assertions as they happen. |
| `--reporter-cli-no-success-assertions` | This turns off the output for successful assertions as they happen. |
| `--reporter-cli-no-console` | This turns off the output of `console.log` (and other console calls) from collection's scripts. |
| `--reporter-cli-no-banner` | This turns off the `newman` banner shown at the beginning of each collection run. |
| `--reporter-cli-truncate-body-output <size in any unit>` | Specify the truncation size of body in verbose CLI output for requests and responses. You can pass inputs like `1KB`, `1024`, `2.5kb`, etc. By default, the truncation size is set to `2KB`. Pass `infinity` to remove any truncation size limit of verbose CLI output body. |

### JSON Reporter
The built-in JSON reporter is useful in producing a comprehensive output of the run summary. It takes the path to the
Expand Down
1 change: 1 addition & 0 deletions bin/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
return num.valueOf();
},


/**
* Helper for collecting argument passed multiple times.
*
Expand Down
40 changes: 29 additions & 11 deletions lib/reporters/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var _ = require('lodash'),
sdk = require('postman-collection'),
colors = require('colors/safe'),
Table = require('cli-table3'),
bytes = require('bytes'),
format = require('util').format,

util = require('../../util'),
Expand Down Expand Up @@ -70,6 +71,7 @@ extractSNR = function (executions) {
* @param {Boolean=} reporterOptions.noFailures - Boolean flag to turn off failure reporting altogether, if set to true.
* @param {Boolean=} reporterOptions.noConsole - Boolean flag to turn off console logging, if set to true.
* @param {Boolean=} reporterOptions.noBanner - Boolean flag to turn off newman banner, if set to true.
* @param {String=} reporterOptions.truncateBodyOutput - Specify the truncation size of body in verbose CLI output.
* @param {Object} options - A set of generic collection run options.
* @returns {*}
*/
Expand Down Expand Up @@ -233,9 +235,24 @@ PostmanCLIReporter = function (emitter, reporterOptions, options) {
`${mime.mimeType}`,
`${mime.mimeFormat}`,
`${mime.charset}`
].join(` ${colors.gray(symbols.star)} `);

].join(` ${colors.gray(symbols.star)} `),
// eslint-disable-next-line max-len
bodyClipSize = BODY_CLIP_SIZE;

if (reporterOptions.truncateBodyOutput) {
// if infinite is passed to truncate-body-output
if (reporterOptions.truncateBodyOutput.toLowerCase() === 'infinity') {
bodyClipSize = Number.POSITIVE_INFINITY;
}
else {
bodyClipSize = bytes.parse(reporterOptions.truncateBodyOutput);
}
}
if (isNaN(bodyClipSize)) {
// eslint-disable-next-line max-len
print(LF + 'Invalid value passed to --reporter-cli-truncate-body-output flag, defaulting to 2KB. You can pass values like 1KB, 1024, 2.5kb or infinite. For more info, view https://github.com/visionmedia/bytes.js' + LF);
bodyClipSize = 2048; // If invalid size, set to 2048 B
}
print.lf(SPC); // also flushes out the circling progress icon

// for clean readability of code. this section compiles the cli string for one line of
Expand All @@ -249,13 +266,14 @@ PostmanCLIReporter = function (emitter, reporterOptions, options) {
`${resHeadersLen}${colors.gray(symbols.down)} ${colors.gray('headers')}`,
`${_.get(res, 'cookies.members.length')} ${colors.gray('cookies')}`
].join(` ${colors.gray(symbols.star)} `));

// print request body

print.lf('Req Text Len : ', req.size());
if (reqTextLen) {
// truncate very large request (is 2048 large enough?)
if (reqTextLen > BODY_CLIP_SIZE) {
reqText = reqText.substr(0, BODY_CLIP_SIZE) +
colors.brightWhite(`\n(showing ${util.filesize(BODY_CLIP_SIZE)}/${util.filesize(reqTextLen)})`);
// truncate very large request
if (reqTextLen > bodyClipSize) {
reqText = reqText.substr(0, bodyClipSize) +
colors.brightWhite(`\n(showing ${util.filesize(bodyClipSize)}/${util.filesize(reqTextLen)})`);
}

reqText = wrap(reqText, ` ${colors.white(symbols.console.middle)} `);
Expand All @@ -271,10 +289,10 @@ PostmanCLIReporter = function (emitter, reporterOptions, options) {

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

resText = wrap(resText, ` ${colors.white(symbols.console.middle)} `);
Expand Down
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"license": "Apache-2.0",
"dependencies": {
"async": "3.2.3",
"bytes": "^3.1.1",
"chardet": "1.4.0",
"cli-progress": "3.10.0",
"cli-table3": "0.6.1",
Expand Down
118 changes: 107 additions & 11 deletions test/cli/verbose.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ var _ = require('lodash');

describe('newman run --verbose', function () {
var verboseStrings = [
'prepare',
'wait',
'dns-lookup',
'tcp-handshake',
'ssl-handshake',
'transfer-start',
'download',
'process',
'average DNS lookup time:',
'average first byte time:'
];
'prepare',
'wait',
'dns-lookup',
'tcp-handshake',
'ssl-handshake',
'transfer-start',
'download',
'process',
'average DNS lookup time:',
'average first byte time:'
],
endTags = [
'</body>',
'</html>'
];

it('should include verbose with --verbose option', function (done) {
exec('node ./bin/newman.js run test/fixtures/run/single-get-request.json --verbose', function (code, stdout) {
Expand All @@ -33,4 +37,96 @@ describe('newman run --verbose', function () {
done();
});
});

it('should limit to 2KB with --verbose option only without setting truncate-body-output', function (done) {
// eslint-disable-next-line max-len
exec('node ./bin/newman.js run test/fixtures/run/large-output-get-request.json --verbose', function (_code, stdout) {
_.forEach(endTags, function (str) {
expect(stdout).to.not.contain(str);
});

done();
});
});

it('should log the entire output when infinity is passed to --truncate-body-output', function (done) {
// eslint-disable-next-line max-len
exec('node ./bin/newman.js run test/fixtures/run/large-output-get-request.json --verbose --reporter-cli-truncate-body-output infinity', function (_code, stdout) {
_.forEach(endTags, function (str) {
expect(stdout).to.contain(str);
});

done();
});
});

// eslint-disable-next-line max-len
it('should log the entire output when infinity is passed to --truncate-body-output without considering case', function (done) {
// eslint-disable-next-line max-len
exec('node ./bin/newman.js run test/fixtures/run/large-output-get-request.json --verbose --reporter-cli-truncate-body-output InFiNiTy', function (_code, stdout) {
_.forEach(endTags, function (str) {
expect(stdout).to.contain(str);
});
// eslint-disable-next-line max-len
exec('node ./bin/newman.js run test/fixtures/run/large-output-get-request.json --verbose --reporter-cli-truncate-body-output INFINITY', function (_code, stdout) {
_.forEach(endTags, function (str) {
expect(stdout).to.contain(str);
});

done();
});
});
});

it('should display twice as much as verbose output when limit set to 2KB than 1KB', function (done) {
var output_response_start = '"args"',
output_response_end = '(showing';

// eslint-disable-next-line max-len
exec('node ./bin/newman.js run test/fixtures/run/continuous-data.json --verbose --reporter-cli-truncate-body-output 2kb', function (_code, largeStdout) {
// eslint-disable-next-line max-len
exec('node ./bin/newman.js run test/fixtures/run/continuous-data.json --verbose --reporter-cli-truncate-body-output 1kb', function (_code, smallStdout) {
// eslint-disable-next-line max-len
var largeStdoutResponseOnly = largeStdout.slice(largeStdout.indexOf(output_response_start), largeStdout.lastIndexOf(output_response_end)),
// eslint-disable-next-line max-len
smallStdoutResponseOnly = smallStdout.slice(smallStdout.indexOf(output_response_start), smallStdout.lastIndexOf(output_response_end));

/*
1000-1193 = 193 characters
1000-1398 = 398 ~= 193 * 2 characters
Thus setting output limit to 2kb ensure almost twice output size as compared to 1kb
*/
expect(smallStdoutResponseOnly).to.contain('1193');
expect(largeStdoutResponseOnly).to.contain('1398');
expect(largeStdoutResponseOnly.length).to.be.greaterThan(smallStdoutResponseOnly.length);
// eslint-disable-next-line max-len
expect(Math.abs(largeStdoutResponseOnly.length - smallStdoutResponseOnly.length * 2)).to.be.lessThanOrEqual(1);
done();
});
});
});

it('should display a warning and fallback to 2KB when invalid truncate-body-output is passed', function (done) {
// eslint-disable-next-line max-len
exec('node ./bin/newman.js run test/fixtures/run/large-output-get-request.json --verbose --reporter-cli-truncate-body-output postman', function (_code, stdout) {
expect(stdout).to.contain('Invalid value');
expect(stdout).to.contain('showing 2.05');
done();
});
});

it('should limit both request and response body output when truncate-body-output is passed', function (done) {
var output_response_start = '"args"';

// eslint-disable-next-line max-len
exec('node ./bin/newman.js run test/fixtures/run/continuous-data.json --verbose --reporter-cli-truncate-body-output 0.5kb', function (_code, stdout) {
var responseStartIndex = stdout.indexOf(output_response_start),
request = stdout.slice(0, responseStartIndex),
response = stdout.slice(0, responseStartIndex);

expect(request).to.contain('512B');
expect(response).to.contain('512B');
done();
});
});
});
73 changes: 73 additions & 0 deletions test/fixtures/run/continuous-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"info": {
"_postman_id": "81f0e310-03a1-4897-bc07-f31bd863ec05",
"name": "Continuous Data",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "Continuous Data Test",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
"const ARRAY_SIZE = 1500;",
"const continuousNumberArray = Array(ARRAY_SIZE).fill().map((_element, index) => index + 1000);",
"pm.collectionVariables.set('continuousNumberArray', continuousNumberArray);"
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 200\", function () {",
" pm.response.to.have.status(200);",
"});",
"pm.test(\"Status code name has string OK\", function () {",
" pm.response.to.have.status(\"OK\");",
"});",
"pm.test(\"Body contains string continuousNumberArray\", function () {",
" pm.expect(pm.response.text()).to.include(\"continuousNumberArray\");",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"continuousNumberArray\": {{continuousNumberArray}}\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "https://postman-echo.com/post",
"protocol": "https",
"host": [
"postman-echo",
"com"
],
"path": [
"post"
]
}
},
"response": []
}
],
"variable": [
{
"key": "continuousNumberArray",
"value": ""
}
]
}
18 changes: 18 additions & 0 deletions test/fixtures/run/large-output-get-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"info": {
"_postman_id": "6d0dfccd-5998-43bb-a0a5-f88065c8c5d1",
"name": "Large Output Test for --output-size Flag",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "Postman Get Request",
"request": {
"method": "GET",
"header": [],
"url": "https://www.postman.com/"
},
"response": []
}
]
}