Aucune description

test-0.6.2.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. CameraRoll,
  10. Platform,
  11. Dimensions,
  12. Image,
  13. } from 'react-native';
  14. const fs = RNFetchBlob.fs
  15. const { Assert, Comparer, Info, prop } = RNTest
  16. const describe = RNTest.config({
  17. group : '0.6.2',
  18. run : true,
  19. expand : false,
  20. timeout : 30000,
  21. })
  22. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  23. const dirs = RNFetchBlob.fs.dirs
  24. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  25. let photo = null
  26. describe('upload asset from camera roll', (report, done) => {
  27. let imgName = `image-from-camera-roll-${Platform.OS}-${Date.now()}.jpg`
  28. let tick = Date.now()
  29. let tick2 = Date.now()
  30. CameraRoll.getPhotos({first : 10})
  31. .then((resp) => {
  32. let url = resp.edges[0].node.image.uri
  33. console.log('CameraRoll',url)
  34. photo = url
  35. return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  36. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  37. 'Dropbox-API-Arg': `{\"path\": \"/rn-upload/${imgName}\",\"mode\": \"add\",\"autorename\": false,\"mute\": false}`,
  38. 'Content-Type' : 'application/octet-stream',
  39. }, RNFetchBlob.wrap(url))
  40. .uploadProgress((now, total) => {
  41. if(Date.now() - tick2 < 1000)
  42. return
  43. report(<Info key="upload progress" uid="pg0">
  44. <Text>{`upload ${now} / ${total} ${Math.floor(now/total*100)}% `}</Text>
  45. </Info>)
  46. tick2 = Date.now()
  47. })
  48. })
  49. .then((resp) => {
  50. console.log(resp)
  51. resp = resp.json()
  52. report(
  53. <Assert key="confirm the file has been uploaded" expect={imgName} actual={resp.name}/>
  54. )
  55. done()
  56. })
  57. })
  58. describe('Upload multipart data with file from CameraRoll', (report, done) => {
  59. let filename = 'test-from-storage-img-'+Date.now()+'.png'
  60. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  61. 'Content-Type' : 'multipart/form-data',
  62. }, [
  63. { name : 'test-img', filename : filename, data: RNFetchBlob.wrap(photo)},
  64. { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  65. { name : 'field1', data : 'hello !!'},
  66. { name : 'field2', data : 'hello2 !!'}
  67. ])
  68. .then((resp) => {
  69. resp = resp.json()
  70. report(
  71. <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  72. <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  73. )
  74. return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/${filename}`)
  75. })
  76. .then((resp) => {
  77. report(<Info key="uploaded image">
  78. <Image
  79. style={styles.image}
  80. source={{ uri : 'data:image/png;base64, '+ resp.base64()}}/>
  81. </Info>)
  82. done()
  83. })
  84. })
  85. describe('access assets from camera roll', (report, done) => {
  86. let photo = null
  87. CameraRoll.getPhotos({first : 10})
  88. .then((resp) => {
  89. photo = resp.edges[0].node.image.uri
  90. report(<Info key="items">
  91. <Text>{photo}</Text>
  92. </Info>)
  93. return fs.readFile(photo, 'base64')
  94. })
  95. .then((data) => {
  96. report(<Info key="asset image">
  97. <Image
  98. style={styles.image}
  99. source={{uri: `data:image/png;base64, ${data}`}}/>
  100. </Info>)
  101. done()
  102. })
  103. })
  104. describe('read asset in app bundle',(report, done) => {
  105. let target = fs.asset('test-asset2.png')
  106. fs.readFile(target, 'base64')
  107. .then((data) => {
  108. report(<Info key="asset image">
  109. <Image
  110. style={styles.image}
  111. source={{uri: `data:image/png;base64, ${data}`}}/>
  112. </Info>)
  113. return fs.readFile(fs.asset('test-asset1.json'), 'utf8')
  114. })
  115. .then((resp) => {
  116. report(
  117. <Assert key="asset content verify"
  118. expect="asset#1"
  119. actual={JSON.parse(resp).secret}/>)
  120. done()
  121. })
  122. })
  123. describe('stat assets in app', (report, done) => {
  124. fs.stat(fs.asset('test-asset2.png'))
  125. .then((data) => {
  126. report(<Info key="list of assets">
  127. <Text>{JSON.stringify(data)}</Text>
  128. </Info>)
  129. done()
  130. })
  131. })
  132. describe('copy asset', (report, done) => {
  133. let dest = `${dirs.DocumentDir}/test-asset-1-${Date.now()}.json`
  134. fs.cp(fs.asset('test-asset1.json'), dest)
  135. .then(() => fs.readFile(dest, 'utf8'))
  136. .then((data) => {
  137. report(<Assert key="asset copied correctly"
  138. expect={'asset#1'}
  139. actual={JSON.parse(data).secret}/>)
  140. return fs.stat(fs.asset('test-asset1.json'))
  141. })
  142. .then((stat) => {
  143. report(<Assert key="file size check"
  144. expect={27}
  145. actual={Math.floor(stat.size)}/>,
  146. <Info key="dest file info">
  147. <Text>{JSON.stringify(stat)}</Text>
  148. </Info>)
  149. done()
  150. })
  151. })
  152. describe('upload file from assets',(report, done) => {
  153. let assetName = fs.asset('test-asset1.json')
  154. RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  155. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  156. 'Dropbox-API-Arg': `{\"path\": \"/rn-upload/file-from-asset-${Platform.OS}.json\",\"mode\": \"add\",\"autorename\": false,\"mute\": false}`,
  157. 'Content-Type' : 'application/octet-stream',
  158. }, RNFetchBlob.wrap(assetName))
  159. .then((resp) => {
  160. resp = resp.json()
  161. report(
  162. <Assert key="file name check"
  163. expect={`file-from-asset-${Platform.OS}.json`}
  164. actual={resp.name}/>)
  165. done()
  166. })
  167. })
  168. describe('Check custom MIME type correctness',(report, done) => {
  169. RNFetchBlob
  170. .config({fileCache : true})
  171. .fetch('GET', `${TEST_SERVER_URL}/public/beethoven.mp3`)
  172. .then((resp) => {
  173. return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/mime`, {
  174. 'Content-Type' : 'multipart/form-data'
  175. }, [
  176. { name : 'image', filename : 'image', type : 'image/jpeg', data : RNFetchBlob.base64.encode('123456') },
  177. { name : 'mp3', filename : 'mp3', type : 'application/mp3', data : RNFetchBlob.base64.encode('123456') },
  178. { name : 'mp3', filename : 'mp3', data : RNFetchBlob.base64.encode('123456') },
  179. { name : 'asset', filename : 'asset.json', type: 'application/json', data : RNFetchBlob.wrap(fs.asset('test-asset1.json')) },
  180. { name : 'camera-roll', filename : 'cameraRoll.png', type: 'image/png', data : RNFetchBlob.wrap(photo) },
  181. { name : 'storage', filename : 'storage.mp3', type: 'application/mp3', data : RNFetchBlob.wrap(resp.path()) },
  182. ])
  183. })
  184. .then((resp) => {
  185. resp = resp.json()
  186. report(
  187. <Assert key="check #1 mime" expect={'image/jpeg'} actual={resp[0]} />,
  188. <Assert key="check #2 mime" expect={'application/mp3'} actual={resp[1]} />,
  189. <Assert key="check #3 mime" expect={'application/octet-stream'} actual={resp[2]} />,
  190. <Assert key="check #4 mime" expect={'application/json'} actual={resp[3]} />,
  191. <Assert key="check #5 mime" expect={'image/png'} actual={resp[4]} />,
  192. <Assert key="check #6 mime" expect={'application/mp3'} actual={resp[5]} />,
  193. )
  194. done()
  195. })
  196. })