Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 { Assert, Comparer, Info, prop } = RNTest
  14. const describe = RNTest.config({
  15. group : '0.5.x',
  16. expand : false,
  17. })
  18. const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  19. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  20. // added after 0.5.0
  21. describe('Get storage folders', (report, done) => {
  22. RNFetchBlob.getSystemDirs().then((dirs) => {
  23. report(
  24. <Assert key="system folders should exists" expect={dirs} comparer={Comparer.exists} />,
  25. <Assert key="check properties"
  26. expect={dirs}
  27. comparer={Comparer.hasProperties}
  28. actual={['PictureDir', 'MovieDir', 'DocumentDir', 'CacheDir', 'MusicDir', 'DCIMDir']}
  29. />,
  30. <Info key="System Folders">
  31. <Text>{`${JSON.stringify(dirs)}`}</Text>
  32. </Info>
  33. )
  34. done()
  35. })
  36. })
  37. let tmpFilePath = null
  38. describe('Download file to storage with custom file extension', (report, done) => {
  39. RNFetchBlob.config({
  40. fileCache : true,
  41. appendExt : 'png'
  42. })
  43. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  44. .then((resp) => {
  45. tmpFilePath = resp.path()
  46. report(<Info key={`image from ${tmpFilePath}`}>
  47. <Image
  48. source={{ uri : prefix + tmpFilePath}}
  49. style={styles.image}/>
  50. </Info>)
  51. done()
  52. })
  53. })
  54. describe('Read cached file via file stream', (report, done) => {
  55. let data = 'data:image/png;base64, '
  56. let stream = RNFetchBlob.readStream(tmpFilePath, 'base64')
  57. stream.onData((chunk) => {
  58. data += chunk
  59. })
  60. stream.onEnd(() => {
  61. report(
  62. <Assert key="image should have value"
  63. expect={0}
  64. comparer={Comparer.smaller}
  65. actual={data.length}/>,
  66. <Info key="image from read stream">
  67. <Image source={{uri : data}} style={styles.image}/>
  68. </Info>)
  69. done()
  70. })
  71. stream.onError((err) => {
  72. console.log('stream err', err)
  73. })
  74. })
  75. describe('File stream reader error should be able to handled', (report, done) => {
  76. let stream = RNFetchBlob.readStream('^_^ not exists', 'base64')
  77. stream.onError((err) => {
  78. report(<Info key="error message">
  79. <Text>
  80. {err}
  81. </Text>
  82. </Info>)
  83. done()
  84. })
  85. })
  86. let localFile = null
  87. let sysDirs = null
  88. describe('Upload from file storage', (report, done) => {
  89. let filename = ''
  90. let filepath = ''
  91. RNFetchBlob.getSystemDirs().then((dirs) => {
  92. sysDirs = dirs
  93. filename = Platform.OS + '0.5.0-' + Date.now() + '-from-storage.png'
  94. filepath = dirs.DocumentDir + '/' + filename
  95. return RNFetchBlob.config({ path : filepath })
  96. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  97. })
  98. .then((resp) => {
  99. let path = resp.path()
  100. localFile = path
  101. return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  102. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  103. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  104. 'Content-Type' : 'application/octet-stream',
  105. }, 'RNFetchBlob-file://' + path)
  106. .then((resp) => {
  107. resp = resp.json()
  108. report(
  109. <Assert key="confirm the file has been uploaded" expect={filename} actual={resp.name}/>
  110. )
  111. done()
  112. })
  113. })
  114. })
  115. describe('Upload multipart data with file from storage', (report, done) => {
  116. let filename = 'test-from-storage-img-'+Date.now()+'.png'
  117. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  118. 'Content-Type' : 'multipart/form-data',
  119. }, [
  120. { name : 'test-img', filename : filename, data: 'RNFetchBlob-file://' + localFile},
  121. { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  122. { name : 'field1', data : 'hello !!'},
  123. { name : 'field2', data : 'hello2 !!'}
  124. ])
  125. .then((resp) => {
  126. resp = resp.json()
  127. report(
  128. <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  129. <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  130. )
  131. return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/${filename}`)
  132. })
  133. .then((resp) => {
  134. report(<Info key="uploaded image">
  135. <Image
  136. style={styles.image}
  137. source={{ uri : 'data:image/png;base64, '+ resp.base64()}}/>
  138. </Info>)
  139. done()
  140. })
  141. })
  142. describe('Session create mechanism test', (report, done) => {
  143. let sessionName = 'foo-' + Date.now()
  144. testSessionName = sessionName
  145. let p1 = RNFetchBlob.config({
  146. session : sessionName,
  147. fileCache : true
  148. })
  149. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  150. let p2 = RNFetchBlob.config({
  151. fileCache : true
  152. })
  153. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  154. let p3 = RNFetchBlob.config({
  155. path : sysDirs.DocumentDir + '/session-test.png'
  156. })
  157. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  158. let promises = [p1, p2, p3]
  159. Promise.all(promises).then((resp) => {
  160. let session = RNFetchBlob.session(sessionName).add(resp[1].path())
  161. resp[2].session(sessionName)
  162. let actual = session.list()
  163. let expect = resp.map((p) => {
  164. return p.path()
  165. })
  166. report(
  167. <Assert key="check if session state correct"
  168. expect={expect}
  169. comparer={Comparer.equalToArray}
  170. actual={actual} />)
  171. done()
  172. })
  173. })
  174. describe('Session API CRUD test', (report, done) => {
  175. let sessionName = 'test-session-' + Date.now()
  176. let baseDir = sysDirs.DocumentDir + '/' + sessionName
  177. RNFetchBlob.mkdir(sysDirs.DocumentDir + '/' + sessionName).then(() => {
  178. let promises = [0,1,2,3,4,5,6,7,8,9].map((p) => {
  179. return RNFetchBlob.config({
  180. session : sessionName,
  181. path : baseDir + '/testfile' + p
  182. })
  183. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  184. })
  185. return Promise.all(promises)
  186. })
  187. .then((resps) => {
  188. let s = RNFetchBlob.session(sessionName)
  189. report(
  190. <Assert
  191. key="list() length validation"
  192. expect={10}
  193. actual={s.list().length}/>)
  194. let modified = [
  195. s.list()[2],
  196. s.list()[3],
  197. s.list()[4],
  198. s.list()[5],
  199. s.list()[6],
  200. s.list()[7],
  201. s.list()[8],
  202. s.list()[9],
  203. ]
  204. let expect = [s.list()[0], s.list()[1]]
  205. s.remove(s.list()[0])
  206. s.remove(s.list()[0])
  207. report(
  208. <Assert
  209. key="remove() should work correctly"
  210. expect={modified}
  211. comparer={Comparer.equalToArray}
  212. actual={s.list()}/>)
  213. s.dispose().then(() => {
  214. RNFetchBlob.ls(baseDir).then((lsRes) => {
  215. report(
  216. <Assert
  217. key="dispose() should work correctly"
  218. expect={expect}
  219. comparer={Comparer.equalToArray}
  220. actual={lsRes.map((p) => {
  221. return baseDir + '/' + p
  222. })}/>)
  223. })
  224. done()
  225. })
  226. })
  227. })