impulse-api 3.0.7 → 3.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/server.js +6 -4
- package/test/server-test.js +74 -0
package/package.json
CHANGED
package/src/server.js
CHANGED
|
@@ -266,10 +266,12 @@ class Server {
|
|
|
266
266
|
|
|
267
267
|
if (route.inputs) {
|
|
268
268
|
try {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
269
|
+
// When rawBody is true, exclude req.body from inputs processing since it's a Buffer
|
|
270
|
+
const paramsForInputs = route.rawBody === true
|
|
271
|
+
? Object.assign(req.query || {}, req.params || {}, req.files || {})
|
|
272
|
+
: Object.assign(req.query || {}, req.body || {}, req.params || {}, req.files || {});
|
|
273
|
+
|
|
274
|
+
data = this.buildParameters(paramsForInputs, route.inputs);
|
|
273
275
|
} catch (error) {
|
|
274
276
|
sendResponse(error);
|
|
275
277
|
return;
|
package/test/server-test.js
CHANGED
|
@@ -701,5 +701,79 @@ describe('server-test', () => {
|
|
|
701
701
|
await Api.preprocessor(route, req, res);
|
|
702
702
|
assert.strictEqual(res.statusCode, 200);
|
|
703
703
|
});
|
|
704
|
+
|
|
705
|
+
it('should not include Buffer body in inputs processing when rawBody is true', async () => {
|
|
706
|
+
const Api = new Server({
|
|
707
|
+
name: 'test-Server',
|
|
708
|
+
routeDir: './test-routes',
|
|
709
|
+
port: 4000,
|
|
710
|
+
env: 'test',
|
|
711
|
+
services: {}
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
const testBody = Buffer.from(JSON.stringify({ test: 'data', shouldNotBeParsed: true }));
|
|
715
|
+
const req = {
|
|
716
|
+
body: testBody,
|
|
717
|
+
query: { param1: 'value1' },
|
|
718
|
+
params: { id: '123' },
|
|
719
|
+
files: {},
|
|
720
|
+
headers: {},
|
|
721
|
+
get: (header) => {
|
|
722
|
+
if (header === 'origin') return 'http://localhost:4000';
|
|
723
|
+
if (header === 'host') return 'localhost:4000';
|
|
724
|
+
return null;
|
|
725
|
+
}
|
|
726
|
+
};
|
|
727
|
+
const res = {
|
|
728
|
+
status: (code) => {
|
|
729
|
+
res.statusCode = code;
|
|
730
|
+
return res;
|
|
731
|
+
},
|
|
732
|
+
send: (data) => {
|
|
733
|
+
res.sentData = data;
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
|
|
737
|
+
const route = {
|
|
738
|
+
name: 'test-raw-body-excluded-from-inputs',
|
|
739
|
+
method: 'post',
|
|
740
|
+
endpoint: '/test/:id',
|
|
741
|
+
rawBody: true,
|
|
742
|
+
inputs: {
|
|
743
|
+
param1: {
|
|
744
|
+
required: true
|
|
745
|
+
},
|
|
746
|
+
id: {
|
|
747
|
+
required: true
|
|
748
|
+
},
|
|
749
|
+
// This should NOT be found in inputs even though it's in the Buffer body
|
|
750
|
+
test: {
|
|
751
|
+
required: false
|
|
752
|
+
},
|
|
753
|
+
shouldNotBeParsed: {
|
|
754
|
+
required: false
|
|
755
|
+
}
|
|
756
|
+
},
|
|
757
|
+
run: (services, inputs, next) => {
|
|
758
|
+
// Verify rawBody is still a Buffer
|
|
759
|
+
assert.strictEqual(Buffer.isBuffer(inputs.rawBody), true);
|
|
760
|
+
assert.deepStrictEqual(inputs.rawBody, testBody);
|
|
761
|
+
|
|
762
|
+
// Verify inputs from query/params work
|
|
763
|
+
assert.strictEqual(inputs.param1, 'value1');
|
|
764
|
+
assert.strictEqual(inputs.id, '123');
|
|
765
|
+
|
|
766
|
+
// Verify that parsed body fields are NOT in inputs (because Buffer was excluded)
|
|
767
|
+
// buildParameters sets missing optional params to empty string, not undefined
|
|
768
|
+
assert.strictEqual(inputs.test, "");
|
|
769
|
+
assert.strictEqual(inputs.shouldNotBeParsed, "");
|
|
770
|
+
|
|
771
|
+
next(200, { success: true });
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
|
|
775
|
+
await Api.preprocessor(route, req, res);
|
|
776
|
+
assert.strictEqual(res.statusCode, 200);
|
|
777
|
+
});
|
|
704
778
|
});
|
|
705
779
|
});
|