Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve virtual request parsing #7287

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions lib/router/req.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,27 @@ module.exports = function buildRequest (_req) {
}
else {

// TODO: send a PR to mock-req with a fix for this
if (_req.headers && typeof _req.headers === 'object') {
// Strip undefined headers
_.each(_req.headers, function (headerVal, headerKey) {
if (_.isUndefined(headerVal)){
for (let headerKey of Object.keys(_req.headers)) {
// Strip undefined headers
if (undefined === _req.headers[headerKey]) {
delete _req.headers[headerKey];
}
});
// Make sure all remaining headers are strings
_req.headers = _.mapValues(_req.headers, function (headerVal /*, headerKey*/) {
if (typeof headerVal !== 'string') {
headerVal = ''+headerVal+'';
// Make sure all remaining headers are strings
if (typeof _req.headers[headerKey] !== 'string') {
try {
_req.headers[headerKey] = ''+_req.headers[headerKey];
// FUTURE: This behavior is likely being relied upon by apps, so we can't just change it.
// But in retrospect, it would probably be better to straight-up reject this here if it's not
// a string, since HTTP header values are always supposed to be strings; or at least primitives.
// So maybe reject non-primitives, reject `null`, and then accept primitives, but be smart about
// this, especially in the context of what the client is doing.
} catch (unusedErr) {
delete _req.headers[headerKey];
}
}
return headerVal;
});
}
}//∞
}//fi

// Create a mock IncomingMessage stream.
req = new MockReq({
Expand Down