Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement query parameter tag and test (#1112) #1301

Merged
merged 1 commit into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions plugins/insomnia-plugin-request/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
38 changes: 34 additions & 4 deletions plugins/insomnia-plugin-request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -98,27 +105,50 @@ 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`);
}

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) {
Expand Down