Skip to content

Commit

Permalink
Ignore null headers.
Browse files Browse the repository at this point in the history
Fixes #180
  • Loading branch information
RubenVerborgh committed Dec 8, 2021
1 parent d01ab7a commit af706be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,12 @@ function removeMatchingHeaders(regex, headers) {
var lastValue;
for (var header in headers) {
if (regex.test(header)) {
lastValue = headers[header].toString().trim();
lastValue = headers[header];
delete headers[header];
}
}
return lastValue;
return (lastValue === null || typeof lastValue === "undefined") ?
undefined : String(lastValue).trim();
}

function createErrorType(code, defaultMessage) {
Expand Down
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,32 @@ describe("follow-redirects", function () {
});

describe("when the client passes an Authorization header", function () {
it("ignores it when null", function () {
app.get("/a", redirectsTo(302, "http://localhost:3600/b"));
app.get("/b", function (req, res) {
res.end(JSON.stringify(req.headers));
});

var opts = url.parse("http://127.0.0.1:3600/a");
opts.headers = {
host: "localhost",
authorization: null,
};

return server.start(app)
.then(asPromise(function (resolve, reject) {
http.get(opts, resolve).on("error", reject);
}))
.then(asPromise(function (resolve, reject, res) {
res.pipe(concat({ encoding: "string" }, resolve)).on("error", reject);
}))
.then(function (str) {
var body = JSON.parse(str);
assert.equal(body.host, "localhost:3600");
assert.equal(body.authorization, undefined);
});
});

it("keeps the header when redirected to the same host", function () {
app.get("/a", redirectsTo(302, "/b"));
app.get("/b", function (req, res) {
Expand Down

0 comments on commit af706be

Please sign in to comment.