Aminadav / node-run-middleware

NodeJS Express module to simulate URL requests, for internal executing REST API's
https://github.com/aminag/node-run-middleware
ISC License
65 stars 22 forks source link

node-run-middleware and helmet or cors = errors #38

Closed n4an closed 3 years ago

n4an commented 4 years ago

If use node-run-middleware and with helmet or cors or other which get/ remove header ( helmet remove header: res.removeHeader("X-Powered-By") or res.getHeader('Vary') I get errors:

(node:23833) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'etag' of undefined at ServerResponse.getHeader (_http_outgoing.js:490:24) at ServerResponse.res.get (/var/www/html/selbi.test/node_modules/express/lib/response.js:789:15) at IncomingMessage.fresh (/var/www/html/selbi.test/node_modules/express/lib/request.js:478:19) at ServerResponse.send (/var/www/html/selbi.test/node_modules/express/lib/response.js:206:11) at ServerResponse.json (/var/www/html/selbi.test/node_modules/express/lib/response.js:267:15) at ServerResponse.send (/var/www/html/selbi.test/node_modules/express/lib/response.js:158:21)

(node:23833) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3) TypeError: Cannot read property 'vary' of undefined at ServerResponse.getHeader (_http_outgoing.js:490:24) at vary (/var/www/html/selbi.test/node_modules/vary/index.js:140:17) at applyHeaders (/var/www/html/selbi.test/node_modules/cors/lib/index.js:151:11) at applyHeaders (/var/www/html/selbi.test/node_modules/cors/lib/index.js:149:11) at applyHeaders (/var/www/html/selbi.test/node_modules/cors/lib/index.js:149:11) at cors (/var/www/html/selbi.test/node_modules/cors/lib/index.js:187:7)

n4an commented 4 years ago

Is need to add such functions like:

res.removeHeader = (x) => { return WTF; }

res.getHeader = (x) => { return WTF; }

???

Use of something like this will help to have all methds?? var req =.extend(options, require('express/lib/request'); var res = .extend(options, require('express/lib/response'));

netpedro-com commented 3 years ago

I just update my project and ended up in this same problem. Any solution? @Mitch-i solutions works.

Aminadav commented 3 years ago

If you wish the create a fix, I will review and merge it.

adityak74 commented 3 years ago

@Mitch-i @pedrovagner - Any updates on this one? I am facing the same error and it seems to not like 'vary' in the scope.

n4an commented 3 years ago

@Mitch-i @pedrovagner - Any updates on this one? I am facing the same error and it seems to not like 'vary' in the scope.

I not use that for a year. Don't remember, but I make it to work. Try in https://github.com/Aminadav/node-run-middleware/blob/master/index.js to replace var req =_.extend(options, require('express/lib/request');

var res = { _removedHeader: {}, _statusCode: 200, statusMessage: 'OK', get statusCode() { return this._statusCode }, set statusCode(status) { this._statusCode = status this.status(status) } };

with

var res = _.extend(options, require('express/lib/response'));

adityak74 commented 3 years ago

Thanks @Mitch-i