2017-11-26 20:45:40 +00:00
|
|
|
const jq = require('jsonpath');
|
2017-12-01 14:18:38 +00:00
|
|
|
const iconv = require('iconv-lite');
|
2018-06-25 17:42:50 +00:00
|
|
|
const { query: queryXPath } = require('insomnia-xpath');
|
|
|
|
|
2020-01-22 19:23:19 +00:00
|
|
|
function isFilterableField(field) {
|
|
|
|
return field !== 'raw' && field !== 'url';
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
module.exports.templateTags = [
|
|
|
|
{
|
|
|
|
name: 'response',
|
|
|
|
displayName: 'Response',
|
|
|
|
description: "reference values from other request's responses",
|
|
|
|
args: [
|
|
|
|
{
|
|
|
|
displayName: 'Attribute',
|
|
|
|
type: 'enum',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Body Attribute',
|
|
|
|
description: 'value of response body',
|
2018-12-12 17:36:11 +00:00
|
|
|
value: 'body',
|
2018-06-25 17:42:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Raw Body',
|
|
|
|
description: 'entire response body',
|
2018-12-12 17:36:11 +00:00
|
|
|
value: 'raw',
|
2018-06-25 17:42:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Header',
|
|
|
|
description: 'value of response header',
|
2018-12-12 17:36:11 +00:00
|
|
|
value: 'header',
|
|
|
|
},
|
2020-01-22 19:23:19 +00:00
|
|
|
{
|
|
|
|
displayName: 'Request URL',
|
|
|
|
description: 'Url of initiating request',
|
|
|
|
value: 'url',
|
|
|
|
},
|
2018-12-12 17:36:11 +00:00
|
|
|
],
|
2018-06-25 17:42:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Request',
|
|
|
|
type: 'model',
|
2018-12-12 17:36:11 +00:00
|
|
|
model: 'Request',
|
2018-06-25 17:42:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'string',
|
2019-05-08 17:39:58 +00:00
|
|
|
encoding: 'base64',
|
2020-01-22 19:23:19 +00:00
|
|
|
hide: args => !isFilterableField(args[0].value),
|
2018-06-25 17:42:50 +00:00
|
|
|
displayName: args => {
|
|
|
|
switch (args[0].value) {
|
|
|
|
case 'body':
|
|
|
|
return 'Filter (JSONPath or XPath)';
|
|
|
|
case 'header':
|
|
|
|
return 'Header Name';
|
|
|
|
default:
|
|
|
|
return 'Filter';
|
|
|
|
}
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
|
|
|
},
|
2019-04-27 04:34:15 +00:00
|
|
|
{
|
|
|
|
displayName: 'Trigger Behavior',
|
|
|
|
help: 'Configure when to resend the dependent request',
|
|
|
|
type: 'enum',
|
|
|
|
options: [
|
|
|
|
{
|
2019-04-27 04:45:11 +00:00
|
|
|
displayName: 'Never',
|
|
|
|
description: 'never resend request',
|
|
|
|
value: 'never',
|
2019-04-27 04:34:15 +00:00
|
|
|
},
|
|
|
|
{
|
2019-04-27 04:45:11 +00:00
|
|
|
displayName: 'No History',
|
|
|
|
description: 'resend when no responses present',
|
|
|
|
value: 'no-history',
|
2019-04-27 04:34:15 +00:00
|
|
|
},
|
2020-06-11 20:00:46 +00:00
|
|
|
{
|
|
|
|
displayName: 'When Expired',
|
|
|
|
description: 'resend when existing response has expired',
|
|
|
|
value: 'when-expired',
|
|
|
|
},
|
2019-04-27 04:34:15 +00:00
|
|
|
{
|
2019-04-27 04:45:11 +00:00
|
|
|
displayName: 'Always',
|
|
|
|
description: 'resend request when needed',
|
|
|
|
value: 'always',
|
2019-04-27 04:34:15 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-06-11 20:00:46 +00:00
|
|
|
{
|
|
|
|
displayName: 'Max age (seconds)',
|
|
|
|
help: 'The maximum age of a response to use before it expires',
|
|
|
|
type: 'number',
|
|
|
|
hide: args => args[3].value !== 'when-expired',
|
|
|
|
defaultValue: 60,
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
],
|
2017-11-26 20:45:40 +00:00
|
|
|
|
2020-06-11 20:00:46 +00:00
|
|
|
async run(context, field, id, filter, resendBehavior, maxAgeSeconds) {
|
2018-06-25 17:42:50 +00:00
|
|
|
filter = filter || '';
|
2019-05-07 14:44:02 +00:00
|
|
|
resendBehavior = (resendBehavior || 'never').toLowerCase();
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2020-01-22 19:23:19 +00:00
|
|
|
if (!['body', 'header', 'raw', 'url'].includes(field)) {
|
2018-06-25 17:42:50 +00:00
|
|
|
throw new Error(`Invalid response field ${field}`);
|
|
|
|
}
|
2017-10-10 14:46:32 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
if (!id) {
|
|
|
|
throw new Error('No request specified');
|
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const request = await context.util.models.request.getById(id);
|
|
|
|
if (!request) {
|
|
|
|
throw new Error(`Could not find request ${id}`);
|
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2020-01-22 19:23:19 +00:00
|
|
|
const environmentId = context.context.getEnvironmentId();
|
|
|
|
let response = await context.util.models.response.getLatestForRequestId(id, environmentId);
|
2019-04-27 04:34:15 +00:00
|
|
|
|
|
|
|
let shouldResend = false;
|
2019-05-15 20:01:36 +00:00
|
|
|
if (context.context.getExtraInfo('fromResponseTag')) {
|
|
|
|
shouldResend = false;
|
|
|
|
} else if (resendBehavior === 'never') {
|
2019-04-27 04:34:15 +00:00
|
|
|
shouldResend = false;
|
2019-05-08 17:39:58 +00:00
|
|
|
} else if (resendBehavior === 'no-history') {
|
|
|
|
shouldResend = !response;
|
2020-06-11 20:00:46 +00:00
|
|
|
} else if (resendBehavior === 'when-expired') {
|
|
|
|
if (!response) {
|
|
|
|
shouldResend = true;
|
|
|
|
} else {
|
|
|
|
const ageSeconds = (Date.now() - response.created) / 1000;
|
|
|
|
shouldResend = ageSeconds > maxAgeSeconds;
|
|
|
|
}
|
2019-05-08 17:39:58 +00:00
|
|
|
} else if (resendBehavior === 'always') {
|
|
|
|
shouldResend = true;
|
2019-04-27 04:34:15 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 20:01:36 +00:00
|
|
|
// Make sure we only send the request once per render so we don't have infinite recursion
|
|
|
|
const fromResponseTag = context.context.getExtraInfo('fromResponseTag');
|
|
|
|
if (fromResponseTag) {
|
|
|
|
console.log('[response tag] Preventing recursive render');
|
|
|
|
shouldResend = false;
|
|
|
|
}
|
|
|
|
|
2019-05-08 17:39:58 +00:00
|
|
|
if (shouldResend && context.renderPurpose === 'send') {
|
2019-04-27 04:34:15 +00:00
|
|
|
console.log('[response tag] Resending dependency');
|
2019-05-15 20:01:36 +00:00
|
|
|
response = await context.network.sendRequest(request, [
|
|
|
|
{ name: 'fromResponseTag', value: true },
|
|
|
|
]);
|
2019-04-27 04:34:15 +00:00
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
if (!response) {
|
2019-05-15 20:01:36 +00:00
|
|
|
console.log('[response tag] No response found');
|
2018-06-25 17:42:50 +00:00
|
|
|
throw new Error('No responses for request');
|
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2019-05-15 20:01:36 +00:00
|
|
|
if (response.error) {
|
|
|
|
console.log('[response tag] Response error ' + response.error);
|
|
|
|
throw new Error('Failed to send dependent request ' + response.error);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
if (!response.statusCode) {
|
2019-05-15 20:01:36 +00:00
|
|
|
console.log('[response tag] Invalid status code ' + response.statusCode);
|
2018-06-25 17:42:50 +00:00
|
|
|
throw new Error('No successful responses for request');
|
2017-12-01 14:18:38 +00:00
|
|
|
}
|
2018-06-25 17:42:50 +00:00
|
|
|
|
2020-01-22 19:23:19 +00:00
|
|
|
if (isFilterableField(field) && !filter) {
|
2018-06-25 17:42:50 +00:00
|
|
|
throw new Error(`No ${field} filter specified`);
|
2017-12-01 14:18:38 +00:00
|
|
|
}
|
2017-05-23 22:05:31 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const sanitizedFilter = filter.trim();
|
|
|
|
|
|
|
|
if (field === 'header') {
|
|
|
|
return matchHeader(response.headers, sanitizedFilter);
|
2020-01-22 19:23:19 +00:00
|
|
|
} else if (field === 'url') {
|
|
|
|
return response.url;
|
2018-06-25 17:42:50 +00:00
|
|
|
} else if (field === 'raw') {
|
2018-10-17 16:42:33 +00:00
|
|
|
const bodyBuffer = context.util.models.response.getBodyBuffer(response, '');
|
2018-06-25 17:42:50 +00:00
|
|
|
const match = response.contentType.match(/charset=([\w-]+)/);
|
|
|
|
const charset = match && match.length >= 2 ? match[1] : 'utf-8';
|
|
|
|
|
|
|
|
// Sometimes iconv conversion fails so fallback to regular buffer
|
|
|
|
try {
|
|
|
|
return iconv.decode(bodyBuffer, charset);
|
|
|
|
} catch (err) {
|
|
|
|
console.warn('[response] Failed to decode body', err);
|
|
|
|
return bodyBuffer.toString();
|
|
|
|
}
|
|
|
|
} else if (field === 'body') {
|
2018-10-17 16:42:33 +00:00
|
|
|
const bodyBuffer = context.util.models.response.getBodyBuffer(response, '');
|
2018-06-25 17:42:50 +00:00
|
|
|
const match = response.contentType.match(/charset=([\w-]+)/);
|
|
|
|
const charset = match && match.length >= 2 ? match[1] : 'utf-8';
|
|
|
|
|
|
|
|
// Sometimes iconv conversion fails so fallback to regular buffer
|
|
|
|
let body;
|
|
|
|
try {
|
|
|
|
body = iconv.decode(bodyBuffer, charset);
|
|
|
|
} catch (err) {
|
|
|
|
body = bodyBuffer.toString();
|
|
|
|
console.warn('[response] Failed to decode body', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sanitizedFilter.indexOf('$') === 0) {
|
|
|
|
return matchJSONPath(body, sanitizedFilter);
|
|
|
|
} else {
|
|
|
|
return matchXPath(body, sanitizedFilter);
|
|
|
|
}
|
2017-05-23 22:05:31 +00:00
|
|
|
} else {
|
2018-06-25 17:42:50 +00:00
|
|
|
throw new Error(`Unknown field ${field}`);
|
2017-05-23 22:05:31 +00:00
|
|
|
}
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
2017-05-17 19:02:09 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
function matchJSONPath(bodyStr, query) {
|
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
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
function matchXPath(bodyStr, query) {
|
2017-11-26 20:45:40 +00:00
|
|
|
const results = queryXPath(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
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
function matchHeader(headers, name) {
|
2017-10-10 14:46:32 +00:00
|
|
|
if (!headers.length) {
|
2020-04-09 17:32:19 +00:00
|
|
|
throw new Error('No headers available');
|
2017-10-10 14:46:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +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');
|
2018-10-17 16:42:33 +00:00
|
|
|
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
|
|
|
}
|