설명 없음

test-0.9.4.js 5.7KB

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