Açıklama Yok

test-0.6.2.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. console.log(photo)
  61. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  62. 'Content-Type' : 'multipart/form-data',
  63. }, [
  64. { name : 'test-img', filename : filename, data: RNFetchBlob.wrap(photo)},
  65. { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  66. { name : 'field1', data : 'hello !!'},
  67. { name : 'field2', data : 'hello2 !!'}
  68. ])
  69. .then((resp) => {
  70. resp = resp.json()
  71. report(
  72. <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  73. <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  74. )
  75. return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/${filename}`)
  76. })
  77. .then((resp) => {
  78. report(<Info key="uploaded image">
  79. <Image
  80. style={styles.image}
  81. source={{ uri : 'data:image/png;base64, '+ resp.base64()}}/>
  82. </Info>)
  83. done()
  84. })
  85. })
  86. describe('access assets from camera roll', (report, done) => {
  87. let photo = null
  88. CameraRoll.getPhotos({first : 10})
  89. .then((resp) => {
  90. photo = resp.edges[0].node.image.uri
  91. report(<Info key="items">
  92. <Text>{photo}</Text>
  93. </Info>)
  94. return fs.readFile(photo, 'base64')
  95. })
  96. .then((data) => {
  97. report(<Info key="asset image">
  98. <Image
  99. style={styles.image}
  100. source={{uri: `data:image/png;base64, ${data}`}}/>
  101. </Info>)
  102. done()
  103. })
  104. })
  105. describe('read asset in app bundle',(report, done) => {
  106. let target = fs.asset('test-asset2.png')
  107. fs.readFile(target, 'base64')
  108. .then((data) => {
  109. report(<Info key="asset image">
  110. <Image
  111. style={styles.image}
  112. source={{uri: `data:image/png;base64, ${data}`}}/>
  113. </Info>)
  114. return fs.readFile(fs.asset('test-asset1.json'), 'utf8')
  115. })
  116. .then((resp) => {
  117. report(
  118. <Assert key="asset content verify"
  119. expect="asset#1"
  120. actual={JSON.parse(resp).secret}/>)
  121. done()
  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. describe('copy asset', (report, done) => {
  134. let dest = `${dirs.DocumentDir}/test-asset-1-${Date.now()}.json`
  135. fs.cp(fs.asset('test-asset1.json'), dest)
  136. .then(() => fs.readFile(dest, 'utf8'))
  137. .then((data) => {
  138. report(<Assert key="asset copied correctly"
  139. expect={'asset#1'}
  140. actual={JSON.parse(data).secret}/>)
  141. return fs.stat(fs.asset('test-asset1.json'))
  142. })
  143. .then((stat) => {
  144. report(<Assert key="file size check"
  145. expect={27}
  146. actual={Math.floor(stat.size)}/>,
  147. <Info key="dest file info">
  148. <Text>{JSON.stringify(stat)}</Text>
  149. </Info>)
  150. done()
  151. })
  152. })
  153. describe('upload file from assets',(report, done) => {
  154. let assetName = fs.asset('test-asset1.json')
  155. RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  156. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  157. 'Dropbox-API-Arg': `{\"path\": \"/rn-upload/file-from-asset-${Platform.OS}.json\",\"mode\": \"overwrite\",\"autorename\": false,\"mute\": false}`,
  158. 'Content-Type' : 'application/octet-stream',
  159. }, RNFetchBlob.wrap(assetName))
  160. .then((resp) => {
  161. console.log(resp)
  162. resp = resp.json()
  163. report(
  164. <Assert key="file name check"
  165. expect={`file-from-asset-${Platform.OS}.json`}
  166. actual={resp.name}/>)
  167. done()
  168. })
  169. })
  170. describe('Check custom MIME type correctness',(report, done) => {
  171. RNFetchBlob
  172. .config({fileCache : true})
  173. .fetch('GET', `${TEST_SERVER_URL}/public/beethoven.mp3`)
  174. .then((resp) => {
  175. return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/mime`, {
  176. 'Content-Type' : 'multipart/form-data'
  177. }, [
  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. })