insomnia-plugin-response
Advanced tools
Comparing version 1.0.4 to 1.0.5
const tag = require('..').templateTags[0]; | ||
const iconv = require('iconv-lite'); | ||
@@ -67,2 +68,3 @@ describe('Response tag', () => { | ||
statusCode: 200, | ||
contentType: 'application/json', | ||
_body: '{"foo": "bar"}' | ||
@@ -84,2 +86,3 @@ }]; | ||
statusCode: 200, | ||
contentType: 'application/json', | ||
_body: '{"foo": "' | ||
@@ -105,2 +108,3 @@ }]; | ||
statusCode: 200, | ||
contentType: 'application/json', | ||
_body: '{"foo": "bar"}' | ||
@@ -126,2 +130,3 @@ }]; | ||
statusCode: 200, | ||
contentType: 'application/json', | ||
_body: '{"foo": "bar"}' | ||
@@ -147,2 +152,3 @@ }]; | ||
statusCode: 200, | ||
contentType: 'application/json', | ||
_body: '{"array": ["bar", "baz"]}' | ||
@@ -160,2 +166,18 @@ }]; | ||
}); | ||
it('works with utf-16 encoding', async () => { | ||
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 context = _genTestContext(requests, responses); | ||
expect(await tag.run(context, 'body', 'req_1', '$.array[0]')).toBe('bar'); | ||
}); | ||
}); | ||
@@ -171,2 +193,3 @@ | ||
statusCode: 200, | ||
contentType: 'application/xml', | ||
_body: '<foo><bar>Hello World!</bar></foo>' | ||
@@ -188,2 +211,3 @@ }]; | ||
statusCode: 200, | ||
contentType: 'application/xml', | ||
_body: '<foo><bar hello="World">Hello World!</bar></foo>' | ||
@@ -205,2 +229,3 @@ }]; | ||
statusCode: 200, | ||
contentType: 'application/xml', | ||
_body: '<hi></hi></sstr>' | ||
@@ -226,2 +251,3 @@ }]; | ||
statusCode: 200, | ||
contentType: 'application/xml', | ||
_body: '<foo></foo>' | ||
@@ -247,2 +273,3 @@ }]; | ||
statusCode: 200, | ||
contentType: 'application/xml', | ||
_body: '<foo></foo>' | ||
@@ -268,2 +295,3 @@ }]; | ||
statusCode: 200, | ||
contentType: 'application/xml', | ||
_body: '<foo><bar>Hello World!</bar><bar>And again!</bar></foo>' | ||
@@ -291,2 +319,3 @@ }]; | ||
statusCode: 200, | ||
contentType: '', | ||
headers: [ | ||
@@ -339,2 +368,3 @@ {name: 'Content-Type', value: 'application/json'}, | ||
statusCode: 200, | ||
contentType: 'text/plain', | ||
_body: 'Hello World!' | ||
@@ -371,4 +401,13 @@ }]; | ||
getBodyBuffer (response) { | ||
const str = bodies[response._id]; | ||
return str ? Buffer.from(str) : null; | ||
const strOrBuffer = bodies[response._id]; | ||
if (typeof strOrBuffer === 'string') { | ||
return Buffer.from(strOrBuffer); | ||
} | ||
if (!strOrBuffer) { | ||
return null; | ||
} | ||
return strOrBuffer; | ||
} | ||
@@ -375,0 +414,0 @@ } |
30
index.js
const jq = require('jsonpath'); | ||
const iconv = require('iconv-lite'); | ||
const {query: queryXPath} = require('insomnia-xpath'); | ||
@@ -74,10 +75,31 @@ | ||
} else if (field === 'raw') { | ||
return context.util.models.response.getBodyBuffer(response, '').toString(); | ||
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 bodyStr = context.util.models.response.getBodyBuffer(response, '').toString(); | ||
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); | ||
} | ||
if (sanitizedFilter.indexOf('$') === 0) { | ||
return matchJSONPath(bodyStr, sanitizedFilter); | ||
return matchJSONPath(body, sanitizedFilter); | ||
} else if (sanitizedFilter.indexOf('/') === 0) { | ||
return matchXPath(bodyStr, sanitizedFilter); | ||
return matchXPath(body, sanitizedFilter); | ||
} else { | ||
@@ -84,0 +106,0 @@ throw new Error(`Invalid format for response query: ${sanitizedFilter}`); |
{ | ||
"name": "insomnia-plugin-response", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"author": "Gregory Schier <gschier1990@gmail.com>", | ||
@@ -19,2 +19,3 @@ "description": "Insomnia response template tag", | ||
"dependencies": { | ||
"iconv-lite": "^0.4.19", | ||
"insomnia-xpath": "^0.0.5", | ||
@@ -21,0 +22,0 @@ "jsonpath": "^1.0.0" |
16900
471
3
+ Addediconv-lite@^0.4.19
+ Addediconv-lite@0.4.24(transitive)
+ Addedsafer-buffer@2.1.2(transitive)