설명 없음

test-0.5.x.js 6.6KB

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