diff --git a/plugins/insomnia-plugin-response/__tests__/index.test.js b/plugins/insomnia-plugin-response/__tests__/index.test.js index 589e009182..6a23fdbb0d 100644 --- a/plugins/insomnia-plugin-response/__tests__/index.test.js +++ b/plugins/insomnia-plugin-response/__tests__/index.test.js @@ -150,7 +150,7 @@ describe('Response tag', () => { } }); - it('fails on more than 1 result', async () => { + it('allows more than 1 result if array of strings', async () => { const requests = [{ _id: 'req_1', parentId: 'wrk_1' }]; const responses = [ @@ -165,12 +165,29 @@ describe('Response tag', () => { const context = _genTestContext(requests, responses); - try { - await tag.run(context, 'body', 'req_1', '$.array.*'); - fail('JSON should have failed to parse'); - } catch (err) { - expect(err.message).toContain('Returned more than one result: $.array.*'); - } + + const result = await tag.run(context, 'body', 'req_1', '$.array'); + expect(result).toBe('["bar","baz"]'); + }); + + it('allows more than 1 result if array of integers', async () => { + const requests = [{ _id: 'req_1', parentId: 'wrk_1' }]; + + const responses = [ + { + _id: 'res_1', + parentId: 'req_1', + statusCode: 200, + contentType: 'application/json', + _body: '{"array": [1, 2]}', + }, + ]; + + const context = _genTestContext(requests, responses); + + + const result = await tag.run(context, 'body', 'req_1', '$.array'); + expect(result).toBe('[1,2]'); }); it('works with utf-16 encoding', async () => { diff --git a/plugins/insomnia-plugin-response/index.js b/plugins/insomnia-plugin-response/index.js index 1d6e5fcd58..3c14383ef5 100644 --- a/plugins/insomnia-plugin-response/index.js +++ b/plugins/insomnia-plugin-response/index.js @@ -134,7 +134,7 @@ module.exports.templateTags = [ shouldResend = ageSeconds > maxAgeSeconds; } break; - + case 'always': shouldResend = true; break; @@ -209,7 +209,7 @@ module.exports.templateTags = [ console.warn('[response] Failed to decode body', err); body = bodyBuffer.toString(); } - + if (sanitizedFilter.indexOf('$') === 0) { return matchJSONPath(body, sanitizedFilter); } else { @@ -241,8 +241,10 @@ function matchJSONPath(bodyStr, query) { 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}`); + } + + if (results.length > 1 ) { + return JSON.stringify(results); } if (typeof results[0] !== 'string') {