2016-10-02 20:57:00 +00:00
|
|
|
import nunjucks from 'nunjucks';
|
|
|
|
import traverse from 'traverse';
|
2016-11-19 03:21:15 +00:00
|
|
|
import uuid from 'uuid';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../models';
|
|
|
|
import {getBasicAuthHeader, hasAuthHeader, setDefaultProtocol} from './misc';
|
2016-11-10 01:15:27 +00:00
|
|
|
import * as db from './database';
|
2016-04-15 02:13:49 +00:00
|
|
|
|
2016-09-13 00:05:04 +00:00
|
|
|
const nunjucksEnvironment = nunjucks.configure({
|
2016-07-20 21:15:11 +00:00
|
|
|
autoescape: false
|
|
|
|
});
|
2016-04-10 02:58:48 +00:00
|
|
|
|
2016-09-13 00:05:04 +00:00
|
|
|
class NoArgsExtension {
|
|
|
|
parse (parser, nodes, lexer) {
|
|
|
|
const args = parser.parseSignature(null, true);
|
|
|
|
parser.skip(lexer.TOKEN_BLOCK_END);
|
|
|
|
return new nodes.CallExtension(this, 'run', args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:00:29 +00:00
|
|
|
// class ArgsExtension {
|
|
|
|
// parse (parser, nodes, lexer) {
|
|
|
|
// const tok = parser.nextToken();
|
|
|
|
// const args = parser.parseSignature(null, true);
|
|
|
|
// parser.advanceAfterBlockEnd(tok.value);
|
|
|
|
// return new nodes.CallExtension(this, 'run', args);
|
|
|
|
// }
|
|
|
|
// }
|
2016-09-13 00:05:04 +00:00
|
|
|
|
|
|
|
class TimestampExtension extends NoArgsExtension {
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
this.tags = ['timestamp'];
|
|
|
|
}
|
|
|
|
|
|
|
|
run (context) {
|
|
|
|
return Date.now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UuidExtension extends NoArgsExtension {
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
this.tags = ['uuid'];
|
|
|
|
}
|
|
|
|
|
|
|
|
run (context) {
|
|
|
|
return uuid.v4();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nunjucksEnvironment.addExtension('uuid', new UuidExtension());
|
|
|
|
nunjucksEnvironment.addExtension('timestamp', new TimestampExtension());
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function render (template, context = {}) {
|
2016-09-03 04:32:45 +00:00
|
|
|
try {
|
2016-09-13 00:05:04 +00:00
|
|
|
return nunjucksEnvironment.renderString(template, context);
|
2016-09-03 04:32:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error(e.message.replace(/\(unknown path\)\s*/, ''));
|
|
|
|
}
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function buildRenderContext (ancestors, rootEnvironment, subEnvironment) {
|
2017-01-13 19:21:03 +00:00
|
|
|
if (!Array.isArray(ancestors)) {
|
|
|
|
ancestors = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const environments = [];
|
2016-09-03 04:32:45 +00:00
|
|
|
|
|
|
|
if (rootEnvironment) {
|
2017-01-13 19:21:03 +00:00
|
|
|
environments.push(rootEnvironment.data);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (subEnvironment) {
|
2017-01-13 19:21:03 +00:00
|
|
|
environments.push(subEnvironment.data);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 19:21:03 +00:00
|
|
|
for (const doc of ancestors.reverse()) {
|
2016-09-03 04:32:45 +00:00
|
|
|
if (!doc.environment) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-13 19:21:03 +00:00
|
|
|
environments.push(doc.environment);
|
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2017-01-24 18:51:25 +00:00
|
|
|
// At this point, environments is a list of environments ordered
|
|
|
|
// from top-most parent to bottom-most child
|
|
|
|
|
2017-01-13 19:21:03 +00:00
|
|
|
const renderContext = {};
|
|
|
|
for (const environment of environments) {
|
2017-01-24 18:51:25 +00:00
|
|
|
// Do an Object.assign, but render each property as it overwrites. This
|
|
|
|
// way we can keep same-name variables from the parent context.
|
|
|
|
_objectDeepAssignRender(renderContext, environment);
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 18:51:25 +00:00
|
|
|
// Render the context with itself to fill in the rest.
|
2017-01-24 18:09:19 +00:00
|
|
|
return recursiveRender(renderContext, renderContext);
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
export function recursiveRender (obj, context) {
|
2016-09-03 04:32:45 +00:00
|
|
|
// Make a copy so no one gets mad :)
|
2016-09-06 16:14:48 +00:00
|
|
|
const newObj = traverse.clone(obj);
|
2016-09-03 04:32:45 +00:00
|
|
|
|
2016-11-29 20:55:31 +00:00
|
|
|
traverse(newObj).forEach(function (x) {
|
|
|
|
try {
|
2016-09-03 04:32:45 +00:00
|
|
|
if (typeof x === 'string') {
|
2016-10-02 20:57:00 +00:00
|
|
|
const str = render(x, context);
|
2016-09-03 04:32:45 +00:00
|
|
|
this.update(str);
|
|
|
|
}
|
2016-11-29 20:55:31 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Failed to render Request
|
|
|
|
const path = this.path.join('.');
|
|
|
|
throw new Error(`Failed to render Request.${path}: "${e.message}"`);
|
|
|
|
}
|
|
|
|
});
|
2016-09-03 04:32:45 +00:00
|
|
|
|
|
|
|
return newObj;
|
2016-10-02 20:57:00 +00:00
|
|
|
}
|
2016-08-15 22:31:30 +00:00
|
|
|
|
2016-11-10 00:01:10 +00:00
|
|
|
export async function getRenderedRequest (request, environmentId) {
|
2016-10-21 17:20:36 +00:00
|
|
|
const ancestors = await db.withAncestors(request);
|
2016-11-10 01:15:27 +00:00
|
|
|
const workspace = ancestors.find(doc => doc.type === models.workspace.type);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2016-11-10 01:15:27 +00:00
|
|
|
const rootEnvironment = await models.environment.getOrCreateForWorkspace(workspace);
|
|
|
|
const subEnvironment = await models.environment.getById(environmentId);
|
|
|
|
const cookieJar = await models.cookieJar.getOrCreateForWorkspace(workspace);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
|
|
|
// Generate the context we need to render
|
|
|
|
const renderContext = buildRenderContext(
|
|
|
|
ancestors,
|
|
|
|
rootEnvironment,
|
|
|
|
subEnvironment
|
|
|
|
);
|
|
|
|
|
|
|
|
// Render all request properties
|
2016-11-07 20:24:38 +00:00
|
|
|
const renderedRequest = recursiveRender(request, renderContext);
|
2016-10-02 20:57:00 +00:00
|
|
|
|
2016-11-22 19:42:10 +00:00
|
|
|
// Remove disabled params
|
|
|
|
renderedRequest.parameters = renderedRequest.parameters.filter(p => !p.disabled);
|
|
|
|
|
|
|
|
// Remove disabled headers
|
|
|
|
renderedRequest.headers = renderedRequest.headers.filter(p => !p.disabled);
|
|
|
|
|
|
|
|
// Remove disabled body params
|
|
|
|
if (renderedRequest.body && Array.isArray(renderedRequest.body.params)) {
|
|
|
|
renderedRequest.body.params = renderedRequest.body.params.filter(p => !p.disabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove disabled authentication
|
|
|
|
if (renderedRequest.authentication && renderedRequest.authentication.disabled) {
|
|
|
|
renderedRequest.authentication = {}
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
// Default the proto if it doesn't exist
|
|
|
|
renderedRequest.url = setDefaultProtocol(renderedRequest.url);
|
|
|
|
|
|
|
|
// Add the yummy cookies
|
|
|
|
renderedRequest.cookieJar = cookieJar;
|
|
|
|
|
|
|
|
// Add authentication
|
|
|
|
const missingAuthHeader = !hasAuthHeader(renderedRequest.headers);
|
|
|
|
if (missingAuthHeader && renderedRequest.authentication.username) {
|
|
|
|
const {username, password} = renderedRequest.authentication;
|
|
|
|
const header = getBasicAuthHeader(username, password);
|
|
|
|
renderedRequest.headers.push(header);
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderedRequest;
|
|
|
|
}
|
2017-01-24 18:51:25 +00:00
|
|
|
|
2017-01-24 18:59:43 +00:00
|
|
|
function _objectDeepAssignRender (base, obj) {
|
|
|
|
for (const key of Object.keys(obj)) {
|
|
|
|
/*
|
|
|
|
* If we're overwriting a string, try to render it first with the base as
|
|
|
|
* a context. This allows for the following scenario:
|
|
|
|
*
|
|
|
|
* base: { base_url: 'google.com' }
|
|
|
|
* obj: { base_url: '{{ base_url }}/foo' }
|
|
|
|
* final: { base_url: 'google.com/foo' }
|
|
|
|
*
|
|
|
|
* A regular Object.assign would yield { base_url: '{{ base_url }}/foo' } and the
|
|
|
|
* original base_url of google.com would be lost.
|
|
|
|
*/
|
|
|
|
if (typeof base[key] === 'string') {
|
|
|
|
base[key] = render(obj[key], base);
|
2017-01-24 18:51:25 +00:00
|
|
|
} else {
|
2017-01-24 18:59:43 +00:00
|
|
|
base[key] = obj[key];
|
2017-01-24 18:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|