Sin descripción

test-0.5.x.js 6.5KB

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