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

Responsetype "arraybuffer" not implemented correctly #104

Open
gflohr opened this issue May 4, 2020 · 0 comments
Open

Responsetype "arraybuffer" not implemented correctly #104

gflohr opened this issue May 4, 2020 · 0 comments

Comments

@gflohr
Copy link

gflohr commented May 4, 2020

In my (jest) test I mock XHR like this:

mock.get('/assets/locale/de/LC_MESSAGES/http.json', {
    status: 200,
    body: 'some string',
});

I use XMLHTTPRequest in my code in like this:

loadFile(url: string): Promise<ArrayBuffer> {
	return new Promise((resolve, reject) => {
		const xhr = new XMLHttpRequest();
		xhr.responseType = 'arraybuffer';
		xhr.open('GET', url, true);
		xhr.onload = () => {
			if (xhr.readyState === 4 && xhr.status === 200) {
				resolve(xhr.response);
			} else {
				reject(new Error('get failed with status ' + xhr.status));
			}
		};
		xhr.onerror = (err) => reject(err);
		xhr.send();
	});
}

In the browser, the response data is in xhr.response (not xhr.responseType!)) as an ArrayBuffer, and accessing xhr.responseText throws an exception. In the mock, the data is a string in xhr.responseText but xhr.response is null.

This looks like a bug to me, see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Handling_binary_data. Any chance to fix this?

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

No branches or pull requests

1 participant