Nessuna descrizione

test-0.6.2.js 7.1KB

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