暫無描述

test-0.5.x.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. Platform,
  10. Dimensions,
  11. Image,
  12. } from 'react-native';
  13. const { Assert, Comparer, Info, describe, prop } = RNTest
  14. const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  15. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  16. // added after 0.5.0
  17. describe('Get storage folders', (report, done) => {
  18. RNFetchBlob.getSystemDirs().then((dirs) => {
  19. report(
  20. <Assert key="system folders should exists" expect={dirs} comparer={Comparer.exists} />,
  21. <Assert key="check properties"
  22. expect={dirs}
  23. comparer={Comparer.hasProperties}
  24. actual={['PictureDir', 'MovieDir', 'DocumentDir', 'CacheDir', 'MusicDir', 'DCIMDir']}
  25. />,
  26. <Info key="System Folders">
  27. <Text>{`${JSON.stringify(dirs)}`}</Text>
  28. </Info>
  29. )
  30. done()
  31. })
  32. })
  33. let tmpFilePath = null
  34. describe('Download file to storage with custom file extension', (report, done) => {
  35. RNFetchBlob.config({
  36. fileCache : true,
  37. appendExt : 'png'
  38. })
  39. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  40. .then((resp) => {
  41. tmpFilePath = resp.path()
  42. report(<Info key={`image from ${tmpFilePath}`}>
  43. <Image
  44. source={{ uri : prefix + tmpFilePath}}
  45. style={styles.image}/>
  46. </Info>)
  47. done()
  48. })
  49. })
  50. describe('Read cached file via file stream', (report, done) => {
  51. let data = 'data:image/png;base64, '
  52. let stream = RNFetchBlob.openReadStream(tmpFilePath, 'base64')
  53. stream.onData((chunk) => {
  54. data += chunk
  55. })
  56. stream.onEnd(() => {
  57. report(
  58. <Assert key="image should have value"
  59. expect={0}
  60. comparer={Comparer.smaller}
  61. actual={data.length}/>,
  62. <Info key="image from read stream">
  63. <Image source={{uri : data}} style={styles.image}/>
  64. </Info>)
  65. done()
  66. })
  67. stream.onError((err) => {
  68. console.log('stream err', err)
  69. })
  70. })
  71. describe('File stream reader error should be able to handled', (report, done) => {
  72. let stream = RNFetchBlob.openReadStream('^_^ not exists', 'base64')
  73. stream.onError((err) => {
  74. report(<Info key="error message">
  75. <Text>
  76. {err}
  77. </Text>
  78. </Info>)
  79. done()
  80. })
  81. })
  82. let localFile = null
  83. describe('Upload from file storage', (report, done) => {
  84. let filename = ''
  85. let filepath = ''
  86. RNFetchBlob.getSystemDirs().then((dirs) => {
  87. filename = Platform.OS + '0.5.0-' + Date.now() + '-from-storage.png'
  88. filepath = dirs.DocumentDir + '/' + filename
  89. return RNFetchBlob.config({ path : filepath })
  90. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  91. })
  92. .then((resp) => {
  93. let path = resp.path()
  94. localFile = path
  95. return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  96. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  97. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  98. 'Content-Type' : 'application/octet-stream',
  99. }, 'RNFetchBlob-file://' + path)
  100. .then((resp) => {
  101. resp = resp.json()
  102. report(
  103. <Assert key="confirm the file has been uploaded" expect={filename} actual={resp.name}/>
  104. )
  105. done()
  106. })
  107. })
  108. })
  109. describe('Upload multipart data with file from storage', (report, done) => {
  110. let filename = 'test-from-storage-img-'+Date.now()+'.png'
  111. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  112. 'Content-Type' : 'multipart/form-data',
  113. }, [
  114. { name : 'test-img', filename : filename, data: 'RNFetchBlob-file://' + localFile},
  115. { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  116. { name : 'field1', data : 'hello !!'},
  117. { name : 'field2', data : 'hello2 !!'}
  118. ])
  119. .then((resp) => {
  120. resp = resp.json()
  121. report(
  122. <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  123. <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  124. )
  125. return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/${filename}`)
  126. })
  127. .then((resp) => {
  128. report(<Info key="uploaded image">
  129. <Image
  130. style={styles.image}
  131. source={{ uri : 'data:image/png;base64, '+ resp.base64()}}/>
  132. </Info>)
  133. done()
  134. })
  135. })