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

Commit fe9e909

Browse files
committed
Add progres and cancellation support for Fetch replacement
1 parent 969daaa commit fe9e909

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed

src/polyfill/Fetch.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,31 @@ class RNFetchBlobFetchPolyfill {
4545
else
4646
promise = Promise.resolve(body)
4747

48-
return promise
49-
.then((body) => RNFetchBlob.config(config)
50-
.fetch(options.method, url, options.headers, options.body))
51-
.then((resp) => {
52-
log.verbose('response', resp)
53-
// release blob cache created when sending request
54-
if(blobCache !== null && blobCache instanceof Blob)
55-
blobCache.close()
56-
let info = resp.info()
57-
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
48+
// task is a progress reportable and cancellable Promise, however,
49+
// task.then is not, so we have to extend task.then with progress and
50+
// cancel function
51+
let task = promise
52+
.then((body) => {
53+
return RNFetchBlob.config(config)
54+
.fetch(options.method, url, options.headers, options.body)
5855
})
56+
57+
let statefulPromise = task.then((resp) => {
58+
log.verbose('response', resp)
59+
// release blob cache created when sending request
60+
if(blobCache !== null && blobCache instanceof Blob)
61+
blobCache.close()
62+
let info = resp.info()
63+
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
64+
})
65+
66+
// extend task.then progress with report and cancelling functions
67+
statefulPromise.cancel = task.cancel
68+
statefulPromise.progress = task.progress
69+
statefulPromise.uploadProgress = task.uploadProgress
70+
71+
return statefulPromise
72+
5973
}
6074
}
6175

test/test-fetch.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ describe('GET request test : path -> any', (report, done) => {
112112

113113
})
114114

115-
describe('POST base64 body auto strategy', (report, done) => {
115+
describe('POST different types of body', (report, done) => {
116116

117117
let image = RNTest.prop('image')
118118
let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
@@ -121,14 +121,12 @@ describe('POST base64 body auto strategy', (report, done) => {
121121
let name = `fetch-replacement-${Platform.OS}-${Date.now()}.png`
122122
return pBody.then((body) =>
123123
fetch('https://content.dropboxapi.com/2/files/upload', {
124-
method : method,
124+
method : 'post',
125125
headers : {
126126
Authorization : `Bearer ${DROPBOX_TOKEN}`,
127127
'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
128128
'Content-Type' : 'application/octet-stream'
129-
},
130-
body : body
131-
})
129+
}, body })
132130
)
133131
.then((res) => {
134132
return res.json()
@@ -139,12 +137,42 @@ describe('POST base64 body auto strategy', (report, done) => {
139137
}
140138

141139
let tests = [
142-
upload('upload base64 encoded body', 'post', Promise.resolve(image)),
143-
upload('upload Blob body', 'post', Blob.build(image, 'image/png;BASE64')),
144-
upload('upload file path body', 'post', fs.writeFile(tmpPath, image, 'base64').then(() => Promise.resolve(RNFetchBlob.wrap(tmpPath))))
140+
upload('upload base64 encoded body', Promise.resolve(image)),
141+
upload('upload Blob body', Blob.build(image, 'image/png;BASE64')),
142+
upload('upload file path body', fs.writeFile(tmpPath, image, 'base64').then(() => Promise.resolve(RNFetchBlob.wrap(tmpPath))))
145143
]
146144

147145
Promise.all(tests).then(() => done())
148146

147+
})
148+
149+
describe('check HTTP body correctness', (report, done) => {
150+
151+
let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
152+
153+
function upload(pBody) {
154+
return pBody.then((body) =>
155+
fetch('https://content.dropboxapi.com/2/files/upload', {
156+
method : 'POST',
157+
headers : {
158+
Authorization : `Bearer ${DROPBOX_TOKEN}`,
159+
'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
160+
'Content-Type' : 'application/octet-stream'
161+
}, body })
162+
.then((res) => res.json())
163+
.then((info) => {
164+
165+
})
166+
)
167+
}
168+
169+
170+
let pUnicodeBody = fetch(`${TEST_SERVER_URL}/public/utf8-dummy`, { method : 'GET' })
171+
.then((res) => res.text())
172+
173+
let tests = [
174+
upload(pUnicodeBody)
175+
]
176+
149177

150178
})

test/test-init.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ describe('GET image from server', (report, done) => {
6767
// require('./test-0.7.0')
6868
// require('./test-0.8.0')
6969
// require('./test-0.8.2')
70+
require('./test-fetch')
7071
// require('./test-fs')
71-
require('./test-xmlhttp')
72+
// require('./test-xmlhttp')
7273
// require('./test-blob')
7374
// require('./test-firebase')
7475
// require('./test-android')

0 commit comments

Comments
 (0)