File tree 2 files changed +18
-3
lines changed
2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -145,8 +145,8 @@ export default class Body {
145
145
* @return Promise
146
146
*/
147
147
async json ( ) {
148
- const buffer = await consumeBody ( this ) ;
149
- return JSON . parse ( buffer . toString ( ) ) ;
148
+ const text = await this . text ( ) ;
149
+ return JSON . parse ( text ) ;
150
150
}
151
151
152
152
/**
@@ -156,7 +156,7 @@ export default class Body {
156
156
*/
157
157
async text ( ) {
158
158
const buffer = await consumeBody ( this ) ;
159
- return buffer . toString ( ) ;
159
+ return new TextDecoder ( ) . decode ( buffer ) ;
160
160
}
161
161
162
162
/**
Original file line number Diff line number Diff line change @@ -77,6 +77,21 @@ describe('Response', () => {
77
77
expect ( res . headers . get ( 'a' ) ) . to . equal ( '1' ) ;
78
78
} ) ;
79
79
80
+ it ( 'should decode responses containing BOM to json' , async ( ) => {
81
+ const json = await new Response ( '\uFEFF{"a":1}' ) . json ( ) ;
82
+ expect ( json . a ) . to . equal ( 1 ) ;
83
+ } ) ;
84
+
85
+ it ( 'should decode responses containing BOM to text' , async ( ) => {
86
+ const text = await new Response ( '\uFEFF{"a":1}' ) . text ( ) ;
87
+ expect ( text ) . to . equal ( '{"a":1}' ) ;
88
+ } ) ;
89
+
90
+ it ( 'should keep BOM when getting raw bytes' , async ( ) => {
91
+ const ab = await new Response ( '\uFEFF{"a":1}' ) . arrayBuffer ( ) ;
92
+ expect ( ab . byteLength ) . to . equal ( 10 ) ;
93
+ } ) ;
94
+
80
95
it ( 'should support text() method' , ( ) => {
81
96
const res = new Response ( 'a=1' ) ;
82
97
return res . text ( ) . then ( result => {
You can’t perform that action at this time.
0 commit comments