Browse Source

Merge branch '0.9.5' into 0.10.0

Ben Hsieh 8 years ago
parent
commit
301c8cf7d4

+ 1
- 1
package.json View File

1
 {
1
 {
2
   "name": "fetchblob-dev",
2
   "name": "fetchblob-dev",
3
-  "version": "0.9.4",
3
+  "version": "0.9.5-beta.2",
4
   "private": true,
4
   "private": true,
5
   "scripts": {
5
   "scripts": {
6
     "start": "node node_modules/react-native/local-cli/cli.js start",
6
     "start": "node node_modules/react-native/local-cli/cli.js start",

+ 28
- 3
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

63
         FileStorage
63
         FileStorage
64
     }
64
     }
65
 
65
 
66
+    enum ResponseFormat {
67
+        Auto,
68
+        UTF8,
69
+        BASE64
70
+    }
71
+
66
     public static HashMap<String, Call> taskTable = new HashMap<>();
72
     public static HashMap<String, Call> taskTable = new HashMap<>();
67
     static HashMap<String, Boolean> progressReport = new HashMap<>();
73
     static HashMap<String, Boolean> progressReport = new HashMap<>();
68
     static HashMap<String, Boolean> uploadProgressReport = new HashMap<>();
74
     static HashMap<String, Boolean> uploadProgressReport = new HashMap<>();
83
     RNFetchBlobBody requestBody;
89
     RNFetchBlobBody requestBody;
84
     RequestType requestType;
90
     RequestType requestType;
85
     ResponseType responseType;
91
     ResponseType responseType;
92
+    ResponseFormat responseFormat = ResponseFormat.Auto;
86
     WritableMap respInfo;
93
     WritableMap respInfo;
87
     boolean timeout = false;
94
     boolean timeout = false;
95
+
88
     ArrayList<String> redirects = new ArrayList<>();
96
     ArrayList<String> redirects = new ArrayList<>();
89
 
97
 
90
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
98
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
200
                 while (it.hasNextKey()) {
208
                 while (it.hasNextKey()) {
201
                     String key = it.nextKey();
209
                     String key = it.nextKey();
202
                     String value = headers.getString(key);
210
                     String value = headers.getString(key);
203
-                    builder.header(key, value);
204
-                    mheaders.put(key,value);
211
+                    if(key.equalsIgnoreCase("RNFB-Response")) {
212
+                        if(value.equalsIgnoreCase("base64"))
213
+                            responseFormat = ResponseFormat.BASE64;
214
+                        else if (value.equalsIgnoreCase("utf8"))
215
+                            responseFormat = ResponseFormat.UTF8;
216
+                    }
217
+                    else {
218
+                        builder.header(key, value);
219
+                        mheaders.put(key, value);
220
+                    }
205
                 }
221
                 }
206
             }
222
             }
207
 
223
 
439
                         // string correctly, we should do URL encoding before BASE64.
455
                         // string correctly, we should do URL encoding before BASE64.
440
                         byte[] b = resp.body().bytes();
456
                         byte[] b = resp.body().bytes();
441
                         CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
457
                         CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
458
+                        if(responseFormat == ResponseFormat.BASE64) {
459
+                            callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
460
+                            return;
461
+                        }
442
                         try {
462
                         try {
443
                             encoder.encode(ByteBuffer.wrap(b).asCharBuffer());
463
                             encoder.encode(ByteBuffer.wrap(b).asCharBuffer());
444
                             // if the data contains invalid characters the following lines will be
464
                             // if the data contains invalid characters the following lines will be
449
                         // This usually mean the data is contains invalid unicode characters, it's
469
                         // This usually mean the data is contains invalid unicode characters, it's
450
                         // binary data
470
                         // binary data
451
                         catch(CharacterCodingException ignored) {
471
                         catch(CharacterCodingException ignored) {
452
-                            callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
472
+                            if(responseFormat == ResponseFormat.UTF8) {
473
+                                callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, "");
474
+                            }
475
+                            else {
476
+                                callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
477
+                            }
453
                         }
478
                         }
454
                     }
479
                     }
455
                 } catch (IOException e) {
480
                 } catch (IOException e) {

+ 33
- 5
src/ios/RNFetchBlobNetwork.m View File

27
 NSMutableDictionary * progressTable;
27
 NSMutableDictionary * progressTable;
28
 NSMutableDictionary * uploadProgressTable;
28
 NSMutableDictionary * uploadProgressTable;
29
 
29
 
30
+typedef NS_ENUM(NSUInteger, ResponseFormat) {
31
+    UTF8,
32
+    BASE64,
33
+    AUTO
34
+};
35
+
30
 
36
 
31
 @interface RNFetchBlobNetwork ()
37
 @interface RNFetchBlobNetwork ()
32
 {
38
 {
37
     NSMutableDictionary * respInfo;
43
     NSMutableDictionary * respInfo;
38
     NSInteger respStatus;
44
     NSInteger respStatus;
39
     NSMutableArray * redirects;
45
     NSMutableArray * redirects;
46
+    ResponseFormat responseFormat;
40
 }
47
 }
41
 
48
 
42
 @end
49
 @end
128
     self.options = options;
135
     self.options = options;
129
     redirects = [[NSMutableArray alloc] init];
136
     redirects = [[NSMutableArray alloc] init];
130
     [redirects addObject:req.URL.absoluteString];
137
     [redirects addObject:req.URL.absoluteString];
138
+    
139
+    // set response format
140
+    NSString * rnfbResp = [req.allHTTPHeaderFields valueForKey:@"RNFB-Response"];
141
+    if([[rnfbResp lowercaseString] isEqualToString:@"base64"])
142
+        responseFormat = BASE64;
143
+    else if([[rnfbResp lowercaseString] isEqualToString:@"utf8"])
144
+        responseFormat = UTF8;
145
+    else
146
+        responseFormat = AUTO;
131
 
147
 
132
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
148
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
133
     NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
149
     NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
365
             // if it turns out not to be `nil` that means the response data contains valid UTF8 string,
381
             // if it turns out not to be `nil` that means the response data contains valid UTF8 string,
366
             // in order to properly encode the UTF8 string, use URL encoding before BASE64 encoding.
382
             // in order to properly encode the UTF8 string, use URL encoding before BASE64 encoding.
367
             NSString * utf8 = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
383
             NSString * utf8 = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
368
-
369
-            if(utf8 != nil)
384
+            
385
+            if(responseFormat == BASE64)
386
+            {
387
+                rnfbRespType = RESP_TYPE_BASE64;
388
+                respStr = [respData base64EncodedStringWithOptions:0];
389
+            }
390
+            else if (responseFormat == UTF8)
370
             {
391
             {
371
                 rnfbRespType = RESP_TYPE_UTF8;
392
                 rnfbRespType = RESP_TYPE_UTF8;
372
                 respStr = utf8;
393
                 respStr = utf8;
373
             }
394
             }
374
             else
395
             else
375
             {
396
             {
376
-                rnfbRespType = RESP_TYPE_BASE64;
377
-                respStr = [respData base64EncodedStringWithOptions:0];
397
+                if(utf8 != nil)
398
+                {
399
+                    rnfbRespType = RESP_TYPE_UTF8;
400
+                    respStr = utf8;
401
+                }
402
+                else
403
+                {
404
+                    rnfbRespType = RESP_TYPE_BASE64;
405
+                    respStr = [respData base64EncodedStringWithOptions:0];
406
+                }
378
             }
407
             }
379
-
380
         }
408
         }
381
     }
409
     }
382
 
410
 

+ 1
- 1
src/package.json View File

1
 {
1
 {
2
   "name": "react-native-fetch-blob",
2
   "name": "react-native-fetch-blob",
3
-  "version": "0.9.4",
3
+  "version": "0.9.5-beta.2",
4
   "description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
4
   "description": "A module provides upload, download, and files access API. Supports file stream read/write for process large files.",
5
   "main": "index.js",
5
   "main": "index.js",
6
   "scripts": {
6
   "scripts": {

+ 2
- 2
src/react-native-fetch-blob.podspec View File

1
 Pod::Spec.new do |s|
1
 Pod::Spec.new do |s|
2
   s.name             = "react-native-fetch-blob"
2
   s.name             = "react-native-fetch-blob"
3
-  s.version          = "0.9.4"
3
+  s.version          = "0.9.5-beta.2"
4
   s.summary          = "A project committed to make file acess and data transfer easier, effiecient for React Native developers."
4
   s.summary          = "A project committed to make file acess and data transfer easier, effiecient for React Native developers."
5
   s.requires_arc = true
5
   s.requires_arc = true
6
   s.license      = 'MIT'
6
   s.license      = 'MIT'
7
   s.homepage     = 'n/a'
7
   s.homepage     = 'n/a'
8
   s.authors      = { "wkh237" => "xeiyan@gmail.com" }
8
   s.authors      = { "wkh237" => "xeiyan@gmail.com" }
9
-  s.source       = { :git => "https://github.com/wkh237/react-native-fetch-blob", :tag => 'v0.9.4-beta.3'}
9
+  s.source       = { :git => "https://github.com/wkh237/react-native-fetch-blob", :tag => 'v0.9.5-beta.2'}
10
   s.source_files = 'ios/**/*.{h,m}'
10
   s.source_files = 'ios/**/*.{h,m}'
11
   s.platform     = :ios, "7.0"
11
   s.platform     = :ios, "7.0"
12
   s.dependency 'React/Core'
12
   s.dependency 'React/Core'

+ 54
- 0
test/test-0.9.5.js View File

1
+import RNTest from './react-native-testkit/'
2
+import React from 'react'
3
+import RNFetchBlob from 'react-native-fetch-blob'
4
+import {
5
+  StyleSheet,
6
+  Text,
7
+  View,
8
+  ScrollView,
9
+  Platform,
10
+  Dimensions,
11
+  Image,
12
+  TouchableOpacity,
13
+} from 'react-native';
14
+
15
+window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
16
+window.Blob = RNFetchBlob.polyfill.Blob
17
+
18
+const fs = RNFetchBlob.fs
19
+const { Assert, Comparer, Info, prop } = RNTest
20
+const describe = RNTest.config({
21
+  group : '0.9.5',
22
+  run : true,
23
+  expand : false,
24
+  timeout : 20000,
25
+})
26
+const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
27
+const dirs = RNFetchBlob.fs.dirs
28
+
29
+let prefix = ((Platform.OS === 'android') ? 'file://' : '')
30
+
31
+describe('issue #122 force response data format', (report, done) => {
32
+
33
+  RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/json-dummy.json`, {
34
+    'RNFB-Response' : 'base64'
35
+  })
36
+  .then((res) => {
37
+    let r = RNFetchBlob.base64.decode(res.data)
38
+    report(
39
+      <Assert key="test data verify" expect="fetchblob-dev" actual={JSON.parse(r).name}/>,
40
+      <Assert key="should successfully decode the data" expect={true} actual={true}/>)
41
+    return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/json-dummy.json`)
42
+  })
43
+  .then((res) => {
44
+    report(
45
+      <Assert key="response should in format of plain-text" expect="fetchblob-dev" actual={JSON.parse(res.data).name}/>)
46
+    done()
47
+  })
48
+  .catch(() => {
49
+    report(
50
+      <Assert key="Should successfully decode the data" expect={true} actual={false}/>)
51
+    done()
52
+  })
53
+
54
+})