Aucune description

test-0.9.6.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. TouchableOpacity,
  13. } from 'react-native';
  14. window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
  15. window.Blob = RNFetchBlob.polyfill.Blob
  16. const fs = RNFetchBlob.fs
  17. const { Assert, Comparer, Info, prop } = RNTest
  18. const describe = RNTest.config({
  19. group : '0.9.6',
  20. run : true,
  21. expand : false,
  22. timeout : 20000,
  23. })
  24. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  25. const dirs = RNFetchBlob.fs.dirs
  26. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  27. describe('support #141 breakpoint download', (report, done) => {
  28. let dest = dirs.DocumentDir + '/breakpoint.png'
  29. let firstChunkSize = 0
  30. fs.unlink(dest)
  31. .then(() => {
  32. let session = RNFetchBlob.config({
  33. path : dest
  34. })
  35. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`, {
  36. 'Cache-Control' : 'no-cache',
  37. Range : 'bytes=0-200'
  38. })
  39. .then((res) => {
  40. console.log(res.info())
  41. console.log(res.data)
  42. return fs.stat(res.path())
  43. })
  44. .then((stat) => {
  45. firstChunkSize = Math.floor(stat.size)
  46. console.log(firstChunkSize)
  47. })
  48. .then(() => {
  49. RNFetchBlob.config({
  50. path : dest + '?append=true'
  51. })
  52. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`, {
  53. 'Cache-Control' : 'no-cache',
  54. Range : 'bytes=201-'
  55. })
  56. .then((res) => {
  57. console.log(res.info())
  58. console.log(res.path())
  59. return fs.stat(res.path())
  60. })
  61. .then((stat) => {
  62. report(
  63. <Info key="image">
  64. <Image style={RNTest.prop('styles').image} source={{uri : prefix + dest}}/>
  65. </Info>,
  66. <Assert key="request data should append to existing one" expect={23975} actual={Math.floor(stat.size)}/>)
  67. done()
  68. })
  69. })
  70. })
  71. })
  72. describe('support download/upload progress interval and division #140 ', (report, done) => {
  73. let tick = 0
  74. let records = []
  75. let last = Date.now()
  76. RNFetchBlob.config({
  77. timeout : 30000
  78. }).fetch('GET', `${TEST_SERVER_URL}/10s-download`, {
  79. 'Cache-Control' : 'no-store'
  80. })
  81. .progress({interval : 1000},(current, total) => {
  82. records.push(Date.now() - last)
  83. last = Date.now()
  84. console.log(current, '/', total, current/total)
  85. tick ++
  86. })
  87. .then(() => {
  88. let avg = 0
  89. for(let i in records) {
  90. avg+=records[i]
  91. }
  92. avg/=records.length
  93. report(<Assert key="interval > 900" expect={900} comparer={Comparer.smaller} actual={avg}/>)
  94. report(<Assert key="interval < 1200" expect={1200} comparer={Comparer.greater} actual={avg}/>)
  95. upload()
  96. })
  97. function upload() {
  98. let count = 0
  99. let image = RNTest.prop('image')
  100. RNFetchBlob.config({
  101. fileCache : true
  102. })
  103. .fetch('GET', `${TEST_SERVER_URL}/public/6mb-dummy`)
  104. .then((res) => {
  105. return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  106. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  107. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/intervalTest.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  108. 'Content-Type' : 'application/octet-stream',
  109. }, RNFetchBlob.wrap(res.path()))
  110. .uploadProgress({count : 10, interval : -1}, (current, total) => {
  111. count++
  112. console.log(current, total)
  113. })
  114. })
  115. .then(() => {
  116. report(<Assert key="count is correct" expect={10} actual={count}/>)
  117. done()
  118. })
  119. }
  120. })