-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
index.js
115 lines (107 loc) · 3.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const { jarFromCookies, cookiesFromJar } = require('insomnia-cookies');
const tag = require('..').templateTags[0];
describe('plugin', () => {
describe('CookieJarPlugin: no cookies for url', () => {
it('should get cookie by name', async () => {
const jar = jarFromCookies([]);
jar.setCookieSync(
[
'foo=bar',
'path=/',
'domain=.insomnia.rest',
'HttpOnly Cache-Control: public, no-cache',
].join('; '),
'https://insomnia.rest',
);
const cookies = await cookiesFromJar(jar);
const requests = [{ _id: 'req_1', parameters: [], url: 'https://insomnia.rest/foo/bar' }];
const jars = [{ _id: 'jar_1', parentId: 'wrk_1', cookies }];
const context = _getTestContext([{ _id: 'wrk_1' }], requests, jars);
try {
await tag.run(context, 'https://google.com/', '');
} catch (err) {
expect(err.message).toContain('No cookies in store for url "https://google.com/');
}
});
});
describe('CookieJarPlugin: cookie not found', () => {
it('should get cookie by name', async () => {
const jar = jarFromCookies([]);
jar.setCookieSync(
[
'foo=bar',
'path=/',
'domain=.insomnia.rest',
'HttpOnly Cache-Control: public, no-cache',
].join('; '),
'https://insomnia.rest',
);
const cookies = await cookiesFromJar(jar);
const requests = [{ _id: 'req_1', parameters: [], url: 'https://insomnia.rest/foo/bar' }];
const jars = [{ _id: 'jar_1', parentId: 'wrk_1', cookies }];
const context = _getTestContext([{ _id: 'wrk_1' }], requests, jars);
try {
await tag.run(context, 'https://insomnia.rest', 'bar');
} catch (err) {
expect(err.message).toContain('No cookie with name "bar"');
expect(err.message).toContain('"foo"');
}
});
});
describe('CookieJarPlugin: cookie name found', () => {
it('should get cookie by name', async () => {
const jar = jarFromCookies([]);
jar.setCookieSync(
[
'foo=bar',
'path=/',
'domain=.insomnia.rest',
'HttpOnly Cache-Control: public, no-cache',
].join('; '),
'https://insomnia.rest',
);
const cookies = await cookiesFromJar(jar);
const requests = [{ _id: 'req_1', parameters: [], url: 'https://insomnia.rest/foo/bar' }];
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,
workspaceId: workspaces[0]._id,
},
util: {
render(str) {
return str.replace(/{{ foo }}/g, 'bar');
},
models: {
request: {
getById(id) {
return requests.find(r => r._id === id);
},
},
workspace: {
getById(id) {
return workspaces.find(w => w._id === id);
},
},
cookieJar: {
getOrCreateForWorkspace(workspace) {
return (
jars.find(j => j.parentId === workspace._id) || {
parentId: workspace._id,
cookies: [],
}
);
},
},
},
},
};
}