Sin descripción

tests.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import RNTest from './react-native-testkit/'
  2. import React from 'react'
  3. import RNFetchBlob from 'react-native-fetch-blob'
  4. import {
  5. AppRegistry,
  6. StyleSheet,
  7. Text,
  8. View,
  9. Platform,
  10. ScrollView,
  11. Dimensions,
  12. Image,
  13. } from 'react-native';
  14. const FILENAME = `${Platform.OS}-0.5.0-${Date.now()}.png`
  15. // paste your test config here
  16. const TEST_SERVER_URL = 'http://192.168.0.14:8123'
  17. const DROPBOX_TOKEN = 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4'
  18. const ctx = new RNTest.TestContext()
  19. const Assert = RNTest.Assert
  20. const Comparer = RNTest.Comparer
  21. const Info = RNTest.Info
  22. let image = null
  23. const styles = StyleSheet.create({
  24. image : {
  25. width:Dimensions.get('window').width*0.9,
  26. height : Dimensions.get('window').width*0.9,
  27. margin :16
  28. }
  29. })
  30. ctx.describe('GET image from server', (report, done) => {
  31. RNFetchBlob
  32. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`, {
  33. Authorization : 'Bearer abde123eqweje'
  34. })
  35. .then((resp) => {
  36. image = resp.base64()
  37. report(
  38. <Info key="Response image">
  39. <Image
  40. style={styles.image}
  41. source={{uri : `data:image/png;base64, ${image}`}}/>
  42. </Info>)
  43. done()
  44. })
  45. })
  46. // FIXME : discard these test cases in feature branch
  47. //
  48. // ctx.describe('The check if it follows 301/302 redirection', (report, done) => {
  49. //
  50. // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
  51. // .then((resp) => {
  52. // report(
  53. // <Assert key="check image size" expect={image.length} actual={resp.base64().length}/>,
  54. // <Info key="Response image">
  55. // <Image
  56. // style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
  57. // source={{uri : `data:image/png;base64, ${image}`}}/>
  58. // </Info>)
  59. // done()
  60. // })
  61. //
  62. // })
  63. //
  64. // ctx.describe('Upload octet-stream image to Dropbox', (report, done) => {
  65. //
  66. // RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  67. // Authorization : `Bearer ${DROPBOX_TOKEN}`,
  68. // 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  69. // 'Content-Type' : 'application/octet-stream',
  70. // }, image)
  71. // .then((resp) => {
  72. // resp = resp.json()
  73. // report(
  74. // <Assert key="confirm the file has been uploaded" expect={FILENAME} actual={resp.name}/>
  75. // )
  76. // done()
  77. // })
  78. //
  79. // })
  80. //
  81. // ctx.describe('Upload multipart/form-data', (report, done) => {
  82. //
  83. // RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  84. // Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
  85. // 'Content-Type' : 'multipart/form-data',
  86. // }, [
  87. // { name : 'test-img', filename : 'test-img.png', data: image},
  88. // { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  89. // { name : 'field1', data : 'hello !!'},
  90. // { name : 'field2', data : 'hello2 !!'}
  91. // ])
  92. // .then((resp) => {
  93. // resp = resp.json()
  94. // report(
  95. // <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  96. // <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  97. // )
  98. // done()
  99. // })
  100. //
  101. //
  102. // })
  103. // ctx.describe('Compare uploaded multipart image', (report, done) => {
  104. // let r1 = null
  105. // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
  106. // .then((resp) => {
  107. // r1 = resp
  108. // return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-text.txt`)
  109. // })
  110. // .then((resp) => {
  111. // report(
  112. // <Assert key="check file length" expect={image.length} actual={r1.base64().length}/>,
  113. // <Assert key="check file content" expect={'hello.txt'} actual={resp.text()}/>
  114. // )
  115. // done()
  116. // })
  117. //
  118. // })
  119. // added after 0.4.2
  120. ctx.describe('Progress report test', (report, done) => {
  121. let received = 0
  122. RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/1mb-dummy`, {
  123. Authorization : 'Bearer abde123eqweje'
  124. })
  125. .progress((written, total) => {
  126. report(<Info key={`progress = ${written} bytes / ${total} bytes`}/>)
  127. if(written === total)
  128. report(<Assert key="progress goes to 100%" expect={written} actual={total}/>)
  129. })
  130. .then((resp) => {
  131. report(<Assert key="response data should be correct event with progress listener"
  132. expect={resp.text().substr(0,10)} actual={"1234567890"}/>)
  133. done()
  134. })
  135. })
  136. // FIXME : not yet supported
  137. // ctx.describe('Large file download test', (report, done) => {
  138. // let received = 0
  139. // // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
  140. // // Authorization : 'Bearer abde123eqweje'
  141. // // })
  142. // // .then((resp) => {
  143. // report(<Assert key="not supported" expect={true} actual={false}/>)
  144. // done()
  145. // // })
  146. //
  147. // })
  148. // added after 0.5.0
  149. ctx.describe('Get storage folders', (report, done) => {
  150. RNFetchBlob.getSystemDirs().then((dirs) => {
  151. report(
  152. <Assert key="system folders should exists" expect={dirs} comparer={Comparer.exists} />,
  153. <Assert key="check properties"
  154. expect={dirs}
  155. comparer={Comparer.hasProperties}
  156. actual={['PictureDir', 'MovieDir', 'DocumentDir', 'CacheDir']}
  157. />,
  158. <Info key="System Folders">
  159. <Text>{`${JSON.stringify(dirs)}`}</Text>
  160. </Info>
  161. )
  162. done()
  163. })
  164. })
  165. let tmpFilePath = null
  166. ctx.describe('Download file to storage with custom file extension', (report, done) => {
  167. RNFetchBlob.config({
  168. fileCache : true,
  169. appendExt : 'png'
  170. })
  171. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  172. .then((resp) => {
  173. tmpFilePath = resp.path()
  174. report(<Info key={`image from ${tmpFilePath}`}>
  175. <Image source={{ uri : tmpFilePath}} style={styles.image}/>
  176. </Info>)
  177. done()
  178. })
  179. })
  180. ctx.describe('Read cache file with file stream', (report, done) => {
  181. let data = 'data:image/png;base64, '
  182. let stream = RNFetchBlob.openReadStream(tmpFilePath, 'base64')
  183. stream.onData((chunk) => {
  184. data += chunk
  185. })
  186. stream.onEnd(() => {
  187. report(
  188. <Assert key="image should have value" expect={0} comparer={Comparer.smaller} actual={data.length}/>,
  189. <Info key="image from read stream">
  190. <Image source={{uri : data}} style={styles.image}/>
  191. </Info>)
  192. done()
  193. })
  194. stream.onError((err) => {
  195. console.log('stream err', err)
  196. })
  197. })
  198. export default ctx