From f2096e2460745007ef5a0951c34f83718073fe4c Mon Sep 17 00:00:00 2001 From: James Sampson Date: Tue, 11 Dec 2018 20:55:49 -0600 Subject: [PATCH] Implement query parameter tag and test --- .../__tests__/index.test.js | 16 ++++++++ plugins/insomnia-plugin-request/index.js | 38 +++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/plugins/insomnia-plugin-request/__tests__/index.test.js b/plugins/insomnia-plugin-request/__tests__/index.test.js index e7bf9c5a649..26910be99c2 100644 --- a/plugins/insomnia-plugin-request/__tests__/index.test.js +++ b/plugins/insomnia-plugin-request/__tests__/index.test.js @@ -70,6 +70,22 @@ describe('plugin', () => { expect(result).toBe('bar'); }); }); + + describe('RequestExtension parameter', async () => { + it('should get parameter', async () => { + const requests = [ + { + _id: 'req_1', + parameters: [{ name: 'foo', value: '{{ foo }}' }], + url: 'https://insomnia.rest/foo/bar' + } + ]; + const context = _getTestContext([{ _id: 'wrk_1' }], requests); + const result = await tag.run(context, 'parameter', 'foo'); + + expect(result).toBe('bar'); + }); + }); }); function _getTestContext(workspaces, requests, jars) { diff --git a/plugins/insomnia-plugin-request/index.js b/plugins/insomnia-plugin-request/index.js index 6bd46984db5..a29ec7b3bfa 100644 --- a/plugins/insomnia-plugin-request/index.js +++ b/plugins/insomnia-plugin-request/index.js @@ -20,6 +20,11 @@ module.exports.templateTags = [ value: 'name', description: 'name of request' }, + { + displayName: 'Query Paramter', + value: 'parameter', + description: 'value of query parameter from request' + }, { displayName: 'Folder', value: 'folder', @@ -54,6 +59,8 @@ module.exports.templateTags = [ switch (args[0].value) { case 'cookie': return 'Cookie Name'; + case 'parameter': + return 'Query Parameter Name'; case 'header': return 'Header Name'; default: @@ -98,12 +105,35 @@ module.exports.templateTags = [ const cookieJar = await context.util.models.cookieJar.getOrCreateForWorkspace(workspace); const url = await getRequestUrl(context, request); return getCookieValue(cookieJar, url, name); + case 'parameter': + if (!name) { + throw new Error('No query parameter specified'); + } + + const parameterNames = []; + + if (request.parameters.length === 0) { + throw new Error(`No headers available`); + } + + for (const queryParameter of request.parameters) { + const queryParameterName = await context.util.render(queryParameter.name); + parameterNames.push(queryParameterName); + if (queryParameterName.toLowerCase() === name.toLowerCase()) { + return context.util.render(queryParameter.value); + } + } + + const parameterNamesStr = parameterNames.map(n => `"${n}"`).join(',\n\t'); + throw new Error( + `No query parameter with name "${name}".\nChoices are [\n\t${parameterNamesStr}\n]` + ); case 'header': if (!name) { throw new Error('No header specified'); } - const names = []; + const headerNames = []; if (request.headers.length === 0) { throw new Error(`No headers available`); @@ -111,14 +141,14 @@ module.exports.templateTags = [ for (const header of request.headers) { const headerName = await context.util.render(header.name); - names.push(headerName); + headerNames.push(headerName); if (headerName.toLowerCase() === name.toLowerCase()) { return context.util.render(header.value); } } - const namesStr = names.map(n => `"${n}"`).join(',\n\t'); - throw new Error(`No header with name "${name}".\nChoices are [\n\t${namesStr}\n]`); + const headerNamesStr = headerNames.map(n => `"${n}"`).join(',\n\t'); + throw new Error(`No header with name "${name}".\nChoices are [\n\t${headerNamesStr}\n]`); case 'oauth2': const token = await context.util.models.oAuth2Token.getByRequestId(request._id); if (!token) {