-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathshellTransformTest.js
167 lines (141 loc) · 7.08 KB
/
shellTransformTest.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict';
const assert = require('assert'),
behaviors = require('../../../src/models/behaviors'),
Logger = require('../../fakes/fakeLogger'),
fs = require('fs-extra'),
filename = 'test/_shellTransform.js'; // Has to be in test dir so nyc doesn't instrument during coverage runs
describe('behaviors', function () {
describe('#shellTransform', function () {
afterEach(function () {
if (fs.existsSync(filename)) {
fs.unlinkSync(filename);
}
});
it('should not execute during dry run', async function () {
const request = { isDryRun: true },
response = { data: 'ORIGINAL' },
logger = Logger.create(),
config = { shellTransform: 'echo Should not reach here' },
actualResponse = await behaviors.execute(request, response, [config], logger);
assert.deepEqual(actualResponse, { data: 'ORIGINAL' });
});
it('should return output of command', async function () {
const request = {},
response = { data: 'ORIGINAL' },
logger = Logger.create(),
shellFn = function exec () {
console.log(JSON.stringify({ data: 'CHANGED' }));
},
config = { shellTransform: `node ${filename}` };
fs.writeFileSync(filename, `${shellFn.toString()}\nexec();`);
const actualResponse = await behaviors.execute(request, response, [config], logger);
assert.deepEqual(actualResponse, { data: 'CHANGED' });
});
it('should pass request and response to shell command', async function () {
const request = { data: 'FROM REQUEST' },
response = { data: 'UNCHANGED', requestData: '' },
logger = Logger.create(),
shellFn = function exec () {
// The replace of quotes only matters on Windows due to shell differences
const shellRequest = JSON.parse(process.argv[2].replace("'", '')),
shellResponse = JSON.parse(process.argv[3].replace("'", ''));
shellResponse.requestData = shellRequest.data;
console.log(JSON.stringify(shellResponse));
},
config = { shellTransform: `node ${filename}` };
fs.writeFileSync(filename, `${shellFn.toString()}\nexec();`);
const actualResponse = await behaviors.execute(request, response, [config], logger);
assert.deepEqual(actualResponse, { data: 'UNCHANGED', requestData: 'FROM REQUEST' });
});
it('should reject promise if file does not exist', async function () {
const request = {},
response = {},
logger = Logger.create(),
config = { shellTransform: 'fileDoesNotExist' };
try {
await behaviors.execute(request, response, [config], logger);
assert.fail('Promise resolved, should have been rejected');
}
catch (error) {
// Error message is OS-dependent
assert.ok(error.indexOf('fileDoesNotExist') >= 0, error);
}
});
it('should reject if command returned non-zero status code', async function () {
const request = {},
response = {},
logger = Logger.create(),
shellFn = function exec () {
console.error('BOOM!!!');
process.exit(1); // eslint-disable-line no-process-exit
},
config = { shellTransform: `node ${filename}` };
fs.writeFileSync(filename, `${shellFn.toString()}\nexec();`);
try {
await behaviors.execute(request, response, [config], logger);
assert.fail('Promise resolved, should have been rejected');
}
catch (error) {
assert.ok(error.indexOf('Command failed') >= 0, error);
assert.ok(error.indexOf('BOOM!!!') >= 0, error);
}
});
it('should reject if command does not return valid JSON', async function () {
const request = {},
response = {},
logger = Logger.create(),
shellFn = function exec () {
console.log('This is not JSON');
},
config = { shellTransform: `node ${filename}` };
fs.writeFileSync(filename, `${shellFn.toString()}\nexec();`);
try {
await behaviors.execute(request, response, [config], logger);
assert.fail('Promise resolved, should have been rejected');
}
catch (error) {
assert.ok(error.indexOf('Shell command returned invalid JSON') >= 0, error);
}
});
it('should not be valid if not a string', function () {
const errors = behaviors.validate([{ shellTransform: 100 }]);
assert.deepEqual(errors, [{
code: 'bad data',
message: 'shellTransform behavior "shellTransform" field must be a string, representing the path to a command line application',
source: { shellTransform: 100 }
}]);
});
it('should correctly shell quote inner quotes (issue #419)', async function () {
const request = { body: '{"fastSearch": "abctef abc def"}' },
response = {},
logger = Logger.create(),
shellFn = function exec () {
const shellRequest = JSON.parse(process.env.MB_REQUEST),
shellResponse = JSON.parse(process.env.MB_RESPONSE);
shellResponse.requestData = shellRequest.body;
console.log(JSON.stringify(shellResponse));
},
config = { shellTransform: `node ${filename}` };
fs.writeFileSync(filename, `${shellFn.toString()}\nexec();`);
const actualResponse = await behaviors.execute(request, response, [config], logger);
assert.deepEqual(actualResponse, { requestData: '{"fastSearch": "abctef abc def"}' });
});
it('should allow large files (issue #518)', async function () {
const request = { key: 'value' },
response = { field: 0 },
logger = Logger.create(),
shellFn = function exec () {
const shellResponse = JSON.parse(process.env.MB_RESPONSE);
shellResponse.added = new Array(1024 * 1024 * 2).join('x');
console.log(JSON.stringify(shellResponse));
},
config = { shellTransform: `node ${filename}` };
fs.writeFileSync(filename, `${shellFn.toString()}\nexec();`);
const actualResponse = await behaviors.execute(request, response, [config], logger);
assert.deepEqual(actualResponse, {
field: 0,
added: new Array(1024 * 1024 * 2).join('x')
});
});
});
});