@@ -181,6 +181,64 @@ describe('fetchBaseQuery', () => {
181
181
} )
182
182
} )
183
183
184
+ it ( 'success: parse text without error ("content-type" responseHandler)' , async ( ) => {
185
+ server . use (
186
+ rest . get ( 'https://example.com/success' , ( _ , res , ctx ) =>
187
+ res . once (
188
+ ctx . text ( `this is not json!` )
189
+ // NOTE: MSW sets content-type header as text automatically
190
+ )
191
+ )
192
+ )
193
+
194
+ const req = baseQuery (
195
+ {
196
+ url : '/success' ,
197
+ responseHandler : 'content-type' ,
198
+ } ,
199
+ commonBaseQueryApi ,
200
+ { }
201
+ )
202
+ expect ( req ) . toBeInstanceOf ( Promise )
203
+ const res = await req
204
+ expect ( res ) . toBeInstanceOf ( Object )
205
+ expect ( res . meta ?. response ?. headers . get ( 'content-type' ) ) . toEqual (
206
+ 'text/plain'
207
+ )
208
+ expect ( res . meta ?. request ) . toBeInstanceOf ( Request )
209
+ expect ( res . meta ?. response ) . toBeInstanceOf ( Object )
210
+ expect ( res . data ) . toEqual ( `this is not json!` )
211
+ } )
212
+
213
+ it ( 'success: parse json without error ("content-type" responseHandler)' , async ( ) => {
214
+ server . use (
215
+ rest . get ( 'https://example.com/success' , ( _ , res , ctx ) =>
216
+ res . once (
217
+ ctx . json ( `this will become json!` )
218
+ // NOTE: MSW sets content-type header as json automatically
219
+ )
220
+ )
221
+ )
222
+
223
+ const req = baseQuery (
224
+ {
225
+ url : '/success' ,
226
+ responseHandler : 'content-type' ,
227
+ } ,
228
+ commonBaseQueryApi ,
229
+ { }
230
+ )
231
+ expect ( req ) . toBeInstanceOf ( Promise )
232
+ const res = await req
233
+ expect ( res ) . toBeInstanceOf ( Object )
234
+ expect ( res . meta ?. response ?. headers . get ( 'content-type' ) ) . toEqual (
235
+ 'application/json'
236
+ )
237
+ expect ( res . meta ?. request ) . toBeInstanceOf ( Request )
238
+ expect ( res . meta ?. response ) . toBeInstanceOf ( Object )
239
+ expect ( res . data ) . toEqual ( `this will become json!` )
240
+ } )
241
+
184
242
it ( 'server error: should fail normally with a 500 status ("text" responseHandler)' , async ( ) => {
185
243
server . use (
186
244
rest . get ( 'https://example.com/error' , ( _ , res , ctx ) =>
@@ -204,6 +262,62 @@ describe('fetchBaseQuery', () => {
204
262
} )
205
263
} )
206
264
265
+ it ( 'server error: should fail normally with a 500 status as text ("content-type" responseHandler)' , async ( ) => {
266
+ const serverResponse = 'Internal Server Error'
267
+ server . use (
268
+ rest . get ( 'https://example.com/error' , ( _ , res , ctx ) =>
269
+ res ( ctx . status ( 500 ) , ctx . text ( serverResponse ) )
270
+ )
271
+ )
272
+
273
+ const req = baseQuery (
274
+ { url : '/error' , responseHandler : 'content-type' } ,
275
+ commonBaseQueryApi ,
276
+ { }
277
+ )
278
+ expect ( req ) . toBeInstanceOf ( Promise )
279
+ const res = await req
280
+ expect ( res ) . toBeInstanceOf ( Object )
281
+ expect ( res . meta ?. request ) . toBeInstanceOf ( Request )
282
+ expect ( res . meta ?. response ) . toBeInstanceOf ( Object )
283
+ expect ( res . meta ?. response ?. headers . get ( 'content-type' ) ) . toEqual (
284
+ 'text/plain'
285
+ )
286
+ expect ( res . error ) . toEqual ( {
287
+ status : 500 ,
288
+ data : serverResponse ,
289
+ } )
290
+ } )
291
+
292
+ it ( 'server error: should fail normally with a 500 status as json ("content-type" responseHandler)' , async ( ) => {
293
+ const serverResponse = {
294
+ errors : { field1 : "Password cannot be 'password'" } ,
295
+ }
296
+ server . use (
297
+ rest . get ( 'https://example.com/error' , ( _ , res , ctx ) =>
298
+ res ( ctx . status ( 500 ) , ctx . json ( serverResponse ) )
299
+ )
300
+ )
301
+
302
+ const req = baseQuery (
303
+ { url : '/error' , responseHandler : 'content-type' } ,
304
+ commonBaseQueryApi ,
305
+ { }
306
+ )
307
+ expect ( req ) . toBeInstanceOf ( Promise )
308
+ const res = await req
309
+ expect ( res ) . toBeInstanceOf ( Object )
310
+ expect ( res . meta ?. request ) . toBeInstanceOf ( Request )
311
+ expect ( res . meta ?. response ) . toBeInstanceOf ( Object )
312
+ expect ( res . meta ?. response ?. headers . get ( 'content-type' ) ) . toEqual (
313
+ 'application/json'
314
+ )
315
+ expect ( res . error ) . toEqual ( {
316
+ status : 500 ,
317
+ data : serverResponse ,
318
+ } )
319
+ } )
320
+
207
321
it ( 'server error: should fail gracefully (default="json" responseHandler)' , async ( ) => {
208
322
server . use (
209
323
rest . get ( 'https://example.com/error' , ( _ , res , ctx ) =>
0 commit comments