Brak opisu

test-0.7.0.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. RNFetchBlob.config({
  33. fileCache : true
  34. })
  35. .fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`)
  36. .progress((now, total) => {
  37. if(begin === -1)
  38. begin = Date.now()
  39. if(Date.now() - deb < 1000)
  40. return
  41. deb = Date.now()
  42. report(<Info uid="200" key="progress">
  43. <Text>
  44. {`download ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s) ${(100*now/total).toFixed(2)}%`}
  45. </Text>
  46. </Info>)
  47. })
  48. .then((res) => {
  49. bigfile = res.path()
  50. return fs.stat(bigfile)
  51. })
  52. .then((stat) => {
  53. report(<Info key="big file stat">
  54. <Text>{JSON.stringify(stat)}</Text>
  55. </Info>)
  56. done()
  57. })
  58. })
  59. describe('cancel task should work properly', (report, done) => {
  60. let filename = Platform.OS+'-0.7.0-cancel-test-22mb-dummy-' + Date.now()
  61. let bytesWitten = 0
  62. let deb = Date.now()
  63. let begin = -1
  64. let task = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  65. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  66. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  67. 'Content-Type' : 'application/octet-stream',
  68. }, RNFetchBlob.wrap(bigfile))
  69. task.uploadProgress((now, total) => {
  70. bytesWitten = now
  71. if(Date.now() - deb < 1000)
  72. return
  73. deb = Date.now()
  74. if(begin === -1)
  75. begin = Date.now()
  76. let speed = Math.floor(now / (Date.now() - begin))
  77. report(<Info uid="100" key="progress">
  78. <Text>
  79. {`upload ${now} / ${total} bytes (${speed} kb/s)`}
  80. {` ${Math.floor((total-now)/speed/1000)} seconds left`}
  81. </Text>
  82. </Info>)
  83. })
  84. let checkpoint1 = 0
  85. Timer.setTimeout(() => {
  86. task.cancel()
  87. }, 5000)
  88. Timer.setTimeout(() => {
  89. checkpoint1 = bytesWitten
  90. }, 6000)
  91. Timer.setTimeout(() => {
  92. report(<Assert key="data should not write to stream after task is canceled"
  93. expect={checkpoint1}
  94. actual={bytesWitten}/>)
  95. fs.unlink(bigfile).then(() => {
  96. done()
  97. })
  98. }, 10000)
  99. task
  100. .then((res) => {
  101. report(
  102. <Assert key="task not canceled"
  103. expected={false}
  104. actual={true}/>)
  105. })
  106. .catch((resp) => {
  107. report(<Assert key="task cancelled rejection should be catachable"
  108. expect={true}
  109. actual={true}/>)
  110. })
  111. })