No Description

test-0.9.4.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. TouchableOpacity,
  13. } from 'react-native';
  14. window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
  15. window.Blob = RNFetchBlob.polyfill.Blob
  16. const fs = RNFetchBlob.fs
  17. const { Assert, Comparer, Info, prop } = RNTest
  18. const describe = RNTest.config({
  19. group : '0.9.4',
  20. run : true,
  21. expand : true,
  22. timeout : 20000,
  23. })
  24. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  25. const dirs = RNFetchBlob.fs.dirs
  26. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  27. describe('issue #105', (report, done) => {
  28. let tmp = null
  29. RNFetchBlob
  30. .config({ fileCache : true })
  31. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  32. .then((res) => {
  33. tmp = res.path()
  34. return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  35. 'Content-Type' : 'multipart/form-data',
  36. 'Expect' : '100-continue'
  37. }, [
  38. { name : 'data', data : 'issue#105 test' },
  39. { name : 'file', filename : 'github.png', data : RNFetchBlob.wrap(tmp) }
  40. ])
  41. })
  42. .then((res) => {
  43. done()
  44. })
  45. })
  46. describe('issue #106', (report, done) => {
  47. fetch('https://rnfb-test-app.firebaseapp.com/6m-json.json')
  48. .then((res) => {
  49. console.log('## converted')
  50. return res.json()
  51. })
  52. .then((data) => {
  53. // console.log(data)
  54. report(<Assert key="fetch request success" expect={20000} actual={data.total}/>)
  55. done()
  56. })
  57. })
  58. describe('issue #111 get redirect destination', (report, done) => {
  59. RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
  60. .then((res) => {
  61. console.log(res.info())
  62. report(
  63. <Assert key="redirect history should tracable"
  64. expect={2}
  65. actual={res.info().redirects.length}/>,
  66. <Assert key="redirect history verify"
  67. expect={[`${TEST_SERVER_URL}/redirect`, `${TEST_SERVER_URL}/public/github.png`]}
  68. comparer={Comparer.equalToArray}
  69. actual={res.info().redirects}/>,
  70. )
  71. done()
  72. })
  73. })
  74. describe('chunked encoding option test', (report, done) => {
  75. let path = null
  76. let base64 = null
  77. RNFetchBlob
  78. // .config({ fileCache : true })
  79. .fetch('GET', `${TEST_SERVER_URL}/public/1600k-img-dummy.jpg`)
  80. .then((res) => {
  81. base64 = res.base64()
  82. return RNFetchBlob
  83. .fetch('POST', `${TEST_SERVER_URL}/upload`, {
  84. 'Content-Type' : 'application/octet-stream;BASE64'
  85. }, base64)
  86. })
  87. .then((res) => {
  88. let headers = res.info().headers
  89. console.log(res.text())
  90. report(<Assert key="request should not use chunked encoding"
  91. expect={undefined}
  92. actual={headers['transfer-encoding']}/>)
  93. fs.unlink(path)
  94. done()
  95. })
  96. })
  97. describe('#118 readStream performance prepare the file', (report, done) => {
  98. let cache = null
  99. let size = 0
  100. let size2 = 0
  101. let tick = Date.now()
  102. let tick2 = Date.now()
  103. let start = -1
  104. let start2 = -1
  105. let count = 0
  106. let task = RNFetchBlob.config({fileCache : true})
  107. .fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`)
  108. task.progress((current, total) => {
  109. report(<Info key="prepare file" uid="prepare">
  110. <Text key="pg"> {Math.floor(current/total*100)}% </Text>
  111. </Info>)
  112. })
  113. task.then((res) => {
  114. report(<Info key="preparation complete"><Text>start in 3 seconds</Text></Info>)
  115. cache = res.path()
  116. setTimeout(readFile, 2500)
  117. function readFile() {
  118. fs.readStream(cache, 'utf8', 102400, 10)
  119. .then((stream) => {
  120. stream.open()
  121. start = Date.now()
  122. stream.onData((chunk) => {
  123. count++
  124. size += chunk.length
  125. if(Date.now() - tick > 500) {
  126. console.log(size, ' read')
  127. tick = Date.now()
  128. report(
  129. <Info key="size" uid="100">
  130. <Text key="AA">File 1 {size}/22000000 bytes read</Text>
  131. <Text key="BB">File 2 {size2}/22000000 bytes read</Text>
  132. </Info>)
  133. }
  134. })
  135. stream.onEnd(() => {
  136. report(
  137. <Info key="size" uid="100"><Text>{size} bytes read</Text></Info>,
  138. <Info key="elapsed"><Text>{Date.now() - start} ms</Text></Info>,
  139. <Info key="events"><Text>{count} times</Text></Info>,
  140. <Assert key="JS thread is not blocked" expect={true} actual={true}/>,)
  141. fs.stat(cache).then((stat) => {
  142. report(
  143. <Info key="info"><Text>{JSON.stringify(stat)}</Text></Info>)
  144. fs.unlink(cache)
  145. done()
  146. })
  147. })
  148. })
  149. }
  150. })
  151. })
  152. describe('issue #120 upload progress should work when sending body as-is', (report, done) => {
  153. let data = RNTest.prop('image')
  154. let tick = 0
  155. let task = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  156. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  157. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  158. 'Content-Type' : 'text/plain; charset=dropbox-cors-hack',
  159. }, data)
  160. task.uploadProgress((current, total) => {
  161. tick ++
  162. })
  163. .then((res) => {
  164. let resp = res.json()
  165. report(
  166. <Info key="viewer"><Text>{JSON.stringify(resp)}</Text></Info>,
  167. <Assert key="event should be triggered"
  168. expect={tick}
  169. comparer={Comparer.greater} actual={0}/>)
  170. done()
  171. })
  172. })