2017-02-20 18:32:27 +00:00
|
|
|
import jq from 'jsonpath';
|
2017-05-17 19:02:09 +00:00
|
|
|
import {DOMParser} from 'xmldom';
|
|
|
|
import xpath from 'xpath';
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
export default {
|
|
|
|
name: 'response',
|
2017-06-09 01:10:12 +00:00
|
|
|
displayName: 'Response',
|
2017-06-01 02:04:27 +00:00
|
|
|
description: 'reference values from other requests',
|
|
|
|
args: [
|
|
|
|
{
|
|
|
|
displayName: 'Attribute',
|
|
|
|
type: 'enum',
|
|
|
|
options: [
|
|
|
|
{displayName: 'Body', description: 'attribute of response body', value: 'body'},
|
|
|
|
{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',
|
|
|
|
hide: args => args[0].value === 'raw',
|
|
|
|
displayName: args => {
|
|
|
|
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
|
|
|
|
|
|
|
async run (context, field, id, filter) {
|
|
|
|
if (!['body', 'header', 'raw'].includes(field)) {
|
2017-02-27 21:00:13 +00:00
|
|
|
throw new Error(`Invalid response field ${field}`);
|
|
|
|
}
|
|
|
|
|
2017-05-23 22:05:31 +00:00
|
|
|
if (field !== 'raw' && !filter) {
|
|
|
|
throw new Error(`No ${field} filter 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-05-23 22:05:31 +00:00
|
|
|
if (!response.statusCode || response.statusCode < 100) {
|
|
|
|
throw new Error('No responses for request');
|
|
|
|
}
|
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') {
|
|
|
|
const bodyBuffer = new Buffer(response.body, response.encoding);
|
|
|
|
return bodyBuffer.toString();
|
|
|
|
} else if (field === 'body') {
|
|
|
|
const bodyBuffer = new Buffer(response.body, response.encoding);
|
|
|
|
const bodyStr = bodyBuffer.toString();
|
|
|
|
|
|
|
|
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-06-01 02:04:27 +00:00
|
|
|
};
|
2017-05-17 19:02:09 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
function matchJSONPath (bodyStr, query) {
|
|
|
|
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-06-01 02:04:27 +00:00
|
|
|
return results[0];
|
|
|
|
}
|
2017-05-17 19:02:09 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
function matchXPath (bodyStr, query) {
|
|
|
|
let results;
|
2017-05-17 19:02:09 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
// This will never throw
|
|
|
|
const dom = new DOMParser().parseFromString(bodyStr);
|
2017-05-17 19:02:09 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
try {
|
|
|
|
results = xpath.select(query, dom);
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Invalid XPath query: ${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-06-01 02:04:27 +00:00
|
|
|
return results[0].childNodes.toString();
|
|
|
|
}
|
2017-05-23 22:05:31 +00:00
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
function matchHeader (headers, name) {
|
|
|
|
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) {
|
|
|
|
throw new Error(`No match for header: ${name}`);
|
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
|
|
|
}
|