Sin descripción

test-0.9.2.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.2',
  23. run : true,
  24. expand : true,
  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('content-length header test', (report, done) => {
  31. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/content-length`, {
  32. 'Content-Type' : 'text/plain',
  33. 'Content-Length' : '5'
  34. }, 'hello')
  35. .then((res) => {
  36. report(
  37. <Info key="response data">
  38. <Text>{res.text()}</Text>
  39. </Info>)
  40. done()
  41. })
  42. })
  43. describe('slice test', (report, done) => {
  44. let str = "PASSSTRING"
  45. let tmp = fs.dirs.DocumentDir + '/slice-tmp-'
  46. let testData = [
  47. {start: 4, contents: "STRING"},
  48. {start: 12, contents: ""},
  49. {start: 0, end: 4, contents: "PASS"},
  50. {start: 0, end: 12, contents: "PASSSTRING"},
  51. {start: 7, end: 4, contents: ""},
  52. {start: -6, contents: "STRING"},
  53. {start: -12, contents: "PASSSTRING"},
  54. {start: 0, end: -6, contents: "PASS"},
  55. {start: 0, end: -12, contents: ""},
  56. ]
  57. fs.writeFile(tmp, str, 'utf8')
  58. .then(() => {
  59. let promises = []
  60. for(let t in testData) {
  61. let p = fs.slice(tmp, tmp + t, testData[t].start, testData[t].end)
  62. .then(function(num) {
  63. console.log('slice finished', num)
  64. return fs.readFile(tmp + num, 'utf8')
  65. .then((data) => {
  66. report(<Assert key={`assertion-${num}`} expect={testData[num].contents} actual={data}/>)
  67. return Promise.resolve()
  68. })
  69. }.bind(this, t))
  70. promises.push(p)
  71. }
  72. Promise.all(promises).then((res) => {
  73. done()
  74. })
  75. })
  76. })
  77. describe('fs.slice test', (report, done) => {
  78. let source = null
  79. let parts = fs.dirs.DocumentDir + '/tmp-source-'
  80. let dests = []
  81. let combined = fs.dirs.DocumentDir + '/combined-' + Date.now() + '.jpg'
  82. let size = 0
  83. window.fetch = new RNFetchBlob.polyfill.Fetch({
  84. auto : true,
  85. binaryContentTypes : ['image/', 'video/', 'audio/']
  86. }).build()
  87. fetch(`${TEST_SERVER_URL}/public/github2.jpg`)
  88. .then((res) => res.rawResp())
  89. .then((res) => {
  90. source = res.path()
  91. return fs.stat(source)
  92. })
  93. // separate file into 4kb chunks
  94. .then((stat) => {
  95. size = stat.size
  96. let promise = Promise.resolve()
  97. let cursor = 0
  98. while(cursor < size) {
  99. promise = promise.then(function(start) {
  100. console.log('slicing part ', start , start + 40960)
  101. let offset = 0
  102. return fs.slice(source, parts + start, start + offset, start + 40960)
  103. .then((dest) => {
  104. console.log('slicing part ', start + offset, start + 40960, 'done')
  105. dests.push(dest)
  106. return Promise.resolve()
  107. })
  108. }.bind(this, cursor))
  109. cursor += 40960
  110. }
  111. console.log('loop end')
  112. return promise
  113. })
  114. // combine chunks and verify the result
  115. .then(() => {
  116. console.log('combinding files')
  117. let p = Promise.resolve()
  118. for(let d in dests) {
  119. p = p.then(function(chunk){
  120. return fs.appendFile(combined, chunk, 'uri').then((write) => {
  121. console.log(write, 'bytes write')
  122. })
  123. }.bind(this, dests[d]))
  124. }
  125. return p.then(() => fs.stat(combined))
  126. })
  127. .then((stat) => {
  128. report(
  129. <Assert key="verify file size" expect={size} actual={stat.size}/>,
  130. <Info key="image viewer">
  131. <Image key="combined image" style={styles.image} source={{ uri : prefix + combined}}/>
  132. </Info>)
  133. done()
  134. })
  135. })