2016-10-02 20:57:00 +00:00
|
|
|
import * as renderUtils from '../render';
|
2016-11-10 05:56:23 +00:00
|
|
|
import * as models from '../../models';
|
2016-09-03 04:32:45 +00:00
|
|
|
|
|
|
|
jest.mock('electron');
|
|
|
|
|
|
|
|
describe('render()', () => {
|
2017-02-20 18:32:27 +00:00
|
|
|
it('renders hello world', async () => {
|
|
|
|
const rendered = await renderUtils.render('Hello {{ msg }}!', {msg: 'World'});
|
2016-09-03 04:32:45 +00:00
|
|
|
expect(rendered).toBe('Hello World!');
|
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('renders custom tag: uuid', async () => {
|
|
|
|
const rendered = await renderUtils.render('Hello {% uuid %}!');
|
2016-10-21 17:20:36 +00:00
|
|
|
expect(rendered).toMatch(/Hello [a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}!/);
|
2016-09-13 00:05:04 +00:00
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('renders custom tag: timestamp', async () => {
|
|
|
|
const rendered = await renderUtils.render('Hello {% timestamp %}!');
|
2016-09-13 00:05:04 +00:00
|
|
|
expect(rendered).toMatch(/Hello \d{13}!/);
|
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('fails on invalid template', async () => {
|
|
|
|
try {
|
|
|
|
await renderUtils.render('Hello {{ msg }!', {msg: 'World'});
|
|
|
|
fail('Render should have failed');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain('expected variable end');
|
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('buildRenderContext()', () => {
|
2017-02-20 18:32:27 +00:00
|
|
|
it('cascades properly', async () => {
|
2016-11-16 17:18:39 +00:00
|
|
|
const ancestors = [
|
|
|
|
{
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {foo: 'parent', ancestor: true}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {foo: 'grandparent', ancestor: true}
|
|
|
|
},
|
|
|
|
];
|
2016-09-03 04:32:45 +00:00
|
|
|
|
|
|
|
const rootEnvironment = {
|
2016-11-10 01:15:27 +00:00
|
|
|
type: models.environment.type,
|
2016-09-03 04:32:45 +00:00
|
|
|
data: {foo: 'root', root: true}
|
|
|
|
};
|
|
|
|
|
|
|
|
const subEnvironment = {
|
2016-11-10 01:15:27 +00:00
|
|
|
type: models.environment.type,
|
2016-09-03 04:32:45 +00:00
|
|
|
data: {foo: 'sub', sub: true}
|
|
|
|
};
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = await renderUtils.buildRenderContext(
|
2016-09-03 04:32:45 +00:00
|
|
|
ancestors,
|
|
|
|
rootEnvironment,
|
|
|
|
subEnvironment
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(context).toEqual({
|
2016-11-16 17:18:39 +00:00
|
|
|
foo: 'parent',
|
2016-09-03 04:32:45 +00:00
|
|
|
ancestor: true,
|
|
|
|
root: true,
|
|
|
|
sub: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('rendered recursive should not infinite loop', async () => {
|
2017-01-13 19:21:03 +00:00
|
|
|
const ancestors = [{
|
|
|
|
// Sub Environment
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {recursive: '{{ recursive }}/hello'}
|
|
|
|
}];
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = await renderUtils.buildRenderContext(ancestors);
|
2017-01-13 19:21:03 +00:00
|
|
|
|
2017-02-07 16:09:12 +00:00
|
|
|
// This is longer than 3 because it multiplies every time (1 -> 2 -> 4 -> 8)
|
|
|
|
expect(context).toEqual({
|
|
|
|
recursive: '{{ recursive }}/hello/hello/hello/hello/hello/hello/hello/hello'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('render up to 3 recursion levels', async () => {
|
2017-02-07 16:09:12 +00:00
|
|
|
const ancestors = [{
|
|
|
|
// Sub Environment
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {
|
|
|
|
d: '/d',
|
|
|
|
c: '/c{{ d }}',
|
|
|
|
b: '/b{{ c }}',
|
|
|
|
a: '/a{{ b }}',
|
|
|
|
test: 'http://insomnia.rest{{ a }}'
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = await renderUtils.buildRenderContext(ancestors);
|
2017-02-07 16:09:12 +00:00
|
|
|
|
|
|
|
expect(context).toEqual({
|
|
|
|
d: '/d',
|
|
|
|
c: '/c/d',
|
|
|
|
b: '/b/c/d',
|
|
|
|
a: '/a/b/c/d',
|
|
|
|
test: 'http://insomnia.rest/a/b/c/d',
|
|
|
|
});
|
2017-01-20 17:51:18 +00:00
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('rendered sibling environment variables', async () => {
|
2017-01-20 17:51:18 +00:00
|
|
|
const ancestors = [{
|
|
|
|
// Sub Environment
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {
|
|
|
|
sibling: 'sibling',
|
|
|
|
test: '{{ sibling }}/hello'
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = await renderUtils.buildRenderContext(ancestors);
|
2017-01-20 17:51:18 +00:00
|
|
|
|
|
|
|
expect(context).toEqual({sibling: 'sibling', test: 'sibling/hello'});
|
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('rendered parent environment variables', async () => {
|
2017-01-20 17:51:18 +00:00
|
|
|
const ancestors = [{
|
|
|
|
name: 'Parent',
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {
|
|
|
|
test: '{{ grandparent }} parent'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
name: 'Grandparent',
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {
|
|
|
|
grandparent: 'grandparent'
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = await renderUtils.buildRenderContext(ancestors);
|
2017-01-20 17:51:18 +00:00
|
|
|
|
|
|
|
expect(context).toEqual({grandparent: 'grandparent', test: 'grandparent parent'});
|
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('rendered parent same name environment variables', async () => {
|
2017-01-24 18:51:25 +00:00
|
|
|
const ancestors = [{
|
|
|
|
name: 'Parent',
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {
|
|
|
|
base_url: '{{ base_url }}/resource'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
name: 'Grandparent',
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {
|
|
|
|
base_url: 'https://insomnia.rest'
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = await renderUtils.buildRenderContext(ancestors);
|
2017-01-24 18:51:25 +00:00
|
|
|
|
|
|
|
expect(context).toEqual({base_url: 'https://insomnia.rest/resource'});
|
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('rendered parent, ignoring sibling environment variables', async () => {
|
2017-01-24 18:09:19 +00:00
|
|
|
const ancestors = [{
|
|
|
|
name: 'Parent',
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {
|
|
|
|
host: 'parent.com',
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
name: 'Grandparent',
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {
|
|
|
|
host: 'grandparent.com',
|
|
|
|
node: {
|
|
|
|
admin: 'admin',
|
|
|
|
test: 'test',
|
|
|
|
port: 8080,
|
|
|
|
},
|
|
|
|
urls: {
|
|
|
|
admin: 'https://{{ host }}/{{ node.admin }}',
|
|
|
|
test: 'https://{{ host }}/{{ node.test }}',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = await renderUtils.buildRenderContext(ancestors);
|
|
|
|
const result = await renderUtils.render('{{ urls.admin }}/foo', context);
|
2017-01-24 18:09:19 +00:00
|
|
|
|
|
|
|
expect(result).toEqual('https://parent.com/admin/foo');
|
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('renders child environment variables', async () => {
|
2017-01-20 17:51:18 +00:00
|
|
|
const ancestors = [{
|
|
|
|
name: 'Parent',
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {
|
|
|
|
parent: 'parent',
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
name: 'Grandparent',
|
|
|
|
type: models.requestGroup.type,
|
|
|
|
environment: {
|
|
|
|
test: '{{ parent }} grandparent'
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = await renderUtils.buildRenderContext(ancestors);
|
2017-01-20 17:51:18 +00:00
|
|
|
|
2017-01-24 18:09:19 +00:00
|
|
|
expect(context).toEqual({parent: 'parent', test: 'parent grandparent'});
|
2017-01-13 19:21:03 +00:00
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('cascades properly and renders', async () => {
|
2016-11-16 17:18:39 +00:00
|
|
|
const ancestors = [
|
|
|
|
{
|
|
|
|
type: models.requestGroup.type,
|
2017-01-13 19:21:03 +00:00
|
|
|
environment: {
|
2017-01-24 18:09:19 +00:00
|
|
|
url: '{{ base_url }}/resource',
|
2017-01-13 19:21:03 +00:00
|
|
|
ancestor: true,
|
|
|
|
winner: 'folder parent'
|
|
|
|
}
|
2016-11-16 17:18:39 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
type: models.requestGroup.type,
|
2017-01-13 19:21:03 +00:00
|
|
|
environment: {
|
|
|
|
ancestor: true,
|
|
|
|
winner: 'folder grandparent'
|
|
|
|
}
|
2016-11-16 17:18:39 +00:00
|
|
|
}
|
|
|
|
];
|
2016-09-12 21:16:55 +00:00
|
|
|
|
2017-01-24 18:09:19 +00:00
|
|
|
const subEnvironment = {
|
2016-11-10 01:15:27 +00:00
|
|
|
type: models.environment.type,
|
2017-01-24 18:09:19 +00:00
|
|
|
data: {winner: 'sub', sub: true, base_url: 'https://insomnia.rest'}
|
2016-09-12 21:16:55 +00:00
|
|
|
};
|
|
|
|
|
2017-01-24 18:09:19 +00:00
|
|
|
const rootEnvironment = {
|
2016-11-10 01:15:27 +00:00
|
|
|
type: models.environment.type,
|
2017-01-24 18:09:19 +00:00
|
|
|
data: {winner: 'root', root: true, base_url: 'ignore this'}
|
2016-09-12 21:16:55 +00:00
|
|
|
};
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = await renderUtils.buildRenderContext(ancestors,
|
2016-09-12 21:16:55 +00:00
|
|
|
rootEnvironment,
|
|
|
|
subEnvironment
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(context).toEqual({
|
2017-01-24 18:09:19 +00:00
|
|
|
base_url: 'https://insomnia.rest',
|
|
|
|
url: 'https://insomnia.rest/resource',
|
2016-09-12 21:16:55 +00:00
|
|
|
ancestor: true,
|
2017-01-13 19:21:03 +00:00
|
|
|
winner: 'folder parent',
|
2016-09-12 21:16:55 +00:00
|
|
|
root: true,
|
|
|
|
sub: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('works with minimal parameters', async () => {
|
2016-09-03 04:32:45 +00:00
|
|
|
const ancestors = null;
|
|
|
|
const rootEnvironment = null;
|
|
|
|
const subEnvironment = null;
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = await renderUtils.buildRenderContext(
|
2016-09-03 04:32:45 +00:00
|
|
|
ancestors,
|
|
|
|
rootEnvironment,
|
|
|
|
subEnvironment
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(context).toEqual({});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('recursiveRender()', () => {
|
2017-02-20 18:32:27 +00:00
|
|
|
it('correctly renders simple Object', async () => {
|
|
|
|
const newObj = await renderUtils.recursiveRender({
|
2016-09-03 04:32:45 +00:00
|
|
|
foo: '{{ foo }}',
|
|
|
|
bar: 'bar',
|
|
|
|
baz: '{{ bad }}'
|
|
|
|
}, {foo: 'bar'});
|
|
|
|
|
|
|
|
expect(newObj).toEqual({
|
|
|
|
foo: 'bar',
|
|
|
|
bar: 'bar',
|
|
|
|
baz: ''
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('correctly renders complex Object', async () => {
|
2016-09-03 04:32:45 +00:00
|
|
|
const d = new Date();
|
2016-09-06 16:14:48 +00:00
|
|
|
const obj = {
|
2016-09-03 04:32:45 +00:00
|
|
|
foo: '{{ foo }}',
|
|
|
|
null: null,
|
|
|
|
bool: true,
|
|
|
|
date: d,
|
2017-02-20 18:32:27 +00:00
|
|
|
undef: undefined,
|
2016-09-03 04:32:45 +00:00
|
|
|
num: 1234,
|
|
|
|
nested: {
|
|
|
|
foo: '{{ foo }}',
|
2016-11-16 17:18:39 +00:00
|
|
|
arr: [1, 2, '{{ foo }}']
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
2016-09-06 16:14:48 +00:00
|
|
|
};
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
const newObj = await renderUtils.recursiveRender(obj, {foo: 'bar'});
|
2016-09-03 04:32:45 +00:00
|
|
|
|
|
|
|
expect(newObj).toEqual({
|
|
|
|
foo: 'bar',
|
|
|
|
null: null,
|
|
|
|
bool: true,
|
|
|
|
date: d,
|
2017-02-20 18:32:27 +00:00
|
|
|
undef: undefined,
|
2016-09-03 04:32:45 +00:00
|
|
|
num: 1234,
|
|
|
|
nested: {
|
|
|
|
foo: 'bar',
|
2016-11-16 17:18:39 +00:00
|
|
|
arr: [1, 2, 'bar']
|
2016-09-03 04:32:45 +00:00
|
|
|
}
|
2016-09-06 16:14:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Make sure original request isn't changed
|
|
|
|
expect(obj.foo).toBe('{{ foo }}');
|
|
|
|
expect(obj.nested.foo).toBe('{{ foo }}');
|
|
|
|
expect(obj.nested.arr[2]).toBe('{{ foo }}');
|
2016-09-03 04:32:45 +00:00
|
|
|
});
|
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
it('fails on bad template', async () => {
|
|
|
|
try {
|
|
|
|
await renderUtils.recursiveRender({
|
|
|
|
foo: '{{ foo }',
|
|
|
|
bar: 'bar',
|
|
|
|
baz: '{{ bad }}'
|
|
|
|
}, {foo: 'bar'});
|
|
|
|
fail('Render should have failed');
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toContain('expected variable end');
|
|
|
|
}
|
2016-09-03 04:32:45 +00:00
|
|
|
})
|
|
|
|
});
|