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

Case-insensitive check for content type before setting it #18

Merged
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion lib/XMLHttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ function XMLHttpRequest(opts) {
} else if (data) {
headers["Content-Length"] = Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data);

if (!headers["Content-Type"]) {
var headersKeys = Object.keys(headers);
if (!headersKeys.some(function (h) { return h.toLowerCase() === 'content-type' })) {
headers["Content-Type"] = "text/plain;charset=UTF-8";
}
} else if (settings.method === "POST") {
Expand Down
11 changes: 8 additions & 3 deletions tests/test-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var server = http.createServer(function (req, res) {
assert.equal("node-XMLHttpRequest-test", req.headers["user-agent"]);
// Test header set with blacklist disabled
assert.equal("http://github.com", req.headers["referer"]);
// Test case insensitive header was set
assert.equal("text/plain", req.headers["content-type"]);

var body = "Hello World";
res.writeHead(200, {
Expand Down Expand Up @@ -53,13 +55,16 @@ xhr.onreadystatechange = function() {

assert.equal(null, xhr.getResponseHeader("Content-Type"));
try {
xhr.open("GET", "http://localhost:8000/");
xhr.open("POST", "http://localhost:8000/");
var body = "Hello World";
// Valid header
xhr.setRequestHeader("X-Test", "Foobar");
// Invalid header
xhr.setRequestHeader("Content-Length", 0);
xhr.setRequestHeader("Content-Length", Buffer.byteLength(body));
// Allowed header outside of specs
xhr.setRequestHeader("user-agent", "node-XMLHttpRequest-test");
// Case insensitive header
xhr.setRequestHeader("content-type", 'text/plain');
// Test getRequestHeader
assert.equal("Foobar", xhr.getRequestHeader("X-Test"));
// Test invalid header
Expand All @@ -70,7 +75,7 @@ try {
xhr.setRequestHeader("Referer", "http://github.com");
assert.equal("http://github.com", xhr.getRequestHeader("Referer"));

xhr.send();
xhr.send(body);
} catch(e) {
console.log("ERROR: Exception raised", e);
}
Loading