1
- import fetchMock from 'fetch-mock' ;
2
- import { Response , Headers } from 'node-fetch' ;
1
+ import { Headers , Response } from 'node-fetch' ;
3
2
4
3
import {
5
- HTTP ,
4
+ addHttpResponseInterceptor ,
6
5
getDefaultConfig ,
7
- setDefaultConfig ,
6
+ HTTP ,
8
7
HTTP_RESPONSE_INTERCEPTORS ,
9
- addHttpResponseInterceptor ,
8
+ setDefaultConfig ,
10
9
} from './config' ;
11
- import { httpFetch , handleBody , encodePayload , handleHttpResponse } from './http.common' ;
10
+ import { encodePayload , handleBody , handleHttpResponse , httpFetch } from './http.common' ;
12
11
import { HTTP_METHODS , HTTP_STATUS } from './http.constants' ;
13
12
import { TalendHttpError } from './http.types' ;
14
13
@@ -18,6 +17,10 @@ const defaultPayload = {
18
17
bar : 42 ,
19
18
} ;
20
19
20
+ interface FetchMock extends jest . Mock {
21
+ mockResponse ?: Response ;
22
+ }
23
+
21
24
beforeEach ( ( ) => {
22
25
jest . clearAllMocks ( ) ;
23
26
} ) ;
@@ -199,19 +202,24 @@ describe('#httpFetch with `CSRF` token', () => {
199
202
} ) ;
200
203
201
204
afterAll ( ( ) => {
202
- fetchMock . restore ( ) ;
203
205
document . cookie = `csrfToken=${ CSRFToken } ; dwf_section_edit=True; Max-Age=0` ;
204
206
} ) ;
205
207
it ( 'should get the CRFS token' , async ( ) => {
206
208
const url = '/foo' ;
207
209
const headers = new Headers ( ) ;
208
210
headers . append ( 'Content-Type' , 'application/json' ) ;
209
211
210
- fetchMock . mock ( url , { body : defaultBody , status : 200 } ) ;
212
+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
213
+ status : 200 ,
214
+ headers : {
215
+ 'Content-Type' : 'application/json' ,
216
+ } ,
217
+ } ) ;
218
+
211
219
const result = await httpFetch ( url , { } , HTTP_METHODS . GET , defaultPayload ) ;
212
220
213
221
expect ( result . data ) . toEqual ( defaultBody ) ;
214
- expect ( fetchMock . calls ( ) [ 0 ] [ 1 ] ) . toEqual ( {
222
+ expect ( global . self . fetch ) . toHaveBeenCalledWith ( url , {
215
223
body : JSON . stringify ( defaultPayload ) ,
216
224
credentials : 'same-origin' ,
217
225
headers : {
@@ -239,7 +247,6 @@ describe('#httpFetch with CSRF handling configuration', () => {
239
247
240
248
afterAll ( ( ) => {
241
249
HTTP . defaultConfig = null ;
242
- fetchMock . restore ( ) ;
243
250
document . cookie = `${ defaultHttpConfiguration . security . CSRFTokenCookieKey } =${ CSRFToken } ; dwf_section_edit=True; Max-Age=0` ;
244
251
} ) ;
245
252
@@ -251,12 +258,17 @@ describe('#httpFetch with CSRF handling configuration', () => {
251
258
const headers = new Headers ( ) ;
252
259
headers . append ( 'Content-Type' , 'application/json' ) ;
253
260
254
- fetchMock . mock ( url , { body : defaultBody , status : 200 } ) ;
261
+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
262
+ status : 200 ,
263
+ headers : {
264
+ 'Content-Type' : 'application/json' ,
265
+ } ,
266
+ } ) ;
255
267
256
268
const result = await httpFetch ( url , { } , HTTP_METHODS . GET , defaultPayload ) ;
257
269
258
270
expect ( result . data ) . toEqual ( defaultBody ) ;
259
- expect ( fetchMock . calls ( ) [ 0 ] [ 1 ] ) . toEqual ( {
271
+ expect ( global . self . fetch ) . toHaveBeenCalledWith ( url , {
260
272
body : JSON . stringify ( defaultPayload ) ,
261
273
credentials : 'same-origin' ,
262
274
headers : {
@@ -276,19 +288,23 @@ describe('#httpFetch with CSRF handling configuration', () => {
276
288
describe ( '#httpFetch' , ( ) => {
277
289
afterEach ( ( ) => {
278
290
HTTP . defaultConfig = null ;
279
- fetchMock . restore ( ) ;
280
291
} ) ;
281
292
282
293
it ( 'should fetch the request' , async ( ) => {
283
294
const url = '/foo' ;
284
295
const headers = new Headers ( ) ;
285
296
headers . append ( 'Content-Type' , 'application/json' ) ;
286
- fetchMock . mock ( url , { body : defaultBody , status : 200 } ) ;
297
+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
298
+ status : 200 ,
299
+ headers : {
300
+ 'Content-Type' : 'application/json' ,
301
+ } ,
302
+ } ) ;
287
303
288
304
const result = await httpFetch ( url , { } , HTTP_METHODS . GET , defaultPayload ) ;
289
305
290
306
expect ( result . data ) . toEqual ( defaultBody ) ;
291
- expect ( fetchMock . calls ( ) [ 0 ] [ 1 ] ) . toEqual ( {
307
+ expect ( global . self . fetch ) . toHaveBeenCalledWith ( url , {
292
308
body : JSON . stringify ( defaultPayload ) ,
293
309
credentials : 'same-origin' ,
294
310
headers : {
@@ -310,12 +326,17 @@ describe('#httpFetch', () => {
310
326
} ,
311
327
} ) ;
312
328
313
- fetchMock . mock ( url , { body : defaultBody , status : 200 } ) ;
329
+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
330
+ status : 200 ,
331
+ headers : {
332
+ 'Content-Type' : 'application/json' ,
333
+ } ,
334
+ } ) ;
314
335
315
336
const result = await httpFetch ( url , { } , HTTP_METHODS . GET , defaultPayload ) ;
316
337
317
338
expect ( result . data ) . toEqual ( defaultBody ) ;
318
- expect ( fetchMock . calls ( ) [ 0 ] [ 1 ] ) . toEqual ( {
339
+ expect ( global . self . fetch ) . toHaveBeenCalledWith ( url , {
319
340
body : JSON . stringify ( defaultPayload ) ,
320
341
credentials : 'same-origin' ,
321
342
headers : {
@@ -333,14 +354,22 @@ describe('#httpFetch', () => {
333
354
headers . append ( 'Content-Type' , 'application/json' ) ;
334
355
const payload = new FormData ( ) ;
335
356
336
- fetchMock . mock ( url , { body : '{"foo": 42}' , status : 200 } ) ;
357
+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( { foo : 42 } ) , {
358
+ status : 200 ,
359
+ headers : {
360
+ 'Content-Type' : 'application/json' ,
361
+ } ,
362
+ } ) ;
337
363
338
364
const result = await httpFetch ( url , { } , HTTP_METHODS . GET , payload ) ;
339
- expect ( result . data ) . toEqual ( '{" foo" : 42}' ) ;
365
+ expect ( result . data ) . toEqual ( { foo : 42 } ) ;
340
366
341
- const mockCalls = fetchMock . calls ( ) ;
342
- expect ( mockCalls [ 0 ] [ 1 ] ?. credentials ) . toEqual ( 'same-origin' ) ;
343
- expect ( mockCalls [ 0 ] [ 1 ] ?. headers ) . toEqual ( { Accept : 'application/json' } ) ;
367
+ expect ( global . self . fetch ) . toHaveBeenCalledWith ( url , {
368
+ body : payload ,
369
+ credentials : 'same-origin' ,
370
+ headers : { Accept : 'application/json' } ,
371
+ method : 'GET' ,
372
+ } ) ;
344
373
} ) ;
345
374
} ) ;
346
375
@@ -353,16 +382,17 @@ describe('#httpFetch with interceptors', () => {
353
382
}
354
383
} ) ;
355
384
356
- afterEach ( ( ) => {
357
- fetchMock . restore ( ) ;
358
- } ) ;
359
-
360
385
it ( 'should call interceptor' , async ( ) => {
361
386
const interceptor = jest . fn ( ) . mockImplementation ( ( res , _ ) => res ) ;
362
387
addHttpResponseInterceptor ( 'interceptor' , interceptor ) ;
363
388
364
389
const url = '/foo' ;
365
- fetchMock . mock ( url , { body : defaultBody , status : 200 } ) ;
390
+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
391
+ status : 200 ,
392
+ headers : {
393
+ 'Content-Type' : 'application/json' ,
394
+ } ,
395
+ } ) ;
366
396
367
397
await httpFetch ( url , { } , HTTP_METHODS . GET , { } ) ;
368
398
expect ( interceptor ) . toHaveBeenCalled ( ) ;
@@ -374,8 +404,12 @@ describe('#httpFetch with interceptors', () => {
374
404
375
405
const url = '/foo' ;
376
406
const context = { async : true } ;
377
- const response = { body : defaultBody , status : 200 } ;
378
- fetchMock . mock ( url , response ) ;
407
+ ( global . self . fetch as FetchMock ) . mockResponse = new Response ( JSON . stringify ( defaultBody ) , {
408
+ status : 200 ,
409
+ headers : {
410
+ 'Content-Type' : 'application/json' ,
411
+ } ,
412
+ } ) ;
379
413
380
414
await httpFetch ( url , { context } , HTTP_METHODS . GET , { } ) ;
381
415
expect ( interceptor ) . toHaveBeenCalledWith (
0 commit comments