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

Commit 969daaa

Browse files
committed
Add fetch replacement test cases #70
1 parent fef20b5 commit 969daaa

File tree

1 file changed

+83
-6
lines changed

1 file changed

+83
-6
lines changed

test/test-fetch.js

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ const dirs = RNFetchBlob.fs.dirs
3131

3232
let prefix = ((Platform.OS === 'android') ? 'file://' : '')
3333

34-
describe('GET request test : text -> any', (report, done) => {
34+
describe('GET request test : unicode text -> any', (report, done) => {
3535

3636
function get(fn1, fn2) {
3737
return fetch(`${TEST_SERVER_URL}/unicode`, { method : 'GET'})
3838
.then((res) => fn1(res))
3939
.then((data) => fn2(data))
4040
}
41+
4142
let promises =
4243
[
4344
get((res) => res.json(), (json) => {
@@ -47,9 +48,7 @@ describe('GET request test : text -> any', (report, done) => {
4748
report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
4849
}),
4950
get((res) => res.blob(), (blob) => {
50-
let path = blob.getRNFetchBlobRef()
51-
return fs.readFile(path, 'utf8').then((text) => {
52-
console.log(text)
51+
return blob.readBlob('utf8').then((text) => {
5352
report(<Assert key="blob data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
5453
})
5554
}),
@@ -64,10 +63,88 @@ describe('GET request test : text -> any', (report, done) => {
6463

6564
})
6665

67-
describe('GET request which has json response', (report, done) => {
66+
describe('GET request test : path -> any', (report, done) => {
67+
68+
function get(fn1, fn2, fn3) {
69+
fetch(`${TEST_SERVER_URL}/public/github.png`, { method : 'GET'})
70+
.then((res) => fn1(res))
71+
.then((data) => fn2(data))
72+
.catch((err) => fn3(err))
73+
}
74+
let contentLength = 0
75+
let promises = [
76+
get((res) => res.json(), (data) => {
77+
report(<Assert key="should not convert blob to JSON" expect={true} actual={false} />)
78+
}, (err) => {
79+
report(<Assert key="should not convert blob to JSON" expect={true} actual={true} />)
80+
}),
81+
get((res) => {
82+
contentLength = res.headers['Content-Length']
83+
return res.text()
84+
}, (data) => {
85+
report(
86+
<Assert key="should convert blob to text" expect={true} actual={true} />,
87+
<Assert key="content length should correct" expect={Math.floor(contentLength)} actual={data.length} />)
88+
}, (err) => {
89+
console.warn(err, err.stack)
90+
report(<Assert key="should convert blob to text" expect={true} actual={false} />)
91+
}),
92+
get((res) => {
93+
contentLength = res.headers['Content-Length']
94+
return res.blob()
95+
}, (blob) => {
96+
return fs.stat(blob.getRNFetchBlobRef()).then((stat) => {
97+
report(<Assert key="stored file size correct" expect={contentLength} actual={stat.size} />)
98+
return blob.readBlob('base64')
99+
})
100+
.then((b64) => {
101+
report(<Info key="stored image">
102+
<Image style={styles.image} source={{uri : 'data:image/png;BASE64 ,' + b64}}/>
103+
</Info>)
104+
})
105+
106+
}, (err) => {
107+
console.warn(err, err.stack)
108+
report(<Assert key="should convert blob to blob" expect={true} actual={false} />)
109+
})
110+
]
111+
Promise.all(promises).then( () => done() )
68112

69113
})
70114

71-
describe('GET request which has blob response', (report, done) => {
115+
describe('POST base64 body auto strategy', (report, done) => {
116+
117+
let image = RNTest.prop('image')
118+
let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
119+
120+
function upload(desc, method, pBody) {
121+
let name = `fetch-replacement-${Platform.OS}-${Date.now()}.png`
122+
return pBody.then((body) =>
123+
fetch('https://content.dropboxapi.com/2/files/upload', {
124+
method : method,
125+
headers : {
126+
Authorization : `Bearer ${DROPBOX_TOKEN}`,
127+
'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
128+
'Content-Type' : 'application/octet-stream'
129+
},
130+
body : body
131+
})
132+
)
133+
.then((res) => {
134+
return res.json()
135+
})
136+
.then((info) => {
137+
report(<Assert key={desc} expect={name} actual={info.name}/>)
138+
})
139+
}
140+
141+
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))))
145+
]
146+
147+
Promise.all(tests).then(() => done())
148+
72149

73150
})

0 commit comments

Comments
 (0)