Nessuna descrizione

test-0.10.0.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. Linking,
  10. Platform,
  11. Dimensions,
  12. BackAndroid,
  13. AsyncStorage,
  14. Image,
  15. } from 'react-native';
  16. window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
  17. window.Blob = RNFetchBlob.polyfill.Blob
  18. const JSONStream = RNFetchBlob.JSONStream
  19. const fs = RNFetchBlob.fs
  20. const { Assert, Comparer, Info, prop } = RNTest
  21. const describe = RNTest.config({
  22. group : '0.10.0',
  23. run : true,
  24. expand : false,
  25. timeout : 20000,
  26. })
  27. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  28. const dirs = RNFetchBlob.fs.dirs
  29. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  30. let begin = Date.now()
  31. describe('json stream via HTTP', (report, done) => {
  32. let count = 0
  33. JSONStream(`${TEST_SERVER_URL}/public/json-dummy.json`).node('name', (name) => {
  34. count++
  35. if(Date.now() - begin < 100)
  36. return
  37. begin = Date.now()
  38. report(<Info key="report" uid="100">
  39. <Text>{count} records</Text>
  40. </Info>)
  41. done()
  42. })
  43. })
  44. describe('json stream via fs', (report, done) => {
  45. let fetch2 = new RNFetchBlob.polyfill.Fetch({
  46. auto : true
  47. })
  48. let res = null
  49. let count = 0
  50. RNFetchBlob.config({
  51. fileCache : true
  52. })
  53. .fetch('GET',`${TEST_SERVER_URL}/public/json-dummy.json`)
  54. .then((resp) => {
  55. res = resp
  56. JSONStream({
  57. url : RNFetchBlob.wrap(res.path()),
  58. headers : { bufferSize : 10240 }
  59. }).node('name', (name) => {
  60. count++
  61. if(Date.now() - begin < 100)
  62. return
  63. begin = Date.now()
  64. console.log(count);
  65. // report(<Info key="report" uid="100">
  66. // <Text>{count} records</Text>
  67. // </Info>)
  68. // done()
  69. })
  70. })
  71. })
  72. describe('cookie test', (report, done) => {
  73. let time = Date.now()
  74. RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/cookie/${time}`)
  75. .then((res) => RNFetchBlob.net.getCookies(`${TEST_SERVER_URL}`))
  76. .then((cookies) => {
  77. let result = /cookieName\=[^;]+/.exec(cookies[0])
  78. console.log(result, 'cookieName=' + time)
  79. report(<Assert key="cookie should not be empty"
  80. expect={'cookieName=' + time}
  81. actual={result[0]}/>)
  82. done()
  83. })
  84. })
  85. describe('SSL test #159', (report, done) => {
  86. RNFetchBlob.config({
  87. trusty : true
  88. })
  89. .fetch('GET', `${TEST_SERVER_URL_SSL}/public/github.png`, {
  90. 'Cache-Control' : 'no-store'
  91. })
  92. .then(res => {
  93. report(<Assert
  94. key="trusty request should pass"
  95. expect={true}
  96. actual={true}/>)
  97. return RNFetchBlob.fetch('GET',`${TEST_SERVER_URL_SSL}/public/github.png`)
  98. })
  99. .catch(e => {
  100. report(<Assert
  101. key="non-trusty request should not pass"
  102. expect={true}
  103. actual={true}/>)
  104. done()
  105. })
  106. })
  107. describe('#171 appendExt verify', (report, done) => {
  108. RNFetchBlob.config({
  109. fileCache : true,
  110. appendExt : 'png'
  111. })
  112. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`, {
  113. 'Cache-Control' : 'no-store'
  114. })
  115. .then(res => {
  116. console.log(res.path())
  117. report(<Assert
  118. key="extension appended to tmp path"
  119. actual={/.png$/.test(res.path())}
  120. expect={true}/>)
  121. return fs.stat(res.path())
  122. })
  123. .then(stat => {
  124. report(<Assert
  125. key="verify the file existence"
  126. expect="23975"
  127. actual={stat.size} />)
  128. done()
  129. })
  130. })
  131. describe('#173 issue with append option', (report, done) => {
  132. let dest = dirs.DocumentDir + '/tmp' + Date.now()
  133. RNFetchBlob.config({
  134. path : dest,
  135. overwrite : true
  136. })
  137. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  138. .then((res) => fs.stat(res.path()))
  139. .then((stat) => {
  140. report(<Assert
  141. key="file size check #1"
  142. expect="23975"
  143. actual={stat.size}/>)
  144. return RNFetchBlob.config({
  145. path : dest,
  146. overwrite : false
  147. })
  148. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  149. })
  150. .then((res) => fs.stat(res.path()))
  151. .then((stat) => {
  152. report(<Assert
  153. key="file size check #2"
  154. expect="47950"
  155. actual={stat.size}/>)
  156. return RNFetchBlob.config({
  157. path : dest,
  158. overwrite : true
  159. })
  160. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  161. })
  162. .then((res) => fs.stat(res.path()))
  163. .then((stat) => {
  164. report(<Assert
  165. key="file size check #3"
  166. expect="23975"
  167. actual={stat.size}/>)
  168. return RNFetchBlob.config({
  169. path : dest,
  170. })
  171. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  172. })
  173. .then((res) => fs.stat(res.path()))
  174. .then((stat) => {
  175. report(<Assert
  176. key="it should successfully overwrite existing file without config"
  177. expect="23975"
  178. actual={stat.size}/>)
  179. done()
  180. })
  181. })
  182. describe('#171 verification ', (report, done) => {
  183. RNFetchBlob
  184. .config({
  185. session: 'SESSION_NAME',
  186. fileCache: true,
  187. appendExt: 'mp4'
  188. })
  189. .fetch('GET', `${TEST_SERVER_URL}/public/cat-fu.mp4`)
  190. .then(res => {
  191. console.log(res.path())
  192. })
  193. })