Browse Source

Change #29 IOS implementation

Ben Hsieh 8 years ago
parent
commit
9ecee65901
5 changed files with 107 additions and 24 deletions
  1. 0
    1
      src/ios/RNFetchBlob/RNFetchBlob.m
  2. 14
    5
      src/ios/RNFetchBlobNetwork.m
  3. 0
    13
      test/test-0.1.x-0.4.x.js
  4. 87
    0
      test/test-0.5.3.js
  5. 6
    5
      test/test-init.js

+ 0
- 1
src/ios/RNFetchBlob/RNFetchBlob.m View File

@@ -160,7 +160,6 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
160 160
                 if([body hasPrefix:self.filePathPrefix]) {
161 161
                     NSString * orgPath = [body substringFromIndex:[self.filePathPrefix length]];
162 162
                     [request setHTTPBodyStream: [NSInputStream inputStreamWithFileAtPath:orgPath ]];
163
-//                    blobData = [[NSData alloc] initWithContentsOfFile:orgPath];
164 163
                 }
165 164
                 // otherwise convert it as BASE64 data string
166 165
                 else {

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

@@ -71,10 +71,19 @@ NSOperationQueue *taskQueue;
71 71
     
72 72
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
73 73
     NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
74
-    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
75
-    NSURLSession * session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:taskQueue];
74
+    NSURLSession * session;
76 75
     
77
-//    NSURLSession * session = [NSURLSession sharedSession];
76
+    // the session trust any SSL certification
77
+    if([options valueForKey:CONFIG_TRUSTY] != nil)
78
+    {
79
+        NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
80
+        session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:taskQueue];
81
+    }
82
+    // the session validates SSL certification, self-signed certification will be aborted
83
+    else
84
+    {
85
+        session = [NSURLSession sharedSession];
86
+    }
78 87
     
79 88
     // file will be stored at a specific path
80 89
     if( path != nil) {
@@ -210,10 +219,10 @@ NSOperationQueue *taskQueue;
210 219
 
211 220
 - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
212 221
 {
213
-    if([options valueForKey:CONFIG_TRUSTY] == YES)
222
+    if([options valueForKey:CONFIG_TRUSTY] != nil)
214 223
         completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
215 224
     else
216
-        RCTLogError(@"counld not create connection with an unstrusted SSL certification, if you're going to create connection anyway, add `trusty:true` to RNFetchBlob.config");
225
+        RCTLogWarn(@"counld not create connection with an unstrusted SSL certification, if you're going to create connection anyway, add `trusty:true` to RNFetchBlob.config");
217 226
 }
218 227
 
219 228
 @end

+ 0
- 13
test/test-0.1.x-0.4.x.js View File

@@ -114,16 +114,3 @@ describe('Progress report test', (report, done) => {
114 114
     })
115 115
 
116 116
 })
117
-
118
-// FIXME : not yet supported
119
-// describe('Large file download test', (report, done) => {
120
-  // let received = 0
121
-  // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
122
-  //   Authorization : 'Bearer abde123eqweje'
123
-  // })
124
-  // .then((resp) => {
125
-    // report(<Assert key="not supported" expect={true} actual={false}/>)
126
-    // done()
127
-  // })
128
-
129
-// })

+ 87
- 0
test/test-0.5.3.js View File

@@ -0,0 +1,87 @@
1
+import RNTest from './react-native-testkit/'
2
+import React from 'react'
3
+import RNFetchBlob from 'react-native-fetch-blob'
4
+
5
+import {
6
+  StyleSheet,
7
+  Text,
8
+  View,
9
+  ScrollView,
10
+  Platform,
11
+  Dimensions,
12
+  Image,
13
+} from 'react-native';
14
+
15
+const fs = RNFetchBlob.fs
16
+const { Assert, Comparer, Info, prop } = RNTest
17
+const describe = RNTest.config({
18
+  group : '0.5.3',
19
+  run : true,
20
+  expand : false,
21
+})
22
+const { TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
23
+
24
+let prefix = ((Platform.OS === 'android') ? 'file://' : '')
25
+
26
+
27
+describe('GET request with params', (report, done) => {
28
+  let time = Date.now()
29
+  RNFetchBlob
30
+  .config({ fileCache : true, trusty : true })
31
+    .fetch('GET', `${TEST_SERVER_URL_SSL}/params?time=${time}&name=RNFetchBlobParams&lang=中文`)
32
+    .then((resp) => {
33
+      let file = resp.path()
34
+      return RNFetchBlob.fs.readStream(resp.path(), 'utf8')
35
+    })
36
+    .then((stream) => {
37
+      let result = ''
38
+      stream.open()
39
+      stream.onData((chunk) => {
40
+        result += chunk
41
+      })
42
+      stream.onEnd(() => {
43
+        result = JSON.parse(result)
44
+        report(<Assert key="param#1 should correct"
45
+          expect={parseInt(time)}
46
+          actual={parseInt(result.time)}/>,
47
+        <Assert key="param#2 should correct"
48
+          expect={'RNFetchBlobParams'}
49
+          actual={result.name}/>,
50
+        <Assert key="param contains unicode data should correct"
51
+          expect={'中文'}
52
+          actual={result.lang}/>)
53
+          done()
54
+      })
55
+    })
56
+})
57
+
58
+
59
+describe('POST request with params', (report, done) => {
60
+  let time = Date.now()
61
+  RNFetchBlob.config({ fileCache : true, trusty : true })
62
+    .fetch('POST', `${TEST_SERVER_URL_SSL}/params?time=${time}&name=RNFetchBlobParams&lang=中文`)
63
+    .then((resp) => {
64
+      let file = resp.path()
65
+      return RNFetchBlob.fs.readStream(resp.path(), 'utf8')
66
+    })
67
+    .then((stream) => {
68
+      let result = ''
69
+      stream.open()
70
+      stream.onData((chunk) => {
71
+        result += chunk
72
+      })
73
+      stream.onEnd(() => {
74
+        result = JSON.parse(result)
75
+        report(<Assert key="param#1 should correct"
76
+          expect={parseInt(time)}
77
+          actual={parseInt(result.time)}/>,
78
+        <Assert key="param#2 should correct"
79
+          expect={'RNFetchBlobParams'}
80
+          actual={result.name}/>,
81
+        <Assert key="param contains unicode data should correct"
82
+          expect={'中文'}
83
+          actual={result.lang}/>)
84
+          done()
85
+      })
86
+    })
87
+})

+ 6
- 5
test/test-init.js View File

@@ -51,8 +51,9 @@ describe('GET image from server', (report, done) => {
51 51
     })
52 52
 })
53 53
 
54
-// require('./test-0.1.x-0.4.x')
55
-// require('./test-0.5.1')
56
-// require('./test-0.5.2')
57
-// require('./test-fs')
58
-require('./test-android')
54
+require('./test-0.1.x-0.4.x')
55
+require('./test-0.5.1')
56
+require('./test-0.5.2')
57
+require('./test-0.5.3')
58
+require('./test-fs')
59
+// require('./test-android')