-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
44 lines (40 loc) · 1.62 KB
/
test.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
const { requestHooks } = require('./main');
/**
* Create a fake insomnia context with a validation on the setBody
* @param {object} givenBody
* @param {object} expectedBody
* @returns a fake insomnia context with a validation
*/
const fakeContext = (id, givenBody, expectedBody) => {
return {
request: {
getBody: () => givenBody,
setBody: (actualBody) => {
const result = actualBody.text === expectedBody.text
? `${id} | passed`
: `${id} | expected: '${expectedBody.text}' => received: '${actualBody.text}'`;
console.log(result);
},
}
};
};
/**
* List of test cases
*/
const cases = [
// Should not alter non JSON body
[{ mimeType: 'non-json', text: 'foo' }, { mimeType: 'non-json', text: 'foo' }],
// Should not alter JSON body without comments
[{ mimeType: 'application/json', text: 'foo' }, { mimeType: 'application/json', text: 'foo' }],
[{ mimeType: 'application/json', text: '{"foo": "bar"}' }, { mimeType: 'application/json', text: '{"foo": "bar"}' }],
// Should remove comments from JSON body (space are preserved)
[{ mimeType: 'application/json', text: '{"foo": /* hello */ "bar"}' }, { mimeType: 'application/json', text: '{"foo": "bar"}' }],
[{ mimeType: 'application/json', text: '/* hello */ {"foo": "bar"}' }, { mimeType: 'application/json', text: ' {"foo": "bar"}' }],
[{ mimeType: 'application/json', text: '{"foo": "bar" // hello\n }' }, { mimeType: 'application/json', text: '{"foo": "bar" \n }' }],
];
/**
* Call and test the hook
*/
for (const [index, test] of Object.entries(cases)) {
requestHooks[0](fakeContext(index, ...test));
}