2018-06-25 17:42:50 +00:00
|
|
|
const { jarFromCookies, cookiesFromJar } = require('insomnia-cookies');
|
2017-11-26 20:45:40 +00:00
|
|
|
const tag = require('..').templateTags[0];
|
|
|
|
|
|
|
|
describe('plugin', () => {
|
2019-05-08 17:59:16 +00:00
|
|
|
describe('RequestExtension cookie', () => {
|
2017-11-26 20:45:40 +00:00
|
|
|
it('should get cookie by name', async () => {
|
|
|
|
const jar = jarFromCookies([]);
|
2018-06-25 17:42:50 +00:00
|
|
|
jar.setCookieSync(
|
|
|
|
[
|
|
|
|
'foo=bar',
|
|
|
|
'path=/',
|
|
|
|
'domain=.insomnia.rest',
|
2018-12-12 17:36:11 +00:00
|
|
|
'HttpOnly Cache-Control: public, no-cache',
|
2018-06-25 17:42:50 +00:00
|
|
|
].join('; '),
|
2018-12-12 17:36:11 +00:00
|
|
|
'https://insomnia.rest',
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2017-11-26 20:45:40 +00:00
|
|
|
|
|
|
|
const cookies = await cookiesFromJar(jar);
|
2018-10-17 16:42:33 +00:00
|
|
|
const requests = [{ _id: 'req_1', parameters: [], url: 'https://insomnia.rest/foo/bar' }];
|
2018-06-25 17:42:50 +00:00
|
|
|
const jars = [{ _id: 'jar_1', parentId: 'wrk_1', cookies }];
|
|
|
|
const context = _getTestContext([{ _id: 'wrk_1' }], requests, jars);
|
2017-11-26 20:45:40 +00:00
|
|
|
const result = await tag.run(context, 'cookie', 'foo');
|
|
|
|
|
|
|
|
expect(result).toBe('bar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-08 17:59:16 +00:00
|
|
|
describe('RequestExtension url', () => {
|
2017-11-26 20:45:40 +00:00
|
|
|
it('should get url', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [
|
|
|
|
{
|
|
|
|
_id: 'req_1',
|
|
|
|
parameters: [{ name: 'foo', value: 'bar' }],
|
2018-12-12 17:36:11 +00:00
|
|
|
url: 'https://insomnia.rest/foo/bar',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
|
|
|
const context = _getTestContext([{ _id: 'wrk_1' }], requests);
|
2017-11-26 20:45:40 +00:00
|
|
|
const result = await tag.run(context, 'url');
|
|
|
|
|
|
|
|
expect(result).toBe('https://insomnia.rest/foo/bar?foo=bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should get rendered url', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [
|
|
|
|
{
|
|
|
|
_id: 'req_1',
|
|
|
|
parameters: [{ name: 'foo', value: '{{ foo }}' }],
|
2018-12-12 17:36:11 +00:00
|
|
|
url: 'https://insomnia.rest/foo/bar',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
|
|
|
const context = _getTestContext([{ _id: 'wrk_1' }], requests);
|
2017-11-26 20:45:40 +00:00
|
|
|
const result = await tag.run(context, 'url');
|
|
|
|
|
|
|
|
expect(result).toBe('https://insomnia.rest/foo/bar?foo=bar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-05-08 17:59:16 +00:00
|
|
|
describe('RequestExtension header', () => {
|
2017-11-26 20:45:40 +00:00
|
|
|
it('should get url', async () => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const requests = [
|
|
|
|
{
|
|
|
|
_id: 'req_1',
|
|
|
|
headers: [{ name: 'foo', value: '{{ foo }}' }],
|
2018-12-12 17:36:11 +00:00
|
|
|
url: 'https://insomnia.rest/foo/bar',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
];
|
|
|
|
const context = _getTestContext([{ _id: 'wrk_1' }], requests);
|
2017-11-26 20:45:40 +00:00
|
|
|
const result = await tag.run(context, 'header', 'foo');
|
|
|
|
|
|
|
|
expect(result).toBe('bar');
|
|
|
|
});
|
|
|
|
});
|
2018-12-12 15:32:30 +00:00
|
|
|
|
2019-05-08 17:59:16 +00:00
|
|
|
describe('RequestExtension parameter', () => {
|
2018-12-12 15:32:30 +00:00
|
|
|
it('should get parameter', async () => {
|
|
|
|
const requests = [
|
|
|
|
{
|
|
|
|
_id: 'req_1',
|
|
|
|
parameters: [{ name: 'foo', value: '{{ foo }}' }],
|
2018-12-12 17:36:11 +00:00
|
|
|
url: 'https://insomnia.rest/foo/bar',
|
|
|
|
},
|
2018-12-12 15:32:30 +00:00
|
|
|
];
|
|
|
|
const context = _getTestContext([{ _id: 'wrk_1' }], requests);
|
|
|
|
const result = await tag.run(context, 'parameter', 'foo');
|
|
|
|
|
|
|
|
expect(result).toBe('bar');
|
|
|
|
});
|
|
|
|
});
|
2017-11-26 20:45:40 +00:00
|
|
|
});
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
function _getTestContext(workspaces, requests, jars) {
|
2017-11-26 20:45:40 +00:00
|
|
|
jars = jars || [];
|
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
requestId: requests[0]._id,
|
2018-12-12 17:36:11 +00:00
|
|
|
workspaceId: workspaces[0]._id,
|
2017-11-26 20:45:40 +00:00
|
|
|
},
|
|
|
|
util: {
|
2018-06-25 17:42:50 +00:00
|
|
|
render(str) {
|
2017-11-26 20:45:40 +00:00
|
|
|
return str.replace(/{{ foo }}/g, 'bar');
|
|
|
|
},
|
|
|
|
models: {
|
|
|
|
request: {
|
2018-06-25 17:42:50 +00:00
|
|
|
getById(id) {
|
2017-11-26 20:45:40 +00:00
|
|
|
return requests.find(r => r._id === id);
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2017-11-26 20:45:40 +00:00
|
|
|
},
|
|
|
|
workspace: {
|
2018-06-25 17:42:50 +00:00
|
|
|
getById(id) {
|
2017-11-26 20:45:40 +00:00
|
|
|
return workspaces.find(w => w._id === id);
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2017-11-26 20:45:40 +00:00
|
|
|
},
|
|
|
|
cookieJar: {
|
2018-06-25 17:42:50 +00:00
|
|
|
getOrCreateForWorkspace(workspace) {
|
|
|
|
return (
|
|
|
|
jars.find(j => j.parentId === workspace._id) || {
|
|
|
|
parentId: workspace._id,
|
2018-12-12 17:36:11 +00:00
|
|
|
cookies: [],
|
2018-06-25 17:42:50 +00:00
|
|
|
}
|
|
|
|
);
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-11-26 20:45:40 +00:00
|
|
|
};
|
|
|
|
}
|