2017-09-22 13:48:47 +00:00
|
|
|
// @flow
|
2017-02-20 18:32:27 +00:00
|
|
|
import jq from 'jsonpath';
|
2017-09-22 13:48:47 +00:00
|
|
|
import * as xpath from '../../common/xpath';
|
|
|
|
import type {ResponseHeader} from '../../models/response';
|
2017-10-10 14:46:32 +00:00
|
|
|
import type {PluginTemplateTag, PluginTemplateTagContext} from './index';
|
|
|
|
import type {NunjucksParsedTagArg} from '../utils';
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-10-10 14:46:32 +00:00
|
|
|
export default ({
|
2017-06-01 02:04:27 +00:00
|
|
|
name: 'response',
|
2017-06-09 01:10:12 +00:00
|
|
|
displayName: 'Response',
|
2017-10-10 14:46:32 +00:00
|
|
|
description: 'reference values from other request\'s responses',
|
2017-06-01 02:04:27 +00:00
|
|
|
args: [
|
|
|
|
{
|
|
|
|
displayName: 'Attribute',
|
|
|
|
type: 'enum',
|
|
|
|
options: [
|
2017-10-10 14:46:32 +00:00
|
|
|
{displayName: 'Body Attribute', description: 'value of response body', value: 'body'},
|
2017-06-01 02:04:27 +00:00
|
|
|
{displayName: 'Raw Body', description: 'entire response body', value: 'raw'},
|
|
|
|
{displayName: 'Header', description: 'value of response header', value: 'header'}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Request',
|
|
|
|
type: 'model',
|
|
|
|
model: 'Request'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'string',
|
2017-10-10 14:46:32 +00:00
|
|
|
hide: (args: Array<NunjucksParsedTagArg>): boolean => args[0].value === 'raw',
|
2017-09-22 13:48:47 +00:00
|
|
|
displayName: (args: Array<Object>): string => {
|
2017-06-01 02:04:27 +00:00
|
|
|
switch (args[0].value) {
|
|
|
|
case 'body':
|
|
|
|
return 'Filter (JSONPath or XPath)';
|
|
|
|
case 'header':
|
|
|
|
return 'Header Name';
|
|
|
|
default :
|
|
|
|
return 'Filter';
|
2017-05-23 22:05:31 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-01 02:04:27 +00:00
|
|
|
}
|
|
|
|
],
|
2017-05-23 22:05:31 +00:00
|
|
|
|
2017-10-10 14:46:32 +00:00
|
|
|
async run (context: PluginTemplateTagContext, field: string, id: string, filter: string) {
|
2017-05-23 22:05:31 +00:00
|
|
|
if (!['body', 'header', 'raw'].includes(field)) {
|
2017-02-27 21:00:13 +00:00
|
|
|
throw new Error(`Invalid response field ${field}`);
|
|
|
|
}
|
|
|
|
|
2017-10-10 14:46:32 +00:00
|
|
|
if (!id) {
|
|
|
|
throw new Error('No request specified');
|
|
|
|
}
|
|
|
|
|
2017-06-09 01:10:12 +00:00
|
|
|
const request = await context.util.models.request.getById(id);
|
2017-02-20 18:32:27 +00:00
|
|
|
if (!request) {
|
2017-02-27 21:00:13 +00:00
|
|
|
throw new Error(`Could not find request ${id}`);
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 01:10:12 +00:00
|
|
|
const response = await context.util.models.response.getLatestForRequestId(id);
|
2017-02-20 18:32:27 +00:00
|
|
|
|
|
|
|
if (!response) {
|
2017-05-23 22:05:31 +00:00
|
|
|
throw new Error('No responses for request');
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 18:20:47 +00:00
|
|
|
if (!response.statusCode) {
|
2017-10-10 17:54:42 +00:00
|
|
|
throw new Error('No successful responses for request');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (field !== 'raw' && !filter) {
|
|
|
|
throw new Error(`No ${field} filter specified`);
|
2017-05-23 22:05:31 +00:00
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-05-23 22:05:31 +00:00
|
|
|
const sanitizedFilter = filter.trim();
|
|
|
|
|
|
|
|
if (field === 'header') {
|
2017-06-01 02:04:27 +00:00
|
|
|
return matchHeader(response.headers, sanitizedFilter);
|
2017-05-23 22:05:31 +00:00
|
|
|
} else if (field === 'raw') {
|
2017-06-30 03:30:22 +00:00
|
|
|
return context.util.models.response.getBodyBuffer(response, '').toString();
|
2017-05-23 22:05:31 +00:00
|
|
|
} else if (field === 'body') {
|
2017-06-30 03:30:22 +00:00
|
|
|
const bodyStr = context.util.models.response.getBodyBuffer(response, '').toString();
|
2017-05-23 22:05:31 +00:00
|
|
|
|
|
|
|
if (sanitizedFilter.indexOf('$') === 0) {
|
2017-06-01 02:04:27 +00:00
|
|
|
return matchJSONPath(bodyStr, sanitizedFilter);
|
2017-05-23 22:05:31 +00:00
|
|
|
} else if (sanitizedFilter.indexOf('/') === 0) {
|
2017-06-01 02:04:27 +00:00
|
|
|
return matchXPath(bodyStr, sanitizedFilter);
|
2017-05-23 22:05:31 +00:00
|
|
|
} else {
|
|
|
|
throw new Error(`Invalid format for response query: ${sanitizedFilter}`);
|
|
|
|
}
|
2017-05-17 19:02:09 +00:00
|
|
|
} else {
|
2017-05-23 22:05:31 +00:00
|
|
|
throw new Error(`Unknown field ${field}`);
|
2017-05-17 19:02:09 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-10 14:46:32 +00:00
|
|
|
}: PluginTemplateTag);
|
2017-05-17 19:02:09 +00:00
|
|
|
|
2017-09-22 13:48:47 +00:00
|
|
|
function matchJSONPath (bodyStr: string, query: string): string {
|
2017-06-01 02:04:27 +00:00
|
|
|
let body;
|
|
|
|
let results;
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
try {
|
|
|
|
body = JSON.parse(bodyStr);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Invalid JSON: ${err.message}`);
|
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
try {
|
|
|
|
results = jq.query(body, query);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Invalid JSONPath query: ${query}`);
|
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
if (results.length === 0) {
|
|
|
|
throw new Error(`Returned no results: ${query}`);
|
|
|
|
} else if (results.length > 1) {
|
|
|
|
throw new Error(`Returned more than one result: ${query}`);
|
2017-05-17 19:02:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 14:46:32 +00:00
|
|
|
if (typeof results[0] !== 'string') {
|
|
|
|
return JSON.stringify(results[0]);
|
|
|
|
} else {
|
|
|
|
return results[0];
|
|
|
|
}
|
2017-06-01 02:04:27 +00:00
|
|
|
}
|
2017-05-17 19:02:09 +00:00
|
|
|
|
2017-09-22 13:48:47 +00:00
|
|
|
function matchXPath (bodyStr: string, query: string): string {
|
|
|
|
const results = xpath.query(bodyStr, query);
|
2017-05-17 19:02:09 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
if (results.length === 0) {
|
|
|
|
throw new Error(`Returned no results: ${query}`);
|
|
|
|
} else if (results.length > 1) {
|
|
|
|
throw new Error(`Returned more than one result: ${query}`);
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
2017-05-23 22:05:31 +00:00
|
|
|
|
2017-09-22 13:48:47 +00:00
|
|
|
return results[0].inner;
|
2017-06-01 02:04:27 +00:00
|
|
|
}
|
2017-05-23 22:05:31 +00:00
|
|
|
|
2017-09-22 13:48:47 +00:00
|
|
|
function matchHeader (headers: Array<ResponseHeader>, name: string): string {
|
2017-10-10 14:46:32 +00:00
|
|
|
if (!headers.length) {
|
|
|
|
throw new Error(`No headers available`);
|
|
|
|
}
|
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
const header = headers.find(
|
|
|
|
h => h.name.toLowerCase() === name.toLowerCase()
|
|
|
|
);
|
2017-05-23 22:05:31 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
if (!header) {
|
2017-10-10 14:46:32 +00:00
|
|
|
const names = headers.map(c => `"${c.name}"`).join(',\n\t');
|
|
|
|
throw new Error(`No header with name "${name}".\nChoices are [\n\t${names}\n]`);
|
2017-05-23 22:05:31 +00:00
|
|
|
}
|
2017-06-01 02:04:27 +00:00
|
|
|
|
|
|
|
return header.value;
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|