Brak opisu

test-0.8.0.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. const fs = RNFetchBlob.fs
  14. const { Assert, Comparer, Info, prop } = RNTest
  15. const describe = RNTest.config({
  16. group : '0.8.0',
  17. run : true,
  18. expand : true,
  19. timeout : 10000,
  20. })
  21. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  22. const dirs = RNFetchBlob.fs.dirs
  23. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  24. describe('fs URI encoding support', (report, done) => {
  25. let testFiles = []
  26. let sizes = []
  27. RNFetchBlob.config({
  28. fileCache : true
  29. })
  30. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
  31. .then((res) => {
  32. testFiles.push(res.path())
  33. sizes.push(Math.floor(res.info().headers['Content-Length']))
  34. return RNFetchBlob.config({fileCache : true}).fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
  35. })
  36. .then((res) => {
  37. testFiles.push(res.path())
  38. sizes.push(Math.floor(res.info().headers['Content-Length']))
  39. return fs.appendFile(testFiles[0], testFiles[1], 'uri')
  40. })
  41. .then(() => fs.stat(testFiles[0]))
  42. .then((stat) => {
  43. report(
  44. <Assert key="append content from URI should be correct"
  45. expect={sizes[0] + sizes[1]}
  46. actual={Math.floor(stat.size)}
  47. />)
  48. return fs.writeFile(testFiles[0], testFiles[1], 'uri')
  49. })
  50. .then(() => fs.stat(testFiles[0]))
  51. .then((stat) => {
  52. report(
  53. <Assert key="replace content from URI should be correct"
  54. expect={sizes[1]}
  55. actual={Math.floor(stat.size)}
  56. />)
  57. done()
  58. })
  59. })
  60. describe('request timeout working properly', (report, done) => {
  61. RNFetchBlob.config({ timeout : 1000 })
  62. .fetch('GET', `${TEST_SERVER_URL}/timeout`)
  63. .then(() => {
  64. report(
  65. <Assert
  66. key="should not execute successfully"
  67. expect={true}
  68. actual={false}/>)
  69. done()
  70. })
  71. .catch((err) => {
  72. report(
  73. <Assert
  74. key="expect timeout error"
  75. expect={true}
  76. actual={/timed out/ig.test(err)}/>)
  77. done()
  78. })
  79. })
  80. describe('regular request should have correct body', (report, done) => {
  81. RNFetchBlob
  82. .fetch('POST', `${TEST_SERVER_URL}/echo`, {
  83. 'content-type' : 'text/foo',
  84. foo : 'bar'
  85. }, 'foo is bar')
  86. .then((res) => {
  87. report(
  88. <Assert key="check headers"
  89. expect={'bar'}
  90. actual={res.json().headers.foo}/>,
  91. <Assert key="check content type"
  92. expect={'text/foo'}
  93. actual={res.json().headers['content-type']}/>,
  94. <Assert key="check body"
  95. expect={'foo is bar'}
  96. actual={res.json().body}/>)
  97. done()
  98. })
  99. })
  100. describe('automatic content conversion test', (report, done) => {
  101. let expect1 = `test-alpha-${Date.now()}`
  102. let expect2 = `test-beta-${Date.now()}`
  103. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/echo`, {
  104. 'Content-Type' : 'application/octet-foo',
  105. }, RNFetchBlob.base64.encode(expect1))
  106. .then((res) => {
  107. report(
  108. <Assert key="request body should be decoded by BASE64 decoder"
  109. expect={expect1}
  110. actual={res.json().body}/>)
  111. return fs.writeFile(dirs.DocumentDir + '/test-0.8.0-auto', expect2, 'utf8')
  112. })
  113. .then(() => RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/echo`, {
  114. /* what ever the header is */
  115. }, RNFetchBlob.wrap(dirs.DocumentDir + '/test-0.8.0-auto')))
  116. .then((resp) => {
  117. report(
  118. <Assert key="request body should send from storage"
  119. expect={expect2}
  120. actual={resp.json().body}/>)
  121. done()
  122. })
  123. })
  124. function getASCIIArray(str) {
  125. let r = []
  126. for(let i=0;i<str.length;i++) {
  127. r.push(str[i].charCodeAt(0))
  128. }
  129. return r
  130. }