@@ -221,6 +221,9 @@ describe('RESTController', () => {
221
221
getResponseHeader : function ( header ) {
222
222
return headers [ header ] ;
223
223
} ,
224
+ getAllResponseHeaders : function ( ) {
225
+ return Object . keys ( headers ) . map ( key => `${ key } : ${ headers [ key ] } ` ) . join ( '\n' ) ;
226
+ } ,
224
227
send : function ( ) {
225
228
this . status = 200 ;
226
229
this . responseText = '{}' ;
@@ -241,6 +244,9 @@ describe('RESTController', () => {
241
244
getResponseHeader : function ( header ) {
242
245
return headers [ header ] ;
243
246
} ,
247
+ getAllResponseHeaders : function ( ) {
248
+ return Object . keys ( headers ) . map ( key => `${ key } : ${ headers [ key ] } ` ) . join ( '\n' ) ;
249
+ } ,
244
250
send : function ( ) {
245
251
this . status = 200 ;
246
252
this . responseText = '{}' ;
@@ -253,6 +259,63 @@ describe('RESTController', () => {
253
259
expect ( response . _headers [ 'X-Parse-Push-Status-Id' ] ) . toBe ( '5678' ) ;
254
260
} ) ;
255
261
262
+ it ( 'does not call getRequestHeader with no headers or no getAllResponseHeaders' , async ( ) => {
263
+ const XHR = function ( ) { } ;
264
+ XHR . prototype = {
265
+ open : function ( ) { } ,
266
+ setRequestHeader : function ( ) { } ,
267
+ getResponseHeader : jest . fn ( ) ,
268
+ send : function ( ) {
269
+ this . status = 200 ;
270
+ this . responseText = '{"result":"hello"}' ;
271
+ this . readyState = 4 ;
272
+ this . onreadystatechange ( ) ;
273
+ } ,
274
+ } ;
275
+ RESTController . _setXHR ( XHR ) ;
276
+ await RESTController . request ( 'GET' , 'classes/MyObject' , { } , { } ) ;
277
+ expect ( XHR . prototype . getResponseHeader . mock . calls . length ) . toBe ( 0 ) ;
278
+
279
+ XHR . prototype . getAllResponseHeaders = jest . fn ( ) ;
280
+ await RESTController . request ( 'GET' , 'classes/MyObject' , { } , { } ) ;
281
+ expect ( XHR . prototype . getAllResponseHeaders . mock . calls . length ) . toBe ( 1 ) ;
282
+ expect ( XHR . prototype . getResponseHeader . mock . calls . length ) . toBe ( 0 ) ;
283
+ } ) ;
284
+
285
+ it ( 'does not invoke Chrome browser console error on getResponseHeader' , async ( ) => {
286
+ const headers = {
287
+ 'access-control-expose-headers' : 'a, b, c' ,
288
+ 'a' : 'value' ,
289
+ 'b' : 'value' ,
290
+ 'c' : 'value' ,
291
+ }
292
+ const XHR = function ( ) { } ;
293
+ XHR . prototype = {
294
+ open : function ( ) { } ,
295
+ setRequestHeader : function ( ) { } ,
296
+ getResponseHeader : jest . fn ( key => {
297
+ if ( Object . keys ( headers ) . includes ( key ) ) {
298
+ return headers [ key ] ;
299
+ }
300
+ throw new Error ( "Chrome creates a console error here." ) ;
301
+ } ) ,
302
+ getAllResponseHeaders : jest . fn ( ( ) => {
303
+ return Object . keys ( headers ) . map ( key => `${ key } : ${ headers [ key ] } ` ) . join ( '\r\n' ) ;
304
+ } ) ,
305
+ send : function ( ) {
306
+ this . status = 200 ;
307
+ this . responseText = '{"result":"hello"}' ;
308
+ this . readyState = 4 ;
309
+ this . onreadystatechange ( ) ;
310
+ } ,
311
+ } ;
312
+ RESTController . _setXHR ( XHR ) ;
313
+ await RESTController . request ( 'GET' , 'classes/MyObject' , { } , { } ) ;
314
+ expect ( XHR . prototype . getAllResponseHeaders . mock . calls . length ) . toBe ( 1 ) ;
315
+ expect ( XHR . prototype . getResponseHeader . mock . calls . length ) . toBe ( 4 ) ;
316
+ } ) ;
317
+
318
+
256
319
it ( 'handles invalid header' , async ( ) => {
257
320
const XHR = function ( ) { } ;
258
321
XHR . prototype = {
0 commit comments