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

XHR: Content-Type clobbered #607

Closed
terinjokes opened this issue Nov 20, 2014 · 13 comments
Closed

XHR: Content-Type clobbered #607

terinjokes opened this issue Nov 20, 2014 · 13 comments

Comments

@terinjokes
Copy link

FakeXMLHttpRequest's send method currently clobbers the "Content-Type" header if set, by removing anything after the first ";" and forcibly appending ";charset=utf-8".

A real XHR object allows sending binary data via TypedArrays, ArrayBuffers, and Blobs. They also correctly form encode when sent with FormData values. As "Content-Type" might have application-specific behavior, blindingly clobbering them makes it difficult for developers to properly test the functionality.

@mroderick
Copy link
Member

@terinjokes Thank you for reporting this.

FakeXMLHttpRequest's send method currently clobbers the "Content-Type" header if set, by removing anything after the first ";" and forcibly appending ";charset=utf-8".

Does this behaviour differ from what out-in-the-wild implementations of XMLHttpRequest? Do they all behave the same way?

A real XHR object allows sending binary data via TypedArrays, ArrayBuffers, and Blobs. They also correctly form encode when sent with FormData values. As "Content-Type" might have application-specific behavior, blindingly clobbering them makes it difficult for developers to properly test the functionality.

By real, do you mean IE10 and onwards? ;) IE9 and below doesn't have the FormData global

Do you have a proposal on how to improve FakeXMLHttpRequest to match developers' expectations, without getting to close to one specific real-world implementation?

@terinjokes
Copy link
Author

Does this behaviour differ from what out-in-the-wild implementations of XMLHttpRequest? Do they all behave the same way?

Just now I've tested the behavior of the following browsers, which were at my easy disposal: IE9, IE10, Chrome 39, Firefox 34, and Safari 8. I created a new XHR object, and set the "Content-Type" header:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://httpbin.org/headers', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function() {
  if (this.readyState==4 || this.readyState=="complete") {
    console.log(this.responseText);
  }
}
xhr.send()

In every case, the response body shows that the browser sent the request with "Content-Type" set to "application/json".

IE9 and below doesn't have the FormData global

That's fine. IE8 doesn't support Date.now, but that doesn't stop FakeTimers from detecting if it exists and faking it if it does.

@moll
Copy link

moll commented May 30, 2015

Just experienced this, too, when using Sinon and its XHR mock on Node. It also lacks the FormData global.

@dstillman
Copy link

Not sure why this was marked as a feature request. This is clearly a bug, as @terinjokes's example shows. This requires ";charset=utf-8" to be appended to all tests that check the sent Content-Type header. (Even "text/plain" shouldn't have a charset appended if it wasn't set explicitly in the XHR.)

@moll
Copy link

moll commented Dec 11, 2015

This is still the case as of Sinon v1.17.2.

@stale
Copy link

stale bot commented Jan 13, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 13, 2018
@moll
Copy link

moll commented Jan 13, 2018

I suspect that is still the case 2 years later unless someone can confirm it's definitely not.

@mroderick
Copy link
Member

Let's investigate this further

@mnahkies
Copy link

mnahkies commented Feb 9, 2018

@mroderick should this move to the nise tracker?

@fatso83
Copy link
Contributor

fatso83 commented Feb 10, 2018

@mnahkies, preferably, yes. If you would do the honours it saves us the trouble :-) Please refer to this bug to keep a history.

@mroderick
Copy link
Member

@mnahkies that would be great, thank you!

I've used this with success in the past: https://github.com/buildo/gh-issue-mover

@mnahkies
Copy link

Issue migrated to sinonjs/nise#33

@fatso83
Copy link
Contributor

fatso83 commented Feb 12, 2018

Closed in favor of the nise bug tracker. AFAIK that's our general workflow, but feel free to reopen, @mroderick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants