Преглед на файлове

Add new option `followRedirect` to fetch request and related test case #230

Ben Hsieh преди 8 години
родител
ревизия
fe2f9d2dcf

+ 4
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConfig.java Целия файл

@@ -19,6 +19,7 @@ public class RNFetchBlobConfig {
19 19
     public Boolean overwrite = true;
20 20
     public long timeout = 60000;
21 21
     public Boolean increment = false;
22
+    public Boolean followRedirect = true;
22 23
     public ReadableArray binaryContentTypes = null;
23 24
 
24 25
     RNFetchBlobConfig(ReadableMap options) {
@@ -36,9 +37,11 @@ public class RNFetchBlobConfig {
36 37
         if(this.path != null && path.toLowerCase().contains("?append=true")) {
37 38
             this.overwrite = false;
38 39
         }
39
-
40 40
         if(options.hasKey("overwrite"))
41 41
             this.overwrite = options.getBoolean("overwrite");
42
+        if(options.hasKey("followRedirect")) {
43
+            this.followRedirect = options.getBoolean("followRedirect");
44
+        }
42 45
         this.key = options.hasKey("key") ? options.getString("key") : null;
43 46
         this.mime = options.hasKey("contentType") ? options.getString("contentType") : null;
44 47
         this.increment = options.hasKey("increment") ? options.getBoolean("increment") : false;

+ 2
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java Целия файл

@@ -362,7 +362,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
362 362
 
363 363
             clientBuilder.connectionPool(pool);
364 364
             clientBuilder.retryOnConnectionFailure(false);
365
-            clientBuilder.followRedirects(true);
365
+            clientBuilder.followRedirects(options.followRedirect);
366
+            clientBuilder.followSslRedirects(options.followRedirect);
366 367
 
367 368
 
368 369
             OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();

+ 25
- 9
src/ios/RNFetchBlobNetwork.m Целия файл

@@ -86,6 +86,7 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
86 86
     NSInteger respStatus;
87 87
     NSMutableArray * redirects;
88 88
     ResponseFormat responseFormat;
89
+    BOOL * followRedirect;
89 90
 }
90 91
 
91 92
 @end
@@ -214,6 +215,7 @@ NSOperationQueue *taskQueue;
214 215
     self.expectedBytes = 0;
215 216
     self.receivedBytes = 0;
216 217
     self.options = options;
218
+    followRedirect = [options valueForKey:@"followRedirect"] == nil ? YES : [[options valueForKey:@"followRedirect"] boolValue];
217 219
     isIncrement = [options valueForKey:@"increment"] == nil ? NO : [[options valueForKey:@"increment"] boolValue];
218 220
     redirects = [[NSMutableArray alloc] init];
219 221
     if(req.URL != nil)
@@ -236,8 +238,15 @@ NSOperationQueue *taskQueue;
236 238
     bodyLength = contentLength;
237 239
 
238 240
     // the session trust any SSL certification
239
-//    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
240
-    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:taskId];
241
+    NSURLSessionConfiguration *defaultConfigObject;
242
+    if(!followRedirect)
243
+    {
244
+        defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
245
+    }
246
+    else
247
+    {
248
+        NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:taskId];
249
+    }
241 250
 
242 251
     // set request timeout
243 252
     float timeout = [options valueForKey:@"timeout"] == nil ? -1 : [[options valueForKey:@"timeout"] floatValue];
@@ -275,7 +284,7 @@ NSOperationQueue *taskQueue;
275 284
         respData = [[NSMutableData alloc] init];
276 285
         respFile = NO;
277 286
     }
278
-
287
+    
279 288
     __block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
280 289
     [taskTable setObject:task forKey:taskId];
281 290
     [task resume];
@@ -284,7 +293,7 @@ NSOperationQueue *taskQueue;
284 293
     if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
285 294
         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
286 295
     __block UIApplication * app = [UIApplication sharedApplication];
287
-
296
+    
288 297
     // #115 handling task expired when application entering backgound for a long time
289 298
     UIBackgroundTaskIdentifier tid = [app beginBackgroundTaskWithName:taskId expirationHandler:^{
290 299
         NSLog([NSString stringWithFormat:@"session %@ expired", taskId ]);
@@ -292,7 +301,6 @@ NSOperationQueue *taskQueue;
292 301
         [app endBackgroundTask:tid];
293 302
     }];
294 303
 
295
-
296 304
 }
297 305
 
298 306
 // #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
@@ -575,7 +583,7 @@ NSOperationQueue *taskQueue;
575 583
                 respStr = [respData base64EncodedStringWithOptions:0];
576 584
             }
577 585
         }
578
-        }
586
+    }
579 587
 
580 588
 
581 589
     callback(@[ errMsg, rnfbRespType, respStr]);
@@ -644,9 +652,17 @@ NSOperationQueue *taskQueue;
644 652
 
645 653
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler
646 654
 {
647
-    if(request.URL != nil)
648
-        [redirects addObject:[request.URL absoluteString]];
649
-    completionHandler(request);
655
+    
656
+    if(followRedirect)
657
+    {
658
+        if(request.URL != nil)
659
+            [redirects addObject:[request.URL absoluteString]];
660
+        completionHandler(request);
661
+    }
662
+    else
663
+    {
664
+        completionHandler(nil);
665
+    }
650 666
 }
651 667
 
652 668
 @end

+ 13
- 0
test/test-0.10.2.js Целия файл

@@ -48,3 +48,16 @@ describe('#227 IOS file modification date correctness', (report, done) => {
48 48
   })
49 49
 
50 50
 })
51
+
52
+describe('#230 add and option for setting if the request follow redirect or not', (report, done) => {
53
+
54
+  RNFetchBlob
55
+  .config({ followRedirect : false })
56
+  .fetch('GET',`${TEST_SERVER_URL}/redirect`)
57
+  .then((res) => {
58
+    console.log(res.data)
59
+    report(<Assert key="should not redirect twice" expect={1} actual={res.info().redirects.length}/>);
60
+    done();
61
+  })
62
+
63
+})