Ei kuvausta

test-0.10.0.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. report(<Info key="report" uid="100">
  65. <Text>{count} records</Text>
  66. </Info>)
  67. done()
  68. })
  69. })
  70. })
  71. describe('cookie test', (report, done) => {
  72. let time = Date.now()
  73. RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/cookie/${time}`)
  74. .then((res) => RNFetchBlob.net.getCookies(`${TEST_SERVER_URL}`))
  75. .then((cookies) => {
  76. let result = /cookieName\=[^;]+/.exec(cookies[0])
  77. console.log(result, 'cookieName=' + time)
  78. report(<Assert key="cookie should not be empty"
  79. expect={'cookieName=' + time}
  80. actual={result[0]}/>)
  81. done()
  82. })
  83. })
  84. describe('SSL test #159', (report, done) => {
  85. RNFetchBlob.config({
  86. trusty : true
  87. })
  88. .fetch('GET', `${TEST_SERVER_URL_SSL}/public/github.png`, {
  89. 'Cache-Control' : 'no-store'
  90. })
  91. .then(res => {
  92. report(<Assert
  93. key="trusty request should pass"
  94. expect={true}
  95. actual={true}/>)
  96. return RNFetchBlob.fetch('GET',`${TEST_SERVER_URL_SSL}/public/github.png`)
  97. })
  98. .catch(e => {
  99. report(<Assert
  100. key="non-trusty request should not pass"
  101. expect={true}
  102. actual={true}/>)
  103. done()
  104. })
  105. })
  106. describe('#171 appendExt verify', (report, done) => {
  107. RNFetchBlob.config({
  108. fileCache : true,
  109. appendExt : 'png'
  110. })
  111. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`, {
  112. 'Cache-Control' : 'no-store'
  113. })
  114. .then(res => {
  115. console.log(res.path())
  116. report(<Assert
  117. key="extension appended to tmp path"
  118. actual={/.png$/.test(res.path())}
  119. expect={true}/>)
  120. return fs.stat(res.path())
  121. })
  122. .then(stat => {
  123. report(<Assert
  124. key="verify the file existence"
  125. expect="23975"
  126. actual={stat.size} />)
  127. done()
  128. })
  129. })
  130. describe('#173 issue with append option', (report, done) => {
  131. let dest = dirs.DocumentDir + '/tmp' + Date.now()
  132. RNFetchBlob.config({
  133. path : dest,
  134. overwrite : true
  135. })
  136. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  137. .then((res) => fs.stat(res.path()))
  138. .then((stat) => {
  139. report(<Assert
  140. key="file size check #1"
  141. expect="23975"
  142. actual={stat.size}/>)
  143. return RNFetchBlob.config({
  144. path : dest,
  145. overwrite : false
  146. })
  147. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  148. })
  149. .then((res) => fs.stat(res.path()))
  150. .then((stat) => {
  151. report(<Assert
  152. key="file size check #2"
  153. expect="47950"
  154. actual={stat.size}/>)
  155. return RNFetchBlob.config({
  156. path : dest,
  157. overwrite : true
  158. })
  159. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  160. })
  161. .then((res) => fs.stat(res.path()))
  162. .then((stat) => {
  163. report(<Assert
  164. key="file size check #3"
  165. expect="23975"
  166. actual={stat.size}/>)
  167. return RNFetchBlob.config({
  168. path : dest,
  169. })
  170. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  171. })
  172. .then((res) => fs.stat(res.path()))
  173. .then((stat) => {
  174. report(<Assert
  175. key="it should successfully overwrite existing file without config"
  176. expect="23975"
  177. actual={stat.size}/>)
  178. done()
  179. })
  180. })
  181. describe('#171 verification ', (report, done) => {
  182. RNFetchBlob
  183. .config({
  184. session: 'SESSION_NAME',
  185. fileCache: true,
  186. appendExt: 'mp4'
  187. })
  188. .fetch('GET', `${TEST_SERVER_URL}/public/cat-fu.mp4`)
  189. .then(res => {
  190. console.log(res.path())
  191. })
  192. })