Browse Source

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

Ben Hsieh 8 years ago
parent
commit
fe2f9d2dcf

+ 4
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConfig.java View File

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

+ 2
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

362
 
362
 
363
             clientBuilder.connectionPool(pool);
363
             clientBuilder.connectionPool(pool);
364
             clientBuilder.retryOnConnectionFailure(false);
364
             clientBuilder.retryOnConnectionFailure(false);
365
-            clientBuilder.followRedirects(true);
365
+            clientBuilder.followRedirects(options.followRedirect);
366
+            clientBuilder.followSslRedirects(options.followRedirect);
366
 
367
 
367
 
368
 
368
             OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();
369
             OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();

+ 25
- 9
src/ios/RNFetchBlobNetwork.m View File

86
     NSInteger respStatus;
86
     NSInteger respStatus;
87
     NSMutableArray * redirects;
87
     NSMutableArray * redirects;
88
     ResponseFormat responseFormat;
88
     ResponseFormat responseFormat;
89
+    BOOL * followRedirect;
89
 }
90
 }
90
 
91
 
91
 @end
92
 @end
214
     self.expectedBytes = 0;
215
     self.expectedBytes = 0;
215
     self.receivedBytes = 0;
216
     self.receivedBytes = 0;
216
     self.options = options;
217
     self.options = options;
218
+    followRedirect = [options valueForKey:@"followRedirect"] == nil ? YES : [[options valueForKey:@"followRedirect"] boolValue];
217
     isIncrement = [options valueForKey:@"increment"] == nil ? NO : [[options valueForKey:@"increment"] boolValue];
219
     isIncrement = [options valueForKey:@"increment"] == nil ? NO : [[options valueForKey:@"increment"] boolValue];
218
     redirects = [[NSMutableArray alloc] init];
220
     redirects = [[NSMutableArray alloc] init];
219
     if(req.URL != nil)
221
     if(req.URL != nil)
236
     bodyLength = contentLength;
238
     bodyLength = contentLength;
237
 
239
 
238
     // the session trust any SSL certification
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
     // set request timeout
251
     // set request timeout
243
     float timeout = [options valueForKey:@"timeout"] == nil ? -1 : [[options valueForKey:@"timeout"] floatValue];
252
     float timeout = [options valueForKey:@"timeout"] == nil ? -1 : [[options valueForKey:@"timeout"] floatValue];
275
         respData = [[NSMutableData alloc] init];
284
         respData = [[NSMutableData alloc] init];
276
         respFile = NO;
285
         respFile = NO;
277
     }
286
     }
278
-
287
+    
279
     __block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
288
     __block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
280
     [taskTable setObject:task forKey:taskId];
289
     [taskTable setObject:task forKey:taskId];
281
     [task resume];
290
     [task resume];
284
     if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
293
     if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
285
         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
294
         [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
286
     __block UIApplication * app = [UIApplication sharedApplication];
295
     __block UIApplication * app = [UIApplication sharedApplication];
287
-
296
+    
288
     // #115 handling task expired when application entering backgound for a long time
297
     // #115 handling task expired when application entering backgound for a long time
289
     UIBackgroundTaskIdentifier tid = [app beginBackgroundTaskWithName:taskId expirationHandler:^{
298
     UIBackgroundTaskIdentifier tid = [app beginBackgroundTaskWithName:taskId expirationHandler:^{
290
         NSLog([NSString stringWithFormat:@"session %@ expired", taskId ]);
299
         NSLog([NSString stringWithFormat:@"session %@ expired", taskId ]);
292
         [app endBackgroundTask:tid];
301
         [app endBackgroundTask:tid];
293
     }];
302
     }];
294
 
303
 
295
-
296
 }
304
 }
297
 
305
 
298
 // #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
306
 // #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
575
                 respStr = [respData base64EncodedStringWithOptions:0];
583
                 respStr = [respData base64EncodedStringWithOptions:0];
576
             }
584
             }
577
         }
585
         }
578
-        }
586
+    }
579
 
587
 
580
 
588
 
581
     callback(@[ errMsg, rnfbRespType, respStr]);
589
     callback(@[ errMsg, rnfbRespType, respStr]);
644
 
652
 
645
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler
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
 @end
668
 @end

+ 13
- 0
test/test-0.10.2.js View File

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
+})