Nessuna descrizione

test-0.8.0.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 testData1 = `test date write file from file ${Date.now()}`
  26. let testData2 = `test date write file from file ${Date.now()*Math.random()}`
  27. let file1 = dirs.DocumentDir + '/testFiletFile1' + Date.now()
  28. let file2 = dirs.DocumentDir + '/testFiletFile2' + Date.now()
  29. let init = [fs.createFile(file1, testData1, 'utf8'),
  30. fs.createFile(file2, testData2, 'utf8')]
  31. Promise.all(init)
  32. .then(() => fs.appendFile(file1, file2, 'uri'))
  33. .then(() => fs.readFile(file1, 'utf8'))
  34. .then((data) => {
  35. report(
  36. <Assert key="append content from URI should be correct"
  37. expect={testData1 + testData2}
  38. actual={data}
  39. />)
  40. return fs.writeFile(file1, file2, 'uri')
  41. })
  42. .then(() => fs.readFile(file1, 'utf8'))
  43. .then((data) => {
  44. report(
  45. <Assert key="write content from URI should be correct"
  46. expect={testData2}
  47. actual={data}
  48. />)
  49. done()
  50. })
  51. })
  52. describe('request timeout working properly', (report, done) => {
  53. RNFetchBlob.config({ timeout : 1000 })
  54. .fetch('GET', `${TEST_SERVER_URL}/timeout`)
  55. .then(() => {
  56. report(
  57. <Assert
  58. key="should not execute successfully"
  59. expect={true}
  60. actual={false}/>)
  61. done()
  62. })
  63. .catch((err) => {
  64. report(
  65. <Assert
  66. key="expect timeout error"
  67. expect={true}
  68. actual={/timed out/ig.test(err)}/>)
  69. done()
  70. })
  71. })
  72. describe('regular request should have correct body', (report, done) => {
  73. RNFetchBlob
  74. .fetch('POST', `${TEST_SERVER_URL}/echo`, {
  75. 'content-type' : 'text/foo',
  76. foo : 'bar'
  77. }, 'foo is bar')
  78. .then((res) => {
  79. report(
  80. <Assert key="check headers"
  81. expect={'bar'}
  82. actual={res.json().headers.foo}/>,
  83. <Assert key="check content type"
  84. expect={'text/foo'}
  85. actual={res.json().headers['content-type']}/>,
  86. <Assert key="check body"
  87. expect={'foo is bar'}
  88. actual={res.json().body}/>)
  89. done()
  90. })
  91. })
  92. describe('automatic content conversion test', (report, done) => {
  93. let expect1 = `test-alpha-${Date.now()}`
  94. let expect2 = `test-beta-${Date.now()}`
  95. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/echo`, {
  96. 'Content-Type' : 'application/octet-foo',
  97. }, RNFetchBlob.base64.encode(expect1))
  98. .then((res) => {
  99. report(
  100. <Assert key="request body should be decoded by BASE64 decoder"
  101. expect={expect1}
  102. actual={res.json().body}/>)
  103. return fs.writeFile(dirs.DocumentDir + '/test-0.8.0-auto', expect2, 'utf8')
  104. })
  105. .then(() => RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/echo`, {
  106. /* what ever the header is */
  107. }, RNFetchBlob.wrap(dirs.DocumentDir + '/test-0.8.0-auto')))
  108. .then((resp) => {
  109. report(
  110. <Assert key="request body should send from storage"
  111. expect={expect2}
  112. actual={resp.json().body}/>)
  113. done()
  114. })
  115. })
  116. function getASCIIArray(str) {
  117. let r = []
  118. for(let i=0;i<str.length;i++) {
  119. r.push(str[i].charCodeAt(0))
  120. }
  121. return r
  122. }