Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit c600716

Browse files
committed
Refactor
1 parent 8045def commit c600716

File tree

2 files changed

+80
-39
lines changed

2 files changed

+80
-39
lines changed

src/ios/RNFetchBlobNetwork.m

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -515,47 +515,46 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
515515
{
516516
errMsg = [error localizedDescription];
517517
}
518-
// else
519-
// {
520-
if(respFile == YES)
518+
519+
if(respFile == YES)
520+
{
521+
[writeStream close];
522+
rnfbRespType = RESP_TYPE_PATH;
523+
respStr = destPath;
524+
}
525+
// base64 response
526+
else {
527+
// #73 fix unicode data encoding issue :
528+
// when response type is BASE64, we should first try to encode the response data to UTF8 format
529+
// if it turns out not to be `nil` that means the response data contains valid UTF8 string,
530+
// in order to properly encode the UTF8 string, use URL encoding before BASE64 encoding.
531+
NSString * utf8 = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
532+
533+
if(responseFormat == BASE64)
521534
{
522-
[writeStream close];
523-
rnfbRespType = RESP_TYPE_PATH;
524-
respStr = destPath;
535+
rnfbRespType = RESP_TYPE_BASE64;
536+
respStr = [respData base64EncodedStringWithOptions:0];
525537
}
526-
// base64 response
527-
else {
528-
// #73 fix unicode data encoding issue :
529-
// when response type is BASE64, we should first try to encode the response data to UTF8 format
530-
// if it turns out not to be `nil` that means the response data contains valid UTF8 string,
531-
// in order to properly encode the UTF8 string, use URL encoding before BASE64 encoding.
532-
NSString * utf8 = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
533-
534-
if(responseFormat == BASE64)
535-
{
536-
rnfbRespType = RESP_TYPE_BASE64;
537-
respStr = [respData base64EncodedStringWithOptions:0];
538-
}
539-
else if (responseFormat == UTF8)
538+
else if (responseFormat == UTF8)
539+
{
540+
rnfbRespType = RESP_TYPE_UTF8;
541+
respStr = utf8;
542+
}
543+
else
544+
{
545+
if(utf8 != nil)
540546
{
541547
rnfbRespType = RESP_TYPE_UTF8;
542548
respStr = utf8;
543549
}
544550
else
545551
{
546-
if(utf8 != nil)
547-
{
548-
rnfbRespType = RESP_TYPE_UTF8;
549-
respStr = utf8;
550-
}
551-
else
552-
{
553-
rnfbRespType = RESP_TYPE_BASE64;
554-
respStr = [respData base64EncodedStringWithOptions:0];
555-
}
552+
rnfbRespType = RESP_TYPE_BASE64;
553+
respStr = [respData base64EncodedStringWithOptions:0];
556554
}
557555
}
558-
// }
556+
}
557+
559558

560559
callback(@[ errMsg, rnfbRespType, respStr]);
561560

test/test-0.10.0.js

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import {
1313
AsyncStorage,
1414
Image,
1515
} from 'react-native';
16+
1617
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
1718
window.Blob = RNFetchBlob.polyfill.Blob
19+
1820
const JSONStream = RNFetchBlob.JSONStream
1921
const fs = RNFetchBlob.fs
2022
const { Assert, Comparer, Info, prop } = RNTest
@@ -99,11 +101,17 @@ describe('SSL test #159', (report, done) => {
99101
'Cache-Control' : 'no-store'
100102
})
101103
.then(res => {
102-
report(<Assert key="trusty request should pass" expect={true} actual={true}/>)
104+
report(<Assert
105+
key="trusty request should pass"
106+
expect={true}
107+
actual={true}/>)
103108
return RNFetchBlob.fetch('GET',`${TEST_SERVER_URL_SSL}/public/github.png`)
104109
})
105110
.catch(e => {
106-
report(<Assert key="non-trusty request should not pass" expect={true} actual={true}/>)
111+
report(<Assert
112+
key="non-trusty request should not pass"
113+
expect={true}
114+
actual={true}/>)
107115
done()
108116
})
109117
})
@@ -118,11 +126,18 @@ describe('#171 appendExt verify', (report, done) => {
118126
'Cache-Control' : 'no-store'
119127
})
120128
.then(res => {
121-
report(<Assert key="extension appended to tmp path" actual={/.png$/.test(res.path())} expect={true}/>)
129+
console.log(res.path())
130+
report(<Assert
131+
key="extension appended to tmp path"
132+
actual={/.png$/.test(res.path())}
133+
expect={true}/>)
122134
return fs.stat(res.path())
123135
})
124136
.then(stat => {
125-
report(<Assert key="verify the file existence" expect="23975" actual={stat.size} />)
137+
report(<Assert
138+
key="verify the file existence"
139+
expect="23975"
140+
actual={stat.size} />)
126141
done()
127142
})
128143

@@ -137,7 +152,10 @@ describe('#173 issue with append option', (report, done) => {
137152
.fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
138153
.then((res) => fs.stat(res.path()))
139154
.then((stat) => {
140-
report(<Assert key="file size check #1" expect="23975" actual={stat.size}/>)
155+
report(<Assert
156+
key="file size check #1"
157+
expect="23975"
158+
actual={stat.size}/>)
141159
return RNFetchBlob.config({
142160
path : dest,
143161
overwrite : false
@@ -146,7 +164,10 @@ describe('#173 issue with append option', (report, done) => {
146164
})
147165
.then((res) => fs.stat(res.path()))
148166
.then((stat) => {
149-
report(<Assert key="file size check #2" expect="47950" actual={stat.size}/>)
167+
report(<Assert
168+
key="file size check #2"
169+
expect="47950"
170+
actual={stat.size}/>)
150171
return RNFetchBlob.config({
151172
path : dest,
152173
overwrite : true
@@ -155,18 +176,39 @@ describe('#173 issue with append option', (report, done) => {
155176
})
156177
.then((res) => fs.stat(res.path()))
157178
.then((stat) => {
158-
report(<Assert key="file size check #3" expect="23975" actual={stat.size}/>)
179+
report(<Assert
180+
key="file size check #3"
181+
expect="23975"
182+
actual={stat.size}/>)
159183
return RNFetchBlob.config({
160184
path : dest,
161185
})
162186
.fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
163187
})
164188
.then((res) => fs.stat(res.path()))
165189
.then((stat) => {
166-
report(<Assert key="it should successfully overwrite existing file without config"
190+
report(<Assert
191+
key="it should successfully overwrite existing file without config"
167192
expect="23975"
168193
actual={stat.size}/>)
169194
done()
170195
})
171196

172197
})
198+
199+
describe('#171 verification ', (report, done) => {
200+
201+
RNFetchBlob
202+
.config({
203+
session: 'SESSION_NAME',
204+
fileCache: true,
205+
appendExt: 'mp4'
206+
})
207+
.fetch('GET', `${TEST_SERVER_URL}/public/cat-fu.mp4`)
208+
.then(res => {
209+
console.log(res.path())
210+
})
211+
212+
213+
214+
})

0 commit comments

Comments
 (0)