No Description

test-0.10.3.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import RNTest from './react-native-testkit/'
  2. import React from 'react'
  3. import _ from 'lodash'
  4. import RNFetchBlob from 'react-native-fetch-blob'
  5. import {
  6. StyleSheet,
  7. Text,
  8. View,
  9. ScrollView,
  10. Linking,
  11. Platform,
  12. Dimensions,
  13. BackAndroid,
  14. AsyncStorage,
  15. Image,
  16. } from 'react-native';
  17. window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
  18. window.Blob = RNFetchBlob.polyfill.Blob
  19. const JSONStream = RNFetchBlob.JSONStream
  20. const fs = RNFetchBlob.fs
  21. const { Assert, Comparer, Info, prop } = RNTest
  22. const describe = RNTest.config({
  23. group : '0.10.3',
  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. let begin = Date.now()
  32. describe('#230 #249 cookies manipulation', (report, done) => {
  33. RNFetchBlob
  34. .fetch('GET', `${TEST_SERVER_URL}/cookie/249230`)
  35. .then((res) => RNFetchBlob.net.getCookies())
  36. .then((cookies) => {
  37. console.log(cookies)
  38. report(<Assert
  39. key="should set 10 cookies"
  40. expect={10}
  41. actual={cookies['localhost'].length}/>)
  42. return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/cookie-echo`)
  43. })
  44. .then((res) => {
  45. console.log(res.data)
  46. let cookies = String(res.data).split(';')
  47. report(<Assert
  48. key="should send 10 cookies"
  49. expect={10}
  50. actual={cookies.length}/>)
  51. return RNFetchBlob.net.removeCookies()
  52. })
  53. .then(() => RNFetchBlob.net.getCookies('localhost'))
  54. .then((cookies) => {
  55. report(<Assert
  56. key="should have no cookies"
  57. expect={undefined}
  58. actual={cookies['localhost']}/>)
  59. return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/cookie-echo`)
  60. })
  61. .then((res) => {
  62. console.log(res.data)
  63. let cookies = String(res.data).split(';')
  64. cookies = _.reject(cookies, r => r.length < 2)
  65. report(<Assert
  66. key="should send no cookies"
  67. expect={0}
  68. actual={cookies.length}/>)
  69. done()
  70. })
  71. })
  72. describe('#254 IOS fs.stat lastModified date correction', (report, done) => {
  73. let path = dirs.DocumentDir + '/temp' + Date.now()
  74. fs.createFile(path, 'hello', 'utf8' )
  75. .then(() => fs.stat(path))
  76. .then((stat) => {
  77. console.log(stat)
  78. let p = stat.lastModified / Date.now()
  79. report(<Assert key="date is correct" expect={true} actual={ p< 1.05 && p > 0.95}/>)
  80. done()
  81. })
  82. })
  83. describe('#263 parallel request', (report, done) => {
  84. let urls = [
  85. `${TEST_SERVER_URL}/public/1mb-dummy`,
  86. `${TEST_SERVER_URL}/public/2mb-dummy`,
  87. `${TEST_SERVER_URL}/public/404`
  88. ]
  89. let size = [1000000, 1310720, 23]
  90. let asserts = []
  91. new Promise
  92. .all(urls.map((url) => RNFetchBlob.fetch('GET', url)))
  93. .then((results) => {
  94. _.each(results, (r, i) => {
  95. report(
  96. <Assert key={`redirect URL ${i} should be correct`}
  97. expect={urls[i]}
  98. actual={r.info().redirects[0]}/>)
  99. report(<Assert key={`content ${i} should be correct`}
  100. expect={size[i]}
  101. actual={r.data.length}/>)
  102. })
  103. done()
  104. })
  105. })
  106. describe('#264 network exceptions should be catachable', (report, done) => {
  107. let task = RNFetchBlob
  108. .config({ fileCache : true})
  109. .fetch('GET',`${TEST_SERVER_URL}/interrupt`)
  110. task
  111. .then((res) => {
  112. console.log(res.data)
  113. console.log(res.info())
  114. })
  115. .catch((err) => {
  116. console.log('##err',err)
  117. })
  118. })
  119. describe('readstream with empty buffer', (report, done) => {
  120. let data = { cool : 100 }
  121. let path = dirs.DocumentDir + '/test' + Date.now()
  122. let result = ''
  123. fs.writeFile(path, JSON.stringify(data), 'utf8')
  124. .then(() => fs.readStream(path, 'utf8'))
  125. .then((stream) => {
  126. stream.open()
  127. stream.onData((chunk) => { result += chunk })
  128. stream.onError((err) => console.log('err' + err))
  129. stream.onEnd(() => {
  130. console.log(result)
  131. console.log(JSON.parse(result))
  132. })
  133. })
  134. })