fix: handle bom in text and json (#1482)

* fix: handle bom in text and json

* add test for text and arraybuffer as well
This commit is contained in:
Jimmy Wärting 2022-03-11 22:49:14 +01:00 committed by GitHub
parent a4ea5f9308
commit 6425e2021a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -145,8 +145,8 @@ export default class Body {
* @return Promise
*/
async json() {
const buffer = await consumeBody(this);
return JSON.parse(buffer.toString());
const text = await this.text();
return JSON.parse(text);
}
/**
@ -156,7 +156,7 @@ export default class Body {
*/
async text() {
const buffer = await consumeBody(this);
return buffer.toString();
return new TextDecoder().decode(buffer);
}
/**

View File

@ -77,6 +77,21 @@ describe('Response', () => {
expect(res.headers.get('a')).to.equal('1');
});
it('should decode responses containing BOM to json', async () => {
const json = await new Response('\uFEFF{"a":1}').json();
expect(json.a).to.equal(1);
});
it('should decode responses containing BOM to text', async () => {
const text = await new Response('\uFEFF{"a":1}').text();
expect(text).to.equal('{"a":1}');
});
it('should keep BOM when getting raw bytes', async () => {
const ab = await new Response('\uFEFF{"a":1}').arrayBuffer();
expect(ab.byteLength).to.equal(10);
});
it('should support text() method', () => {
const res = new Response('a=1');
return res.text().then(result => {