Brak opisu

test-0.9.4.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
  14. window.Blob = RNFetchBlob.polyfill.Blob
  15. const fs = RNFetchBlob.fs
  16. const { Assert, Comparer, Info, prop } = RNTest
  17. const describe = RNTest.config({
  18. group : '0.9.4',
  19. run : true,
  20. expand : true,
  21. timeout : 20000,
  22. })
  23. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  24. const dirs = RNFetchBlob.fs.dirs
  25. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  26. describe('issue #105', (report, done) => {
  27. let tmp = null
  28. RNFetchBlob
  29. .config({ fileCache : true })
  30. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  31. .then((res) => {
  32. tmp = res.path()
  33. return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  34. 'Content-Type' : 'multipart/form-data',
  35. 'Expect' : '100-continue'
  36. }, [
  37. { name : 'data', data : 'issue#105 test' },
  38. { name : 'file', filename : 'github.png', data : RNFetchBlob.wrap(tmp) }
  39. ])
  40. })
  41. .then((res) => {
  42. done()
  43. })
  44. })
  45. describe('issue #106', (report, done) => {
  46. fetch('https://rnfb-test-app.firebaseapp.com/6m-json.json')
  47. .then((res) => {
  48. console.log('## converted')
  49. return res.json()
  50. })
  51. .then((data) => {
  52. // console.log(data)
  53. report(<Assert key="fetch request success" expect={20000} actual={data.total}/>)
  54. done()
  55. })
  56. })
  57. describe('issue #111 get redirect destination', (report, done) => {
  58. RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
  59. .then((res) => {
  60. report(
  61. <Assert key="redirect history should tracable"
  62. expect={2}
  63. actual={res.info().redirects.length}/>,
  64. <Assert key="redirect history verify"
  65. expect={[`${TEST_SERVER_URL}/redirect`, `${TEST_SERVER_URL}/public/github.png`]}
  66. comparer={Comparer.equalToArray}
  67. actual={res.info().redirects}/>,
  68. )
  69. done()
  70. })
  71. })
  72. describe('chunked encoding option test', (report, done) => {
  73. let path = null
  74. let base64 = null
  75. RNFetchBlob
  76. // .config({ fileCache : true })
  77. .fetch('GET', `${TEST_SERVER_URL}/public/1600k-img-dummy.jpg`)
  78. .then((res) => {
  79. base64 = res.base64()
  80. return RNFetchBlob
  81. .fetch('POST', `${TEST_SERVER_URL}/upload`, {
  82. 'Content-Type' : 'application/octet-stream;BASE64'
  83. }, base64)
  84. })
  85. .then((res) => {
  86. let headers = res.info().headers
  87. console.log(res.text())
  88. report(<Assert key="request should not use chunked encoding"
  89. expect={undefined}
  90. actual={headers['transfer-encoding']}/>)
  91. fs.unlink(path)
  92. done()
  93. })
  94. })
  95. describe('#118 ', (report, done) => {
  96. let cache = null
  97. let size = 0
  98. let tick = Date.now()
  99. let start = -1
  100. let count = 0
  101. RNFetchBlob.config({fileCache : true})
  102. .fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`)
  103. .then((res) => {
  104. cache = res.path()
  105. return fs.readStream(cache, 'utf8', 102400)
  106. })
  107. // .then((res) => {
  108. // cache = res.path()
  109. // return fs.readFile(cache, 'utf8')
  110. // })
  111. // .then(() => {
  112. // report(<Info key="good" />)
  113. // done()
  114. // fs.unlink(cache)
  115. // })
  116. .then((stream) => {
  117. stream.open()
  118. start = Date.now()
  119. stream.onData((chunk) => {
  120. count++
  121. size += chunk.length
  122. if(Date.now() - tick > 500) {
  123. tick = Date.now()
  124. report(
  125. <Info key="size" uid="100"><Text>{size} bytes read</Text></Info>)
  126. }
  127. })
  128. stream.onEnd(() => {
  129. report(
  130. <Info key="size" uid="100"><Text>{size} bytes read</Text></Info>,
  131. <Info key="elapsed"><Text>{Date.now() - start} ms</Text></Info>,
  132. <Info key="events"><Text>{count} times</Text></Info>,
  133. <Assert key="JS thread is not blocked" expect={true} actual={true}/>,)
  134. fs.stat(cache).then((stat) => {
  135. report(
  136. <Info key="info"><Text>{JSON.stringify(stat)}</Text></Info>)
  137. fs.unlink(cache)
  138. })
  139. })
  140. })
  141. })