Sin descripción

test-0.9.4.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 readStream performance prepare the file', (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. report(<Info key="preparation complete"><Text>start in 3 seconds</Text></Info>)
  105. cache = res.path()
  106. setTimeout(() => {
  107. fs.readStream(cache, 'utf8', 1024000)
  108. .then((stream) => {
  109. stream.open()
  110. start = Date.now()
  111. stream.onData((chunk) => {
  112. count++
  113. size += chunk.length
  114. if(Date.now() - tick > 500) {
  115. tick = Date.now()
  116. report(
  117. <Info key="size" uid="100"><Text>{size} bytes read</Text></Info>)
  118. }
  119. })
  120. stream.onEnd(() => {
  121. report(
  122. <Info key="size" uid="100"><Text>{size} bytes read</Text></Info>,
  123. <Info key="elapsed"><Text>{Date.now() - start} ms</Text></Info>,
  124. <Info key="events"><Text>{count} times</Text></Info>,
  125. <Assert key="JS thread is not blocked" expect={true} actual={true}/>,)
  126. fs.stat(cache).then((stat) => {
  127. report(
  128. <Info key="info"><Text>{JSON.stringify(stat)}</Text></Info>)
  129. fs.unlink(cache)
  130. done()
  131. })
  132. })
  133. })
  134. }, 3000)
  135. })
  136. })
  137. describe('issue #120 upload progress should work when sending body as-is', (report, done) => {
  138. let data = RNTest.prop('image')
  139. let tick = 0
  140. let task = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  141. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  142. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  143. 'Content-Type' : 'text/plain; charset=dropbox-cors-hack',
  144. }, data)
  145. task.uploadProgress((current, total) => {
  146. tick ++
  147. })
  148. .then((res) => {
  149. let resp = res.json()
  150. report(
  151. <Info key="viewer"><Text>{JSON.stringify(resp)}</Text></Info>,
  152. <Assert key="event should be triggered"
  153. expect={tick}
  154. comparer={Comparer.greater} actual={0}/>)
  155. done()
  156. })
  157. })