暂无描述

test-0.6.2.js 6.7KB

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