Skip to content

Commit

Permalink
allow headers to be passed as 2nd param to writeHead
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin Luchs committed Jul 29, 2017
1 parent 375e255 commit cab590d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/OutgoingMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ function writeHead(context, statusCode, statusMessage, headers) {
}

// 2. Status message
this.statusMessage = statusMessage || statusCodes[statusCode] || "unknown";
if (typeof statusMessage === "string") {
this.statusMessage = statusMessage;
} else {
this.statusMessage = statusCodes[statusCode] || "unknown";
}

// 3. Headers
if (typeof statusMessage === "object" && typeof headers === "undefined") {
headers = statusMessage; // eslint-disable-line no-param-reassign
}
if (this._headers) {
// Slow-case: when progressive API and header fields are passed.
if (headers) {
Expand Down
22 changes: 22 additions & 0 deletions test/OutgoingMessage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ describe("OutgoingMessage", () => {
expect(context.res.headers).toEqual({ foo: "bar", previous: "previous" });
});

it("Should work with a status message", () => {
const context = { res: {} };
const res = new OutgoingMessage(context);

res.writeHead(200, "baz", { foo: "bar" });

expect(res.statusMessage).toBe("baz");
expect(context.res.status).toBe(200);
expect(context.res.headers).toEqual({ foo: "bar" });
});

it("Should work with a headers as second parameter if no status message is given", () => {
const context = { res: {} };
const res = new OutgoingMessage(context);

res.writeHead(200, { foo: "bar" });

expect(res.statusMessage).toBe("OK");
expect(context.res.status).toBe(200);
expect(context.res.headers).toEqual({ foo: "bar" });
});

});

});

0 comments on commit cab590d

Please sign in to comment.