2018-07-12 18:29:03 +00:00
|
|
|
const { jarFromCookies, cookiesFromJar } = require('insomnia-cookies');
|
|
|
|
const tag = require('..').templateTags[0];
|
|
|
|
|
|
|
|
describe('plugin', () => {
|
|
|
|
describe('CookieJarPlugin: no cookies for url', async () => {
|
|
|
|
it('should get cookie by name', async () => {
|
|
|
|
const jar = jarFromCookies([]);
|
|
|
|
jar.setCookieSync(
|
|
|
|
[
|
|
|
|
'foo=bar',
|
|
|
|
'path=/',
|
|
|
|
'domain=.insomnia.rest',
|
2018-12-12 17:36:11 +00:00
|
|
|
'HttpOnly Cache-Control: public, no-cache',
|
2018-07-12 18:29:03 +00:00
|
|
|
].join('; '),
|
2018-12-12 17:36:11 +00:00
|
|
|
'https://insomnia.rest',
|
2018-07-12 18:29:03 +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-07-12 18:29:03 +00:00
|
|
|
const jars = [{ _id: 'jar_1', parentId: 'wrk_1', cookies }];
|
|
|
|
const context = _getTestContext([{ _id: 'wrk_1' }], requests, jars);
|
|
|
|
try {
|
|
|
|
const result = await tag.run(context, 'https://google.com/', '');
|
2018-10-17 16:42:33 +00:00
|
|
|
} catch (err) {
|
2018-07-12 18:29:03 +00:00
|
|
|
expect(err.message).toContain('No cookies in store for url "https://google.com/');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('CookieJarPlugin: cookie not found', async () => {
|
|
|
|
it('should get cookie by name', async () => {
|
|
|
|
const jar = jarFromCookies([]);
|
|
|
|
jar.setCookieSync(
|
|
|
|
[
|
|
|
|
'foo=bar',
|
|
|
|
'path=/',
|
|
|
|
'domain=.insomnia.rest',
|
2018-12-12 17:36:11 +00:00
|
|
|
'HttpOnly Cache-Control: public, no-cache',
|
2018-07-12 18:29:03 +00:00
|
|
|
].join('; '),
|
2018-12-12 17:36:11 +00:00
|
|
|
'https://insomnia.rest',
|
2018-07-12 18:29:03 +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-07-12 18:29:03 +00:00
|
|
|
const jars = [{ _id: 'jar_1', parentId: 'wrk_1', cookies }];
|
|
|
|
const context = _getTestContext([{ _id: 'wrk_1' }], requests, jars);
|
|
|
|
try {
|
|
|
|
const result = await tag.run(context, 'https://insomnia.rest', 'bar');
|
2018-10-17 16:42:33 +00:00
|
|
|
} catch (err) {
|
2018-07-12 18:29:03 +00:00
|
|
|
expect(err.message).toContain('No cookie with name "bar"');
|
|
|
|
expect(err.message).toContain('"foo"');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('CookieJarPlugin: cookie name found', async () => {
|
|
|
|
it('should get cookie by name', async () => {
|
|
|
|
const jar = jarFromCookies([]);
|
|
|
|
jar.setCookieSync(
|
|
|
|
[
|
|
|
|
'foo=bar',
|
|
|
|
'path=/',
|
|
|
|
'domain=.insomnia.rest',
|
2018-12-12 17:36:11 +00:00
|
|
|
'HttpOnly Cache-Control: public, no-cache',
|
2018-07-12 18:29:03 +00:00
|
|
|
].join('; '),
|
2018-12-12 17:36:11 +00:00
|
|
|
'https://insomnia.rest',
|
2018-07-12 18:29:03 +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-07-12 18:29:03 +00:00
|
|
|
const jars = [{ _id: 'jar_1', parentId: 'wrk_1', cookies }];
|
|
|
|
const context = _getTestContext([{ _id: 'wrk_1' }], requests, jars);
|
|
|
|
const result = await tag.run(context, 'https://insomnia.rest', 'foo');
|
|
|
|
|
|
|
|
expect(result).toBe('bar');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function _getTestContext(workspaces, requests, jars) {
|
|
|
|
jars = jars || [];
|
|
|
|
return {
|
|
|
|
meta: {
|
|
|
|
requestId: requests[0]._id,
|
2018-12-12 17:36:11 +00:00
|
|
|
workspaceId: workspaces[0]._id,
|
2018-07-12 18:29:03 +00:00
|
|
|
},
|
|
|
|
util: {
|
|
|
|
render(str) {
|
|
|
|
return str.replace(/{{ foo }}/g, 'bar');
|
|
|
|
},
|
|
|
|
models: {
|
|
|
|
request: {
|
|
|
|
getById(id) {
|
|
|
|
return requests.find(r => r._id === id);
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2018-07-12 18:29:03 +00:00
|
|
|
},
|
|
|
|
workspace: {
|
|
|
|
getById(id) {
|
|
|
|
return workspaces.find(w => w._id === id);
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
2018-07-12 18:29:03 +00:00
|
|
|
},
|
|
|
|
cookieJar: {
|
|
|
|
getOrCreateForWorkspace(workspace) {
|
|
|
|
return (
|
|
|
|
jars.find(j => j.parentId === workspace._id) || {
|
|
|
|
parentId: workspace._id,
|
2018-12-12 17:36:11 +00:00
|
|
|
cookies: [],
|
2018-07-12 18:29:03 +00:00
|
|
|
}
|
|
|
|
);
|
2018-12-12 17:36:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-07-12 18:29:03 +00:00
|
|
|
};
|
|
|
|
}
|