No Description

test-0.10.2.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.2',
  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('#227 IOS file modification date correctness', (report, done) => {
  33. let path = dirs.DocumentDir + '/issue-223-' + Date.now()
  34. fs.createFile(path, 'datafornow')
  35. .then(() => fs.stat(path))
  36. .then((stat) => {
  37. let date = stat.lastModified;
  38. console.log(date, stat);
  39. let correct = date/Date.now() > 0.95 || date/Date.now() < 1.05;
  40. report(<Assert key="modification date should be correct"
  41. expect={true} actual={correct}/>);
  42. done()
  43. })
  44. })
  45. describe('#230 add and option for setting if the request follow redirect or not', (report, done) => {
  46. RNFetchBlob
  47. .config({ followRedirect : false })
  48. .fetch('GET',`${TEST_SERVER_URL}/redirect`)
  49. .then((res) => {
  50. console.log(res.data)
  51. report(<Assert key="should not redirect twice" expect={1} actual={res.info().redirects.length}/>);
  52. done()
  53. })
  54. })
  55. // describe('#240 openDocument does not support file URI', (report, done) => {
  56. // RNFetchBlob
  57. // .config({ path : dirs.DocumentDir + '/app copy.png' })
  58. // .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  59. // .then((res) => {
  60. // RNFetchBlob.ios.openDocument(res.path())
  61. // .then(() => {
  62. // done();
  63. // })
  64. // .catch((err) => {
  65. // console.log(err)
  66. // })
  67. // })
  68. //
  69. // })
  70. describe('#241 null header silent failed issue', (report, done) => {
  71. RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/github.png`, {
  72. foo : null
  73. })
  74. .then(() => {
  75. report(<Assert key="null header should not crash the app"
  76. expect={true}
  77. actual={true}/>)
  78. done()
  79. })
  80. })
  81. describe('#247 binary data UTF8 encoding causes app crash', (report, done) => {
  82. RNFetchBlob
  83. .config({fileCache : true})
  84. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  85. .then((res) => fs.readStream(res.path(), 'utf8'))
  86. .then((stream) => {
  87. stream.open()
  88. stream.onError((err) => {
  89. report(<Assert
  90. key="read binary data to UTF8 should cause error but not crash the app"
  91. expect={true}
  92. actual={true}/>)
  93. done()
  94. })
  95. })
  96. })
  97. describe('#248 create blob from file has spaces in filename', (report, done) => {
  98. let source = '',
  99. size = 0,
  100. path = 'archive image.zip'
  101. RNFetchBlob
  102. .config({path : fs.dirs.DocumentDir +'/' + path})
  103. .fetch('GET', `${TEST_SERVER_URL}/public/issue-248-dummy.zip`)
  104. .then((res) => {
  105. source = res.path();
  106. console.log('source=', source)
  107. window.Blob = RNFetchBlob.polyfill.Blob;
  108. return Blob.build(RNFetchBlob.wrap(source), { type : 'application/zip'})
  109. })
  110. .then((b) => {
  111. console.log(b)
  112. size = b.size
  113. return fs.stat(b._ref)
  114. })
  115. .then((stat) => {
  116. report(<Assert key="blob created without error"
  117. expect={stat.size} actual={size}/>)
  118. return RNFetchBlob.fetch('POST',
  119. `${TEST_SERVER_URL}/upload-form`,
  120. {
  121. 'Content-Type' : 'multipart/form-data'
  122. }, [
  123. {
  124. name : 'file',
  125. filename : 'file name '+Platform.OS+'.zip',
  126. type : 'application/zip',
  127. data : RNFetchBlob.wrap(source)
  128. }
  129. ])
  130. })
  131. .then(() => {
  132. done()
  133. })
  134. })