Нет описания

test-0.6.2.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. console.log('cameraroll', JSON.stringify(resp))
  32. let url = resp.edges[0].node.image.uri
  33. photo = url
  34. return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  35. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  36. 'Dropbox-API-Arg': `{\"path\": \"/rn-upload/${imgName}\",\"mode\": \"add\",\"autorename\": false,\"mute\": false}`,
  37. 'Content-Type' : 'application/octet-stream',
  38. }, RNFetchBlob.wrap(url))
  39. .progress((now, total) => {
  40. if(Date.now() - tick < 1000)
  41. return
  42. report(<Info key="progress" uid="pg1">
  43. <Text>{`upload ${now} / ${total} ${Math.floor(now/total*100)}% `}</Text>
  44. </Info>)
  45. })
  46. })
  47. .then((resp) => {
  48. resp = resp.json()
  49. report(
  50. <Assert key="confirm the file has been uploaded" expect={imgName} actual={resp.name}/>
  51. )
  52. done()
  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. describe('access assets from camera roll', (report, done) => {
  83. let photo = null
  84. CameraRoll.getPhotos({first : 10})
  85. .then((resp) => {
  86. photo = resp.edges[0].node.image.uri
  87. report(<Info key="items">
  88. <Text>{photo}</Text>
  89. </Info>)
  90. return fs.readFile(photo, 'base64')
  91. })
  92. .then((data) => {
  93. report(<Info key="asset image">
  94. <Image
  95. style={styles.image}
  96. source={{uri: `data:image/png;base64, ${data}`}}/>
  97. </Info>)
  98. done()
  99. })
  100. })
  101. describe('read asset in app bundle',(report, done) => {
  102. let target = fs.asset('test-asset2.png')
  103. fs.readFile(target, 'base64')
  104. .then((data) => {
  105. report(<Info key="asset image">
  106. <Image
  107. style={styles.image}
  108. source={{uri: `data:image/png;base64, ${data}`}}/>
  109. </Info>)
  110. return fs.readFile(fs.asset('test-asset1.json'), 'utf8')
  111. })
  112. .then((resp) => {
  113. report(
  114. <Assert key="asset content verify"
  115. expect="asset#1"
  116. actual={JSON.parse(resp).secret}/>)
  117. done()
  118. })
  119. })
  120. describe('stat assets in app', (report, done) => {
  121. fs.stat(fs.asset('test-asset2.png'))
  122. .then((data) => {
  123. report(<Info key="list of assets">
  124. <Text>{JSON.stringify(data)}</Text>
  125. </Info>)
  126. done()
  127. })
  128. })
  129. describe('copy asset', (report, done) => {
  130. let dest = `${dirs.DocumentDir}/test-asset-1-${Date.now()}.json`
  131. fs.cp(fs.asset('test-asset1.json'), dest)
  132. .then(() => fs.readFile(dest, 'utf8'))
  133. .then((data) => {
  134. report(<Assert key="asset copied correctly"
  135. expect={'asset#1'}
  136. actual={JSON.parse(data).secret}/>)
  137. return fs.stat(fs.asset('test-asset1.json'))
  138. })
  139. .then((stat) => {
  140. report(<Assert key="file size check"
  141. expect={27}
  142. actual={Math.floor(stat.size)}/>,
  143. <Info key="dest file info">
  144. <Text>{JSON.stringify(stat)}</Text>
  145. </Info>)
  146. done()
  147. })
  148. })
  149. describe('upload file from assets',(report, done) => {
  150. let assetName = fs.asset('test-asset1.json')
  151. RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  152. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  153. 'Dropbox-API-Arg': `{\"path\": \"/rn-upload/file-from-asset-${Platform.OS}.json\",\"mode\": \"add\",\"autorename\": false,\"mute\": false}`,
  154. 'Content-Type' : 'application/octet-stream',
  155. }, RNFetchBlob.wrap(assetName))
  156. .then((resp) => {
  157. resp = resp.json()
  158. report(
  159. <Assert key="file name check"
  160. expect={`file-from-asset-${Platform.OS}.json`}
  161. actual={resp.name}/>)
  162. done()
  163. })
  164. })
  165. describe('Check custom MIME type correctness',(report, done) => {
  166. RNFetchBlob
  167. .config({fileCache : true})
  168. .fetch('GET', `${TEST_SERVER_URL}/public/beethoven.mp3`)
  169. .then((resp) => {
  170. return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/mime`, null, [
  171. { name : 'image', filename : 'image', type : 'image/jpeg', data : RNFetchBlob.base64.encode('123456') },
  172. { name : 'mp3', filename : 'mp3', type : 'application/mp3', data : RNFetchBlob.base64.encode('123456') },
  173. { name : 'mp3', filename : 'mp3', data : RNFetchBlob.base64.encode('123456') },
  174. { name : 'asset', filename : 'asset.json', type: 'application/json', data : RNFetchBlob.wrap(fs.asset('test-asset1.json')) },
  175. { name : 'camera-roll', filename : 'cameraRoll.png', type: 'image/png', data : RNFetchBlob.wrap(photo) },
  176. { name : 'storage', filename : 'storage.mp3', type: 'application/mp3', data : RNFetchBlob.wrap(resp.path()) },
  177. ])
  178. })
  179. .then((resp) => {
  180. resp = resp.json()
  181. report(
  182. <Assert key="check #1 mime" expect={'image/jpeg'} actual={resp[0]} />,
  183. <Assert key="check #2 mime" expect={'application/mp3'} actual={resp[1]} />,
  184. <Assert key="check #3 mime" expect={'application/octet-stream'} actual={resp[2]} />,
  185. <Assert key="check #4 mime" expect={'application/json'} actual={resp[3]} />,
  186. <Assert key="check #5 mime" expect={'image/png'} actual={resp[4]} />,
  187. <Assert key="check #6 mime" expect={'application/mp3'} actual={resp[5]} />,
  188. )
  189. done()
  190. })
  191. })