Sin descripción

test-0.7.0.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import RNTest from './react-native-testkit/'
  2. import React from 'react'
  3. import RNFetchBlob from 'react-native-fetch-blob'
  4. import Timer from 'react-timer-mixin'
  5. import {
  6. StyleSheet,
  7. Text,
  8. View,
  9. ScrollView,
  10. CameraRoll,
  11. Platform,
  12. Dimensions,
  13. Image,
  14. } from 'react-native';
  15. const fs = RNFetchBlob.fs
  16. const { Assert, Comparer, Info, prop } = RNTest
  17. const describe = RNTest.config({
  18. group : '0.7.0',
  19. run : true,
  20. expand : false,
  21. timeout : 300000000,
  22. })
  23. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  24. const dirs = RNFetchBlob.fs.dirs
  25. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  26. let bigfile = null
  27. describe('Upload and download large file', (report, done) => {
  28. let filename = Platform.OS+'-0.7.0-22mb-dummy-' + Date.now()
  29. let begin = -1
  30. let begin2 = -1
  31. let deb = Date.now()
  32. let download = false, upload = false
  33. RNFetchBlob.config({
  34. fileCache : true
  35. })
  36. .fetch('GET', `${TEST_SERVER_URL}/public/2mb-dummy`)
  37. .progress((now, total) => {
  38. download = true
  39. if(begin === -1)
  40. begin = Date.now()
  41. if(Date.now() - deb < 1000)
  42. return
  43. deb = Date.now()
  44. report(<Info uid="200" key="progress">
  45. <Text>
  46. {`download ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s) ${(100*now/total).toFixed(2)}%`}
  47. </Text>
  48. </Info>)
  49. })
  50. .then((res) => {
  51. bigfile = res.path()
  52. return fs.stat(bigfile)
  53. })
  54. .then((stat) => {
  55. report(<Info key="big file stat">
  56. <Text>{JSON.stringify(stat)}</Text>
  57. </Info>)
  58. let task = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  59. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  60. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+Date.now()+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  61. 'Content-Type' : 'application/octet-stream',
  62. }, RNFetchBlob.wrap(bigfile))
  63. begin = -1
  64. task.uploadProgress((now, total) => {
  65. upload = true
  66. if(begin === -1)
  67. begin = Date.now()
  68. if(Date.now() - deb < 1000)
  69. return
  70. deb = Date.now()
  71. report(<Info uid="300" key="upload progress">
  72. <Text>
  73. {`upload ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s) ${(100*now/total).toFixed(2)}%`}
  74. </Text>
  75. </Info>)
  76. })
  77. return task
  78. })
  79. .then(() => {
  80. report(<Assert key="upload and download event triggered" expect={true} actual={download && upload}/>)
  81. done()
  82. })
  83. })
  84. describe('cancel task should work properly', (report, done) => {
  85. let filename = Platform.OS+'-0.7.0-cancel-test-22mb-dummy-' + Date.now()
  86. let bytesWitten = 0
  87. let deb = Date.now()
  88. let begin = -1
  89. let task = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  90. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  91. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  92. 'Content-Type' : 'application/octet-stream',
  93. }, RNFetchBlob.wrap(bigfile))
  94. task.uploadProgress((now, total) => {
  95. bytesWitten = now
  96. if(Date.now() - deb < 1000)
  97. return
  98. deb = Date.now()
  99. if(begin === -1)
  100. begin = Date.now()
  101. let speed = Math.floor(now / (Date.now() - begin))
  102. report(<Info uid="100" key="progress">
  103. <Text>
  104. {`upload ${now} / ${total} bytes (${speed} kb/s)`}
  105. {` ${Math.floor((total-now)/speed/1000)} seconds left`}
  106. </Text>
  107. </Info>)
  108. })
  109. let checkpoint1 = 0
  110. Timer.setTimeout(() => {
  111. task.cancel()
  112. }, 5000)
  113. Timer.setTimeout(() => {
  114. checkpoint1 = bytesWitten
  115. }, 6000)
  116. Timer.setTimeout(() => {
  117. report(<Assert key="data should not write to stream after task is canceled"
  118. expect={checkpoint1}
  119. actual={bytesWitten}/>)
  120. fs.unlink(bigfile).then(() => {
  121. done()
  122. })
  123. }, 10000)
  124. task
  125. .then((res) => {
  126. report(
  127. <Assert key="task not canceled"
  128. expected={false}
  129. actual={true}/>)
  130. })
  131. .catch((resp) => {
  132. report(<Assert key="task cancelled rejection should be catachable"
  133. expect={true}
  134. actual={true}/>)
  135. done()
  136. })
  137. })