Keine Beschreibung

test-0.9.5.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. const fs = RNFetchBlob.fs
  17. const { Assert, Comparer, Info, prop } = RNTest
  18. const describe = RNTest.config({
  19. group : '0.9.5',
  20. run : true,
  21. expand : false,
  22. timeout : 20000,
  23. })
  24. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  25. const dirs = RNFetchBlob.fs.dirs
  26. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  27. describe('issue #122 force response data format', (report, done) => {
  28. RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/json-dummy-1.json`, {
  29. 'RNFB-Response' : 'base64'
  30. })
  31. .then((res) => {
  32. let r = RNFetchBlob.base64.decode(res.data)
  33. report(
  34. <Assert key="test data verify" expect="fetchblob-dev" actual={JSON.parse(r).name}/>,
  35. <Assert key="should successfully decode the data" expect={true} actual={true}/>)
  36. return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/json-dummy-1.json`)
  37. })
  38. .then((res) => {
  39. report(
  40. <Assert key="response should in format of plain-text" expect="fetchblob-dev" actual={JSON.parse(res.data).name}/>)
  41. done()
  42. })
  43. .catch(() => {
  44. report(
  45. <Assert key="Should successfully decode the data" expect={true} actual={false}/>)
  46. done()
  47. })
  48. })
  49. describe('#129 memory leaking when enable uploadProgress', (report, done) => {
  50. let file = null
  51. let count = 0
  52. RNFetchBlob.config({ fileCache : true })
  53. .fetch('GET', `${TEST_SERVER_URL}/public/6mb-dummy`)
  54. .then((res) => {
  55. file = res.path()
  56. for(let i=0;i<10;i++){
  57. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload`, {
  58. 'Transfer-Encoding' : 'Chunked'
  59. }, RNFetchBlob.wrap(file))
  60. .uploadProgress((current, total) => {})
  61. .then(() => {
  62. count ++
  63. if(count >= 10) {
  64. fs.unlink(file)
  65. done()
  66. }
  67. })
  68. }
  69. })
  70. })
  71. describe('#131 status code != 200 should not throw an error', (report, done) => {
  72. let count = 0
  73. let codes = [404, 500, 501, 403]
  74. codes.forEach((code) => {
  75. RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/xhr-code/${code}`, {
  76. 'Cache-Control' : 'no-store'
  77. })
  78. .then(function(res) {
  79. report(<Assert key={`status code should be ${this}`} expect={Math.floor(this)} actual={Math.floor(res.info().status)}/>)
  80. count ++
  81. if(count >= 4)
  82. done()
  83. }.bind(code))
  84. .catch(function(err) {
  85. report(<Assert key={`status code ${this} should not cause error`} expect={true} actual={false}/>)
  86. count++
  87. }.bind(code))
  88. })
  89. })