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

fix(skytrace): send parsed json body correctly to expect plugin #1955

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions packages/skytrace/src/commands/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ const VERBS = [
'trace'
];

const _parseValidJson = (jsonString: string, shouldError = true) => {
try {
return JSON.parse(jsonString)
} catch (err) {
console.error(chalk.red(`Invalid JSON: ${err.message}`));
if (shouldError) {
process.exit(1);
} else {
return undefined
}
}
}

class PingCommand extends Command {
static aliases = ['probe', 'http'];
// Enable multiple args:
Expand Down Expand Up @@ -430,8 +443,18 @@ class PingCommand extends Command {
this.log(`\n${chalk.cyan('Body')} stored in: ${fn}\n`);
}

const isJSON = contentType.match(/json/gi);
const isXML = contentType.match(/html/gi) || contentType.match(/xml/gi);
const isJSON = /json/gi.test(contentType)
const isXML = /html/gi.test(contentType) || /xml/gi.test(contentType);

let validatedBody;

if (flags.jmespath || (isJSON && flags.q)) {
validatedBody = _parseValidJson(context.vars.body)
} else if (isJSON) {
validatedBody = _parseValidJson(context.vars.body, false)
} else {
validatedBody = context.vars.body
}

if (flags.showBody) {
let language;
Expand All @@ -443,21 +466,20 @@ class PingCommand extends Command {
}

if (language) {
let output = context.vars.body;
let output = validatedBody;
if (language === 'json' && flags.pretty) {
output = JSON.stringify(JSON.parse(context.vars.body), null, 4);
output = JSON.stringify(_parseValidJson(context.vars.body), null, 4);
}
this.log(highlight(output, { language }));
} else {
this.log(context.vars.body);
this.log(validatedBody);
}
}

if (flags.jmespath || flags.cheerio || flags.q) {
if (flags.jmespath || (isJSON && flags.q)) {
try {
const json = JSON.parse(context.vars.body);
const result = jmespath.search(json, flags.jmespath || flags.query);
const result = jmespath.search(validatedBody, flags.jmespath || flags.query);

// If our output is piped we want to print the JSON without highlighting:
if (process.stdout.isTTY) {
Expand All @@ -473,7 +495,7 @@ class PingCommand extends Command {
}
} else if (flags.cheerio || (isXML && flags.query)) {
try {
const $ = cheerio.load(context.vars.body);
const $ = cheerio.load(validatedBody);
const elts = $(flags.cheerio || flags.query).html();
// If our output is piped we want to print the without highlighting:
if (process.stdout.isTTY) {
Expand All @@ -497,13 +519,12 @@ class PingCommand extends Command {
if (checks.length > 0) {
this.log(chalk.cyan('Expectations:\n'));
let results = [];

for (const ex of checks) {
const checker = Object.keys(ex)[0];
let result = expectations[checker].call(
this,
ex,
context.vars.body,
validatedBody,
context.vars.req,
context.vars.res,
{}
Expand Down