New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

insomnia-plugin-response

Package Overview
Dependencies
Maintainers
1
Versions
103
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

insomnia-plugin-response - npm Package Compare versions

Comparing version 1.0.7 to 1.0.8

377

__tests__/index.test.js

@@ -7,3 +7,3 @@ const tag = require('..').templateTags[0];

it('fails on no responses', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];

@@ -21,10 +21,12 @@ const context = _genTestContext(requests);

it('fails on no request', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
_body: '{"foo": "bar"}'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
_body: '{"foo": "bar"}'
}
];

@@ -42,10 +44,12 @@ const context = _genTestContext(requests, responses);

it('fails on empty filter', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
_body: '{"foo": "bar"}'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
_body: '{"foo": "bar"}'
}
];

@@ -65,11 +69,13 @@ const context = _genTestContext(requests, responses);

it('basic query', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json',
_body: '{"foo": "bar"}'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json',
_body: '{"foo": "bar"}'
}
];

@@ -83,11 +89,13 @@ const context = _genTestContext(requests, responses);

it('fails on invalid JSON', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json',
_body: '{"foo": "'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json',
_body: '{"foo": "'
}
];

@@ -100,3 +108,5 @@ const context = _genTestContext(requests, responses);

} catch (err) {
expect(err.message).toContain('Invalid JSON: Unexpected end of JSON input');
expect(err.message).toContain(
'Invalid JSON: Unexpected end of JSON input'
);
}

@@ -106,11 +116,13 @@ });

it('fails on invalid query', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json',
_body: '{"foo": "bar"}'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json',
_body: '{"foo": "bar"}'
}
];

@@ -128,11 +140,13 @@ const context = _genTestContext(requests, responses);

it('fails on no results', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json',
_body: '{"foo": "bar"}'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json',
_body: '{"foo": "bar"}'
}
];

@@ -150,11 +164,13 @@ const context = _genTestContext(requests, responses);

it('fails on more than 1 result', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json',
_body: '{"array": ["bar", "baz"]}'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json',
_body: '{"array": ["bar", "baz"]}'
}
];

@@ -167,3 +183,5 @@ const context = _genTestContext(requests, responses);

} catch (err) {
expect(err.message).toContain('Returned more than one result: $.array.*');
expect(err.message).toContain(
'Returned more than one result: $.array.*'
);
}

@@ -173,11 +191,13 @@ });

it('works with utf-16 encoding', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json; charset=UTF-16',
_body: iconv.encode('{"array": ["bar", "baz"]}', 'UTF-16')
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/json; charset=UTF-16',
_body: iconv.encode('{"array": ["bar", "baz"]}', 'UTF-16')
}
];

@@ -192,11 +212,13 @@ const context = _genTestContext(requests, responses);

it('renders basic response "body" query', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo><bar>Hello World!</bar></foo>'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo><bar>Hello World!</bar></foo>'
}
];

@@ -210,11 +232,13 @@ const context = _genTestContext(requests, responses);

it('renders basic response "body" attribute query', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo><bar hello="World">Hello World!</bar></foo>'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo><bar hello="World">Hello World!</bar></foo>'
}
];

@@ -228,14 +252,21 @@ const context = _genTestContext(requests, responses);

it('renders query that does not start with slash', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo><bar hello="World">Hello World!</bar></foo>'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo><bar hello="World">Hello World!</bar></foo>'
}
];
const context = _genTestContext(requests, responses);
const result = await tag.run(context, 'body', 'req_1', 'substring(/foo/bar, 7)');
const result = await tag.run(
context,
'body',
'req_1',
'substring(/foo/bar, 7)'
);

@@ -246,11 +277,13 @@ expect(result).toBe('World!');

it('no results on invalid XML', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<hi></hi></sstr>'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<hi></hi></sstr>'
}
];

@@ -268,11 +301,13 @@ const context = _genTestContext(requests, responses);

it('fails on invalid query', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo></foo>'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo></foo>'
}
];

@@ -290,11 +325,13 @@ const context = _genTestContext(requests, responses);

it('fails on no results', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo></foo>'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo></foo>'
}
];

@@ -312,11 +349,13 @@ const context = _genTestContext(requests, responses);

it('fails on more than 1 result', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo><bar>Hello World!</bar><bar>And again!</bar></foo>'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'application/xml',
_body: '<foo><bar>Hello World!</bar><bar>And again!</bar></foo>'
}
];

@@ -336,35 +375,47 @@ const context = _genTestContext(requests, responses);

it('renders basic response "header"', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: '',
headers: [
{name: 'Content-Type', value: 'application/json'},
{name: 'Content-Length', value: '20'}
]
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: '',
headers: [
{ name: 'Content-Type', value: 'application/json' },
{ name: 'Content-Length', value: '20' }
]
}
];
const context = _genTestContext(requests, responses);
expect(await tag.run(context, 'header', 'req_1', 'content-type')).toBe('application/json');
expect(await tag.run(context, 'header', 'req_1', 'Content-Type')).toBe('application/json');
expect(await tag.run(context, 'header', 'req_1', 'CONTENT-type')).toBe('application/json');
expect(await tag.run(context, 'header', 'req_1', 'CONTENT-type ')).toBe('application/json');
expect(await tag.run(context, 'header', 'req_1', 'content-type')).toBe(
'application/json'
);
expect(await tag.run(context, 'header', 'req_1', 'Content-Type')).toBe(
'application/json'
);
expect(await tag.run(context, 'header', 'req_1', 'CONTENT-type')).toBe(
'application/json'
);
expect(
await tag.run(context, 'header', 'req_1', 'CONTENT-type ')
).toBe('application/json');
});
it('no results on missing header', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
headers: [
{name: 'Content-Type', value: 'application/json'},
{name: 'Content-Length', value: '20'}
]
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
headers: [
{ name: 'Content-Type', value: 'application/json' },
{ name: 'Content-Length', value: '20' }
]
}
];

@@ -377,4 +428,6 @@ const context = _genTestContext(requests, responses);

} catch (err) {
expect(err.message).toBe('No header with name "missing".\n' +
'Choices are [\n\t"Content-Type",\n\t"Content-Length"\n]');
expect(err.message).toBe(
'No header with name "missing".\n' +
'Choices are [\n\t"Content-Type",\n\t"Content-Length"\n]'
);
}

@@ -386,11 +439,13 @@ });

it('renders basic response', async () => {
const requests = [{_id: 'req_1', parentId: 'wrk_1'}];
const requests = [{ _id: 'req_1', parentId: 'wrk_1' }];
const responses = [{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'text/plain',
_body: 'Hello World!'
}];
const responses = [
{
_id: 'res_1',
parentId: 'req_1',
statusCode: 200,
contentType: 'text/plain',
_body: 'Hello World!'
}
];

@@ -404,3 +459,3 @@ const context = _genTestContext(requests, responses);

function _genTestContext (requests, responses) {
function _genTestContext(requests, responses) {
requests = requests || [];

@@ -417,3 +472,3 @@ responses = responses || [];

request: {
getById (requestId) {
getById(requestId) {
return requests.find(r => r._id === requestId) || null;

@@ -423,6 +478,6 @@ }

response: {
getLatestForRequestId (requestId) {
getLatestForRequestId(requestId) {
return responses.find(r => r.parentId === requestId) || null;
},
getBodyBuffer (response) {
getBodyBuffer(response) {
const strOrBuffer = bodies[response._id];

@@ -429,0 +484,0 @@

const jq = require('jsonpath');
const iconv = require('iconv-lite');
const {query: queryXPath} = require('insomnia-xpath');
const { query: queryXPath } = require('insomnia-xpath');
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', 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';
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',
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';
}
}
}
}
],
],
async run (context, field, id, filter) {
filter = filter || '';
async run(context, field, id, filter) {
filter = filter || '';
if (!['body', 'header', 'raw'].includes(field)) {
throw new Error(`Invalid response field ${field}`);
}
if (!['body', 'header', 'raw'].includes(field)) {
throw new Error(`Invalid response field ${field}`);
}
if (!id) {
throw new Error('No request specified');
}
if (!id) {
throw new Error('No request specified');
}
const request = await context.util.models.request.getById(id);
if (!request) {
throw new Error(`Could not find request ${id}`);
}
const request = await context.util.models.request.getById(id);
if (!request) {
throw new Error(`Could not find request ${id}`);
}
const response = await context.util.models.response.getLatestForRequestId(id);
const response = await context.util.models.response.getLatestForRequestId(
id
);
if (!response) {
throw new Error('No responses for request');
}
if (!response) {
throw new Error('No responses for request');
}
if (!response.statusCode) {
throw new Error('No successful responses for request');
}
if (!response.statusCode) {
throw new Error('No successful responses for request');
}
if (field !== 'raw' && !filter) {
throw new Error(`No ${field} filter specified`);
}
if (field !== 'raw' && !filter) {
throw new Error(`No ${field} filter specified`);
}
const sanitizedFilter = filter.trim();
const sanitizedFilter = filter.trim();
if (field === 'header') {
return matchHeader(response.headers, sanitizedFilter);
} else if (field === 'raw') {
const bodyBuffer = context.util.models.response.getBodyBuffer(response, '');
const match = response.contentType.match(/charset=([\w-]+)/);
const charset = (match && match.length >= 2) ? match[1] : 'utf-8';
if (field === 'header') {
return matchHeader(response.headers, sanitizedFilter);
} else if (field === 'raw') {
const bodyBuffer = context.util.models.response.getBodyBuffer(
response,
''
);
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') {
const bodyBuffer = context.util.models.response.getBodyBuffer(response, '');
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') {
const bodyBuffer = context.util.models.response.getBodyBuffer(
response,
''
);
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);
}
// 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);
if (sanitizedFilter.indexOf('$') === 0) {
return matchJSONPath(body, sanitizedFilter);
} else {
return matchXPath(body, sanitizedFilter);
}
} else {
return matchXPath(body, sanitizedFilter);
throw new Error(`Unknown field ${field}`);
}
} else {
throw new Error(`Unknown field ${field}`);
}
}
}];
];
function matchJSONPath (bodyStr, query) {
function matchJSONPath(bodyStr, query) {
let body;

@@ -140,3 +162,3 @@ let results;

function matchXPath (bodyStr, query) {
function matchXPath(bodyStr, query) {
const results = queryXPath(bodyStr, query);

@@ -153,3 +175,3 @@

function matchHeader (headers, name) {
function matchHeader(headers, name) {
if (!headers.length) {

@@ -159,9 +181,9 @@ throw new Error(`No headers available`);

const header = headers.find(
h => h.name.toLowerCase() === name.toLowerCase()
);
const header = headers.find(h => h.name.toLowerCase() === name.toLowerCase());
if (!header) {
const names = headers.map(c => `"${c.name}"`).join(',\n\t');
throw new Error(`No header with name "${name}".\nChoices are [\n\t${names}\n]`);
throw new Error(
`No header with name "${name}".\nChoices are [\n\t${names}\n]`
);
}

@@ -171,2 +193,1 @@

}
{
"name": "insomnia-plugin-response",
"version": "1.0.7",
"version": "1.0.8",
"author": "Gregory Schier <gschier1990@gmail.com>",

@@ -20,5 +20,5 @@ "description": "Insomnia response template tag",

"iconv-lite": "^0.4.19",
"insomnia-xpath": "^1.0.1",
"insomnia-xpath": "^1.0.2",
"jsonpath": "^1.0.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc