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

Commit 0fcdf3c

Browse files
committed
Change test case #70
1 parent 273f8ee commit 0fcdf3c

File tree

1 file changed

+154
-156
lines changed

1 file changed

+154
-156
lines changed

test/test-fetch.js

Lines changed: 154 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -32,161 +32,160 @@ const dirs = RNFetchBlob.fs.dirs
3232

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

35-
// describe('GET request test : unicode text -> any', (report, done) => {
36-
//
37-
// function get(fn1, fn2) {
38-
// return fetch(`${TEST_SERVER_URL}/unicode`, { method : 'GET'})
39-
// .then((res) => fn1(res))
40-
// .then((data) => fn2(data))
41-
// }
42-
//
43-
// let promises =
44-
// [
45-
// get((res) => res.json(), (json) => {
46-
// report(<Assert key="json data correct" expect={'你好!'} actual={json.data}/>)
47-
// }),
48-
// get((res) => res.text(), (text) => {
49-
// report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
50-
// }),
51-
// get((res) => res.blob(), (blob) => {
52-
// return blob.readBlob('utf8').then((text) => {
53-
// report(<Assert key="blob data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
54-
// })
55-
// }),
56-
// // get((res) => res.arrayBuffer(), (text) => {
57-
// // report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
58-
// // })
59-
// ]
60-
//
61-
// Promise.all(promises).then(() => {
62-
// done()
63-
// })
64-
//
65-
// })
66-
//
67-
// describe('GET request test : path -> any', (report, done) => {
68-
//
69-
// function get(fn1, fn2, fn3) {
70-
// fetch(`${TEST_SERVER_URL}/public/github.png`, { method : 'GET'})
71-
// .then((res) => fn1(res))
72-
// .then((data) => fn2(data))
73-
// .catch((err) => fn3(err))
74-
// }
75-
// let contentLength = 0
76-
// let isIOS = Platform.OS === 'ios'
77-
// let promises = [
78-
// // FIXME: IOS only
79-
// // https://github.com/facebook/react-native/issues/9178
80-
// get((res) => {
81-
// if(isIOS)
82-
// return res.json()
83-
// return Promise.resolve()
84-
// }, (data) => {
85-
// report(<Assert key="should not convert blob to JSON (IOS only)" expect={true} actual={false} />)
86-
// }, (err) => {
87-
// report(<Assert key="should not convert blob to JSON (IOS only)" expect={true} actual={true} />)
88-
// }),
89-
// // FIXME: IOS only
90-
// // https://github.com/facebook/react-native/issues/9178
91-
// get((res) => {
92-
// contentLength = res.headers['Content-Length']
93-
// if(isIOS)
94-
// return res.text()
95-
// return Promise.resolve()
96-
//
97-
// }, (data) => {
98-
// try {
99-
// report(<Assert key="content length should correct (IOS only)" expect={Math.floor(contentLength)} actual={data ? data.length : 0} />)
100-
// } catch(e){}
101-
// }, (err) => {
102-
// console.warn(err, err.stack)
103-
// }),
104-
// get((res) => {
105-
// console.log('##',res.headers['Content-Length'], res)
106-
// contentLength = res.headers['Content-Length']
107-
// return res.blob()
108-
// }, (blob) => {
109-
// return fs.stat(blob._ref).then((stat) => {
110-
// report(<Assert key="stored file size correct" expect={contentLength} actual={stat.size} />)
111-
// return blob.readBlob('base64')
112-
// })
113-
// .then((b64) => {
114-
// report(<Info key="stored image">
115-
// <Image style={styles.image} source={{uri : 'data:image/png;BASE64 ,' + b64}}/>
116-
// </Info>)
117-
// })
118-
//
119-
// }, (err) => {
120-
// console.warn(err, err.stack)
121-
// })
122-
// ]
123-
// Promise.all(promises).then( () => done() )
124-
//
125-
// })
126-
//
127-
// describe('POST different kinds of body', (report, done) => {
128-
//
129-
// let image = RNTest.prop('image')
130-
// let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
131-
//
132-
// function upload(desc, pBody) {
133-
// let name = `fetch-replacement-${Platform.OS}-${Date.now()}-${Math.random()}.png`
134-
// return pBody.then((body) =>
135-
// fetch('https://content.dropboxapi.com/2/files/upload', {
136-
// method : 'post',
137-
// headers : {
138-
// Authorization : `Bearer ${DROPBOX_TOKEN}`,
139-
// 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
140-
// 'Content-Type' : 'application/octet-stream'
141-
// },
142-
// body})
143-
// )
144-
// .then((res) => {
145-
// return res.json()
146-
// })
147-
// .then((info) => {
148-
// report(<Assert key={desc} expect={name} actual={info.name}/>)
149-
// })
150-
// }
151-
//
152-
// fetch(`${TEST_SERVER_URL}/public/github2.jpg`)
153-
// .then((res) => res.blob())
154-
// .then((b) => b.readBlob('base64'))
155-
// .then((image) => {
156-
// let tests = [
157-
// upload('upload base64 encoded body', Promise.resolve(image)),
158-
// upload('upload Blob body', Blob.build(image, 'image/png;BASE64')),
159-
// upload('upload file path body', fs.writeFile(tmpPath, image, 'base64').then(() => {
160-
// Promise.resolve(RNFetchBlob.wrap(tmpPath))
161-
// }))
162-
// ]
163-
// Promise.all(tests).then(() => done())
164-
// })
165-
//
166-
// })
167-
//
168-
// describe('Request header correctness', (report, done) => {
169-
//
170-
// let expect = {
171-
// 'hello' : 'world',
172-
// 'Content-Type' : 'application/json',
173-
// 'foo' : encodeURIComponent('福' + Date.now())
174-
// }
175-
//
176-
// fetch(`${TEST_SERVER_URL}/xhr-header`, {
177-
// method : 'GET',
178-
// headers : expect
179-
// })
180-
// .then((res) => res.json())
181-
// .then((actual) => {
182-
// report(<Info key={JSON.stringify(actual)}/>)
183-
// report(<Assert key="header field test #1" expect={expect.hello} actual={actual.hello}/>)
184-
// report(<Assert key="header field test #2" expect={expect['content-type']} actual={actual['content-type']}/>)
185-
// report(<Assert key="header field test #3" expect={expect.foo} actual={actual.foo}/>)
186-
// done()
187-
// })
188-
//
189-
// })
35+
describe('GET request test : unicode text -> any', (report, done) => {
36+
37+
function get(fn1, fn2) {
38+
return fetch(`${TEST_SERVER_URL}/unicode`, { method : 'GET'})
39+
.then((res) => fn1(res))
40+
.then((data) => fn2(data))
41+
}
42+
43+
let promises =
44+
[
45+
get((res) => res.json(), (json) => {
46+
report(<Assert key="json data correct" expect={'你好!'} actual={json.data}/>)
47+
}),
48+
get((res) => res.text(), (text) => {
49+
report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
50+
}),
51+
get((res) => res.blob(), (blob) => {
52+
return blob.readBlob('utf8').then((text) => {
53+
report(<Assert key="blob data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
54+
})
55+
}),
56+
// get((res) => res.arrayBuffer(), (text) => {
57+
// report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
58+
// })
59+
]
60+
61+
Promise.all(promises).then(() => {
62+
done()
63+
})
64+
65+
})
66+
67+
describe('GET request test : path -> any', (report, done) => {
68+
69+
function get(fn1, fn2, fn3) {
70+
fetch(`${TEST_SERVER_URL}/public/github.png`, { method : 'GET'})
71+
.then((res) => fn1(res))
72+
.then((data) => fn2(data))
73+
.catch((err) => fn3(err))
74+
}
75+
let contentLength = 0
76+
let isIOS = Platform.OS === 'ios'
77+
let promises = [
78+
// FIXME: IOS only
79+
// https://github.com/facebook/react-native/issues/9178
80+
get((res) => {
81+
if(isIOS)
82+
return res.json()
83+
return Promise.resolve()
84+
}, (data) => {
85+
report(<Assert key="should not convert blob to JSON (IOS only)" expect={true} actual={false} />)
86+
}, (err) => {
87+
report(<Assert key="should not convert blob to JSON (IOS only)" expect={true} actual={true} />)
88+
}),
89+
// FIXME: IOS only
90+
// https://github.com/facebook/react-native/issues/9178
91+
get((res) => {
92+
contentLength = res.headers['Content-Length']
93+
if(isIOS)
94+
return res.text()
95+
return Promise.resolve()
96+
97+
}, (data) => {
98+
try {
99+
report(<Assert key="content length should correct (IOS only)" expect={Math.floor(contentLength)} actual={data ? data.length : 0} />)
100+
} catch(e){}
101+
}, (err) => {
102+
console.warn(err, err.stack)
103+
}),
104+
get((res) => {
105+
contentLength = res.headers['Content-Length']
106+
return res.blob()
107+
}, (blob) => {
108+
return fs.stat(blob._ref).then((stat) => {
109+
report(<Assert key="stored file size correct" expect={contentLength} actual={stat.size} />)
110+
return blob.readBlob('base64')
111+
})
112+
.then((b64) => {
113+
report(<Info key="stored image">
114+
<Image style={styles.image} source={{uri : 'data:image/png;BASE64 ,' + b64}}/>
115+
</Info>)
116+
})
117+
118+
}, (err) => {
119+
console.warn(err, err.stack)
120+
})
121+
]
122+
Promise.all(promises).then( () => done() )
123+
124+
})
125+
126+
describe('POST different kinds of body', (report, done) => {
127+
128+
let image = RNTest.prop('image')
129+
let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
130+
131+
function upload(desc, pBody) {
132+
let name = `fetch-replacement-${Platform.OS}-${Date.now()}-${Math.random()}.png`
133+
return pBody.then((body) =>
134+
fetch('https://content.dropboxapi.com/2/files/upload', {
135+
method : 'post',
136+
headers : {
137+
Authorization : `Bearer ${DROPBOX_TOKEN}`,
138+
'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
139+
'Content-Type' : 'application/octet-stream'
140+
},
141+
body})
142+
)
143+
.then((res) => {
144+
return res.json()
145+
})
146+
.then((info) => {
147+
report(<Assert key={desc} expect={name} actual={info.name}/>)
148+
})
149+
}
150+
151+
fetch(`${TEST_SERVER_URL}/public/github2.jpg`)
152+
.then((res) => res.blob())
153+
.then((b) => b.readBlob('base64'))
154+
.then((image) => {
155+
let tests = [
156+
upload('upload base64 encoded body', Promise.resolve(image)),
157+
upload('upload Blob body', Blob.build(image, 'image/png;BASE64')),
158+
upload('upload file path body', fs.writeFile(tmpPath, image, 'base64').then(() => {
159+
Promise.resolve(RNFetchBlob.wrap(tmpPath))
160+
}))
161+
]
162+
Promise.all(tests).then(() => done())
163+
})
164+
165+
})
166+
167+
describe('Request header correctness', (report, done) => {
168+
169+
let expect = {
170+
'hello' : 'world',
171+
'Content-Type' : 'application/json',
172+
'foo' : encodeURIComponent('福' + Date.now())
173+
}
174+
175+
fetch(`${TEST_SERVER_URL}/xhr-header`, {
176+
method : 'GET',
177+
headers : expect
178+
})
179+
.then((res) => res.json())
180+
.then((actual) => {
181+
report(<Info key={JSON.stringify(actual)}/>)
182+
report(<Assert key="header field test #1" expect={expect.hello} actual={actual.hello}/>)
183+
report(<Assert key="header field test #2" expect={expect['content-type']} actual={actual['content-type']}/>)
184+
report(<Assert key="header field test #3" expect={expect.foo} actual={actual.foo}/>)
185+
done()
186+
})
187+
188+
})
190189

191190
describe('Upload form data ', (report, done) => {
192191

@@ -214,5 +213,4 @@ describe('Upload form data ', (report, done) => {
214213
done()
215214
})
216215

217-
218216
})

0 commit comments

Comments
 (0)