mirror of
https://github.com/Kong/insomnia
synced 2024-11-07 22:30:15 +00:00
Implement query parameter tag and test (#1301)
This commit is contained in:
parent
7bdd40892d
commit
0e16af8e83
@ -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) {
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user