Sin descripción

test-0.6.3.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.6.3',
  19. run : true,
  20. expand : true,
  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. false && describe('massive HTTP request', (report, done) => {
  27. let promises = []
  28. let progress = []
  29. let begin = Date.now()
  30. for(let i=0; i<500;i++) {
  31. let p = RNFetchBlob
  32. .config({fileCache : true})
  33. .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  34. promises.push(p)
  35. }
  36. Promise.all(promises).then((resps) => {
  37. for(let i in resps) {
  38. fs.unlink(resps[i].path())
  39. }
  40. report(<Info key={`time = ${(Date.now() - begin) / 1000} sec`}></Info>)
  41. done()
  42. })
  43. })
  44. RNTest.config({
  45. group : '0.7.0',
  46. run : true,
  47. expand : false,
  48. timeout : 600000,
  49. })('Upload and download large file', (report, done) => {
  50. let filename = '22mb-dummy-' + Date.now()
  51. let begin = -1
  52. let begin2 = -1
  53. let deb = Date.now()
  54. RNFetchBlob.config({
  55. fileCache : true
  56. })
  57. .fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`)
  58. .progress((now, total) => {
  59. if(begin === -1)
  60. begin = Date.now()
  61. if(Date.now() - deb < 1000)
  62. return
  63. deb = Date.now()
  64. report(<Info uid="200" key="progress">
  65. <Text>
  66. {`download ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s)`}
  67. </Text>
  68. </Info>)
  69. })
  70. .then((res) => {
  71. try {
  72. deb = Date.now()
  73. let promise = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  74. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  75. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  76. 'Content-Type' : 'application/octet-stream',
  77. }, RNFetchBlob.wrap(res.path()))
  78. promise.uploadProgress((now, total) => {
  79. if(Date.now() - deb < 1000)
  80. return
  81. deb = Date.now()
  82. if(begin2 === -1)
  83. begin2 = Date.now()
  84. let speed = Math.floor(now / (Date.now() - begin2))
  85. report(<Info uid="100" key="progress">
  86. <Text>
  87. {`upload ${now} / ${total} bytes (${speed} kb/s)`}
  88. {` ${Math.floor((total-now)/speed/1000)} seconds left`}
  89. </Text>
  90. </Info>)
  91. })
  92. return promise
  93. } catch(err) { console.log(err) }
  94. })
  95. .then((res) => {
  96. report(<Assert
  97. key="upload should success without crashing app"
  98. expect={filename}
  99. actual={res.json().name}/>)
  100. done()
  101. })
  102. })