Geen omschrijving

test-0.5.1.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import RNTest from './react-native-testkit/'
  2. import React from 'react'
  3. import RNFetchBlob from 'react-native-fetch-blob'
  4. import {
  5. StyleSheet,
  6. Text,
  7. View,
  8. ScrollView,
  9. Platform,
  10. Dimensions,
  11. Image,
  12. } from 'react-native';
  13. const fs = RNFetchBlob.fs
  14. const { Assert, Comparer, Info, prop } = RNTest
  15. const describe = RNTest.config({
  16. group : '0.5.1',
  17. run : true,
  18. expand : false,
  19. })
  20. const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  21. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  22. // added after 0.5.0
  23. let tmpFilePath = null
  24. describe('Download file to storage with custom file extension', (report, done) => {
  25. RNFetchBlob.config({
  26. fileCache : true,
  27. appendExt : 'png'
  28. })
  29. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  30. .then((resp) => {
  31. console.log(resp.path())
  32. tmpFilePath = resp.path()
  33. report(<Info key={`image from ${tmpFilePath}`}>
  34. <Image
  35. source={{ uri : prefix + tmpFilePath}}
  36. style={styles.image}/>
  37. </Info>)
  38. done()
  39. })
  40. })
  41. describe('Read cached file via file stream', (report, done) => {
  42. let data = 'data:image/png;base64, '
  43. fs.readStream(tmpFilePath, 'base64')
  44. .then((stream) => {
  45. stream.open()
  46. stream.onData((chunk) => {
  47. data += chunk
  48. })
  49. stream.onEnd(() => {
  50. report(
  51. <Assert key="image should have value"
  52. expect={0}
  53. comparer={Comparer.smaller}
  54. actual={data.length}/>,
  55. <Info key="image from read stream">
  56. <Image source={{uri : data}} style={styles.image}/>
  57. </Info>)
  58. done()
  59. })
  60. stream.onError((err) => {
  61. console.log('stream err', err)
  62. })
  63. })
  64. })
  65. describe('File stream reader error should be able to handled', (report, done) => {
  66. fs.readStream('^_^ not exists', 'base64')
  67. .then((stream) => {
  68. stream.open()
  69. stream.onError((err) => {
  70. report(<Info key="error message">
  71. <Text>
  72. {err}
  73. </Text>
  74. </Info>)
  75. done()
  76. })
  77. })
  78. })
  79. let localFile = null
  80. let sysDirs = RNFetchBlob.fs.dirs
  81. let dirs = RNFetchBlob.fs.dirs
  82. describe('Upload from file storage', (report, done) => {
  83. let filename = ''
  84. let filepath = ''
  85. filename = Platform.OS + '0.5.0-' + Date.now() + '-from-storage.png'
  86. filepath = dirs.DocumentDir + '/' + filename
  87. RNFetchBlob
  88. .config({ path : filepath })
  89. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  90. .then((resp) => {
  91. let path = resp.path()
  92. localFile = path
  93. return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  94. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  95. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  96. 'Content-Type' : 'application/octet-stream',
  97. }, 'RNFetchBlob-file://' + path)
  98. .then((resp) => {
  99. resp = resp.json()
  100. report(
  101. <Assert key="confirm the file has been uploaded" expect={filename} actual={resp.name}/>
  102. )
  103. done()
  104. })
  105. })
  106. })
  107. describe('Upload multipart data with file from storage', (report, done) => {
  108. try{
  109. let filename = 'test-from-storage-img-'+Date.now()+'.png'
  110. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  111. 'Content-Type' : 'multipart/form-data',
  112. }, [
  113. { name : 'test-img', filename : filename, data: 'RNFetchBlob-file://' + localFile},
  114. { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  115. { name : 'field1', data : 'hello !!'},
  116. { name : 'field2', data : 'hello2 !!'}
  117. ])
  118. .then((resp) => {
  119. resp = resp.json()
  120. report(
  121. <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  122. <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  123. )
  124. return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/${filename}`)
  125. })
  126. .then((resp) => {
  127. report(<Info key="uploaded image">
  128. <Image
  129. style={styles.image}
  130. source={{ uri : 'data:image/png;base64, '+ resp.base64()}}/>
  131. </Info>)
  132. done()
  133. })
  134. } catch(err) {
  135. console.log(err)
  136. }
  137. })
  138. describe('Upload and download at the same time', (report, done) => {
  139. let content = 'POST and PUT calls with headers and body should also work correctly'
  140. let filename = 'download-header-test-' + Date.now()
  141. let body = RNFetchBlob.base64.encode(content)
  142. RNFetchBlob
  143. .config({
  144. fileCache : true,
  145. })
  146. .fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  147. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  148. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  149. 'Content-Type' : 'application/octet-stream',
  150. }, body)
  151. .then((resp) => {
  152. return RNFetchBlob.fs.readStream(resp.path(), 'utf8')
  153. })
  154. .then((stream) => {
  155. let actual = ''
  156. stream.open()
  157. stream.onData((chunk) => {
  158. actual += chunk
  159. })
  160. stream.onEnd(() => {
  161. report(
  162. <Assert
  163. key="response data should be the filename"
  164. expect={filename}
  165. actual={JSON.parse(actual).name} />)
  166. done()
  167. })
  168. })
  169. })
  170. RNTest.config({
  171. group : '0.5.1',
  172. run : true,
  173. expand : false,
  174. timeout : 600000,
  175. })('Upload and download large file', (report, done) => {
  176. let filename = '22mb-dummy-' + Date.now()
  177. let begin = -1
  178. let begin2 = -1
  179. let deb = Date.now()
  180. RNFetchBlob.config({
  181. fileCache : true
  182. })
  183. .fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`)
  184. .progress((now, total) => {
  185. if(begin === -1)
  186. begin = Date.now()
  187. if(Date.now() - deb < 1000)
  188. return
  189. deb = Date.now()
  190. report(<Info uid="200" key="progress">
  191. <Text>
  192. {`download ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s)`}
  193. </Text>
  194. </Info>)
  195. })
  196. .then((res) => {
  197. try {
  198. deb = Date.now()
  199. let promise = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  200. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  201. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  202. 'Content-Type' : 'application/octet-stream',
  203. }, RNFetchBlob.wrap(res.path()))
  204. if(Platform.OS === 'ios') {
  205. promise.progress((now, total) => {
  206. if(Date.now() - deb < 1000)
  207. return
  208. deb = Date.now()
  209. if(begin2 === -1)
  210. begin2 = Date.now()
  211. let speed = Math.floor(now / (Date.now() - begin2))
  212. report(<Info uid="100" key="progress">
  213. <Text>
  214. {`upload ${now} / ${total} bytes (${speed} kb/s)`}
  215. {` ${Math.floor((total-now)/speed/1000)} seconds left`}
  216. </Text>
  217. </Info>)
  218. })
  219. }
  220. return promise
  221. } catch(err) { console.log(err) }
  222. })
  223. .then((res) => {
  224. report(<Assert
  225. key="upload should success without crashing app"
  226. expect={filename}
  227. actual={res.json().name}/>)
  228. done()
  229. })
  230. })
  231. describe('Session create mechanism test', (report, done) => {
  232. let sessionName = 'foo-' + Date.now()
  233. testSessionName = sessionName
  234. let p1 = RNFetchBlob.config({
  235. session : sessionName,
  236. fileCache : true
  237. })
  238. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  239. let p2 = RNFetchBlob.config({
  240. fileCache : true
  241. })
  242. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  243. let p3 = RNFetchBlob.config({
  244. path : sysDirs.DocumentDir + '/session-test'+Date.now()+'.png'
  245. })
  246. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  247. let promises = [p1, p2, p3]
  248. Promise.all(promises).then((resp) => {
  249. let session = RNFetchBlob.session(sessionName).add(resp[1].path())
  250. resp[2].session(sessionName)
  251. let actual = session.list()
  252. let expect = resp.map((p) => {
  253. return p.path()
  254. })
  255. report(
  256. <Assert key="check if session state correct"
  257. expect={expect}
  258. comparer={Comparer.equalToArray}
  259. actual={actual} />)
  260. done()
  261. })
  262. })
  263. describe('Session API CRUD test', (report, done) => {
  264. let sessionName = 'test-session-' + Date.now()
  265. let baseDir = sysDirs.DocumentDir + '/' + sessionName
  266. fs.mkdir(sysDirs.DocumentDir + '/' + sessionName).then(() => {
  267. let promises = [0,1,2,3,4,5,6,7,8,9].map((p) => {
  268. return RNFetchBlob.config({
  269. session : sessionName,
  270. path : baseDir + '/testfile' + p
  271. })
  272. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  273. })
  274. return Promise.all(promises)
  275. })
  276. .then((resps) => {
  277. let s = RNFetchBlob.session(sessionName)
  278. report(
  279. <Assert
  280. key="list() length validation"
  281. expect={10}
  282. actual={s.list().length}/>)
  283. let modified = [
  284. s.list()[2],
  285. s.list()[3],
  286. s.list()[4],
  287. s.list()[5],
  288. s.list()[6],
  289. s.list()[7],
  290. s.list()[8],
  291. s.list()[9],
  292. ]
  293. let expect = [s.list()[0], s.list()[1]]
  294. s.remove(s.list()[0])
  295. s.remove(s.list()[0])
  296. report(
  297. <Assert
  298. key="remove() should work correctly"
  299. expect={modified}
  300. comparer={Comparer.equalToArray}
  301. actual={s.list()}/>)
  302. s.dispose()
  303. .then(() => {
  304. return fs.ls(baseDir)
  305. })
  306. .then((lsRes) => {
  307. report(
  308. <Assert
  309. key="dispose() should work correctly"
  310. expect={expect}
  311. comparer={Comparer.equalToArray}
  312. actual={lsRes.map((p) => {
  313. return baseDir + '/' + p
  314. })}/>)
  315. done()
  316. })
  317. })
  318. })