Skip to content

Commit b14856e

Browse files
authored
fix: preserve explicit XHR Content-Type header (#250)
Fixes #33
1 parent 8e01af8 commit b14856e

2 files changed

Lines changed: 10 additions & 13 deletions

File tree

lib/fake-xhr/index.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -910,11 +910,8 @@ function fakeXMLHttpRequestFor(globalScope) {
910910
"Content-Type",
911911
);
912912
var FormDataConstructor = getFormDataConstructor();
913-
if (this.requestHeaders[contentType]) {
914-
var value = this.requestHeaders[contentType].split(";");
915-
this.requestHeaders[contentType] =
916-
`${value[0]};charset=utf-8`;
917-
} else if (
913+
if (
914+
!this.requestHeaders[contentType] &&
918915
!(
919916
FormDataConstructor &&
920917
data instanceof FormDataConstructor

lib/fake-xhr/index.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -677,26 +677,26 @@ describe("FakeXMLHttpRequest", function () {
677677
);
678678
});
679679

680-
it("does not override mime", function () {
680+
it("preserves caller-provided Content-Type", function () {
681681
this.xhr.open("POST", "/");
682682
this.xhr.setRequestHeader("Content-Type", "text/html");
683683
this.xhr.send("Data");
684684

685-
assert.equals(
686-
this.xhr.requestHeaders["Content-Type"],
687-
"text/html;charset=utf-8",
688-
);
685+
assert.equals(this.xhr.requestHeaders["Content-Type"], "text/html");
689686
});
690687

691-
it("does not add new 'Content-Type' header if 'content-type' already exists", function () {
688+
it("preserves caller-provided content-type casing and value", function () {
692689
this.xhr.open("POST", "/");
693-
this.xhr.setRequestHeader("content-type", "application/json");
690+
this.xhr.setRequestHeader(
691+
"content-type",
692+
"application/json; charset=iso-8859-1",
693+
);
694694
this.xhr.send("Data");
695695

696696
assert.isUndefined(this.xhr.requestHeaders["Content-Type"]);
697697
assert.equals(
698698
this.xhr.requestHeaders["content-type"],
699-
"application/json;charset=utf-8",
699+
"application/json; charset=iso-8859-1",
700700
);
701701
});
702702

0 commit comments

Comments
 (0)