Nenhuma descrição

test-0.9.0.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. } from 'react-native';
  13. window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
  14. window.Blob = RNFetchBlob.polyfill.Blob
  15. window.fetch = new RNFetchBlob.polyfill.Fetch({
  16. auto : true,
  17. binaryContentTypes : ['image/', 'video/', 'audio/']
  18. }).build()
  19. const fs = RNFetchBlob.fs
  20. const { Assert, Comparer, Info, prop } = RNTest
  21. const describe = RNTest.config({
  22. group : '0.9.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. describe('cache control header and range request test', (report, done) => {
  31. let image = RNTest.prop('image')
  32. let part = [
  33. `${fs.dirs.DocumentDir}/cache-control-test-part1.png`,
  34. `${fs.dirs.DocumentDir}/cache-control-test-part2.png`,
  35. `${fs.dirs.DocumentDir}/cache-control-test-part3.png`
  36. ]
  37. let tmp = null
  38. RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  39. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  40. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  41. 'Content-Type' : 'application/octet-stream',
  42. }, image)
  43. .then((resp) => {
  44. resp = resp.json()
  45. report(
  46. <Assert key="confirm the file has been uploaded" expect={FILENAME} actual={resp.name}/>
  47. )
  48. return RNFetchBlob.config({
  49. path : part[0],
  50. })
  51. .fetch('GET', 'https://content.dropboxapi.com/1/files/auto/rn-upload/' + FILENAME, {
  52. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  53. 'Cache-Control' : 'no-store',
  54. 'Range' : 'bytes=0-23000'
  55. })
  56. })
  57. .then((res) => {
  58. let size = Math.floor(res.info().headers['Content-Length'])
  59. report(<Assert key="part2 content length = 23001" expect={23001} actual={size}/>)
  60. return RNFetchBlob.config({
  61. path : part[2]
  62. })
  63. .fetch('GET', 'https://content.dropboxapi.com/1/files/auto/rn-upload/' + FILENAME, {
  64. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  65. 'Range' : 'bytes=23001-23975',
  66. 'Cache-Control' : 'no-store'
  67. })
  68. })
  69. .then((res) => {
  70. let size = Math.floor(res.info().headers['Content-Length'])
  71. report(<Assert key="part3 content length = 975" expect={974} actual={size}/>)
  72. return fs.appendFile(part[0], part[2], 'uri')
  73. })
  74. .then((written) => {
  75. return fs.stat(part[0])
  76. })
  77. .then((stat) => {
  78. report(<Assert key="combined file size check" expect="23975" actual={stat.size}/>)
  79. done()
  80. })
  81. })
  82. describe('#73 unicode response BASE64 content test', (report, done) => {
  83. fetch(`${TEST_SERVER_URL}/unicode`, {
  84. method : 'GET'
  85. })
  86. .then((res) => {
  87. return res.json()
  88. })
  89. .then((data) => {
  90. report(<Assert key="data should correct" expect={'你好!'} actual={data.data}/>)
  91. done()
  92. })
  93. })
  94. describe('#73 unicode response content test', (report, done) => {
  95. let expect = '中文!檔案\\u00測試 ABCDE 測試'
  96. RNFetchBlob.config({ fileCache : true })
  97. .fetch('GET', `${TEST_SERVER_URL}/public/utf8-dummy`, {
  98. method : 'GET'
  99. })
  100. .then((res) => res.readFile('utf8'))
  101. .then((data) => {
  102. report(
  103. <Assert key="data should correct"
  104. expect={expect}
  105. actual={data}/>)
  106. done()
  107. })
  108. })
  109. describe = RNTest.config({
  110. group : '0.9.0',
  111. run : true,
  112. expand : true,
  113. timeout : 24000
  114. })
  115. describe('request should not retry after timed out', (report, done) => {
  116. let count = 0
  117. let task = RNFetchBlob
  118. .fetch('GET', `${TEST_SERVER_URL}/timeout408/${Date.now()}`)
  119. task.then((res) => {
  120. report(<Assert key="request should not success" expect={true} actual={false}/>)
  121. })
  122. .catch(() => {
  123. task.cancel()
  124. count ++
  125. })
  126. setTimeout(() => {
  127. report(<Assert key="request does not retry" expect={1} actual={count}/>)
  128. done()
  129. }, 12000)
  130. })
  131. describe = RNTest.config({
  132. group : '0.9.0',
  133. run : true,
  134. expand : true,
  135. timeout : 65000
  136. })
  137. describe('long live download or upload task won\'t timeout', (report, done) => {
  138. RNFetchBlob.config({timeout : 0})
  139. .fetch('GET', `${TEST_SERVER_URL}/long/`)
  140. .then((res) => {
  141. report(
  142. <Assert key="download not terminated" expect={true} actual={true}/>,
  143. <Info key={res.text()}/>)
  144. done()
  145. })
  146. })