Keine Beschreibung

test-0.9.4.js 5.3KB

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. //
  46. // describe('issue #106', (report, done) => {
  47. //
  48. // fetch('https://rnfb-test-app.firebaseapp.com/6m-json.json')
  49. // .then((res) => {
  50. // console.log('## converted')
  51. // return res.json()
  52. // })
  53. // .then((data) => {
  54. // // console.log(data)
  55. // report(<Assert key="fetch request success" expect={20000} actual={data.total}/>)
  56. // done()
  57. // })
  58. //
  59. // })
  60. //
  61. // describe('issue #111 get redirect destination', (report, done) => {
  62. // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
  63. // .then((res) => {
  64. // report(
  65. // <Assert key="redirect history should tracable"
  66. // expect={2}
  67. // actual={res.info().redirects.length}/>,
  68. // <Assert key="redirect history verify"
  69. // expect={[`${TEST_SERVER_URL}/redirect`, `${TEST_SERVER_URL}/public/github.png`]}
  70. // comparer={Comparer.equalToArray}
  71. // actual={res.info().redirects}/>,
  72. // )
  73. // done()
  74. // })
  75. // })
  76. //
  77. // describe('chunked encoding option test', (report, done) => {
  78. //
  79. // let path = null
  80. // let base64 = null
  81. //
  82. // RNFetchBlob
  83. // // .config({ fileCache : true })
  84. // .fetch('GET', `${TEST_SERVER_URL}/public/1600k-img-dummy.jpg`)
  85. // .then((res) => {
  86. // base64 = res.base64()
  87. // return RNFetchBlob
  88. // .fetch('POST', `${TEST_SERVER_URL}/upload`, {
  89. // 'Content-Type' : 'application/octet-stream;BASE64'
  90. // }, base64)
  91. // })
  92. // .then((res) => {
  93. // let headers = res.info().headers
  94. // console.log(res.text())
  95. // report(<Assert key="request should not use chunked encoding"
  96. // expect={undefined}
  97. // actual={headers['transfer-encoding']}/>)
  98. // fs.unlink(path)
  99. // done()
  100. // })
  101. // })
  102. describe('#118 readStream performance prepare the file', (report, done) => {
  103. let cache = null
  104. let size = 0
  105. let tick = Date.now()
  106. let start = -1
  107. let count = 0
  108. RNFetchBlob.config({fileCache : true})
  109. .fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`)
  110. .then((res) => {
  111. report(<Info key="preparation complete"><Text>start in 3 seconds</Text></Info>)
  112. cache = res.path()
  113. setTimeout(() => {
  114. fs.readStream(cache, 'utf8', 102400)
  115. .then((stream) => {
  116. stream.open()
  117. start = Date.now()
  118. stream.onData((chunk) => {
  119. count++
  120. size += chunk.length
  121. if(Date.now() - tick > 500) {
  122. tick = Date.now()
  123. report(
  124. <Info key="size" uid="100"><Text>{size} bytes read</Text></Info>)
  125. }
  126. })
  127. stream.onEnd(() => {
  128. report(
  129. <Info key="size" uid="100"><Text>{size} bytes read</Text></Info>,
  130. <Info key="elapsed"><Text>{Date.now() - start} ms</Text></Info>,
  131. <Info key="events"><Text>{count} times</Text></Info>,
  132. <Assert key="JS thread is not blocked" expect={true} actual={true}/>,)
  133. fs.stat(cache).then((stat) => {
  134. report(
  135. <Info key="info"><Text>{JSON.stringify(stat)}</Text></Info>)
  136. fs.unlink(cache)
  137. done()
  138. })
  139. })
  140. })
  141. }, 3000)
  142. })
  143. })
  144. // describe('issue #120 upload progress should work when sending body as-is', (report, done) => {
  145. //
  146. // let data = RNTest.prop('image')
  147. // let tick = 0
  148. //
  149. // let task = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  150. // Authorization : `Bearer ${DROPBOX_TOKEN}`,
  151. // 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  152. // 'Content-Type' : 'text/plain; charset=dropbox-cors-hack',
  153. // }, data)
  154. //
  155. // task.uploadProgress((current, total) => {
  156. // tick ++
  157. // })
  158. // .then((res) => {
  159. // let resp = res.json()
  160. // report(
  161. // <Info key="viewer"><Text>{JSON.stringify(resp)}</Text></Info>,
  162. // <Assert key="event should be triggered"
  163. // expect={tick}
  164. // comparer={Comparer.greater} actual={0}/>)
  165. // done()
  166. // })
  167. //
  168. // })