Bez popisu

test-init.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. Dimensions,
  10. Platform,
  11. Dimension,
  12. Image,
  13. } from 'react-native';
  14. const { Assert, Comparer, Info, prop } = RNTest
  15. // test environment variables
  16. prop('FILENAME', `${Platform.OS}-0.8.0-${Date.now()}.png`)
  17. prop('TEST_SERVER_URL', 'http://192.168.0.11:8123')
  18. prop('TEST_SERVER_URL_SSL', 'https://192.168.0.11:8124')
  19. prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
  20. prop('styles', {
  21. image : {
  22. width: Dimensions.get('window').width*0.9,
  23. height : Dimensions.get('window').width*0.9,
  24. margin : 16,
  25. flex : 1
  26. }
  27. })
  28. const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop()
  29. const describe = RNTest.config({
  30. run : true,
  31. expand : true,
  32. timeout : 5000,
  33. })
  34. // init
  35. describe('GET image from server', (report, done) => {
  36. RNFetchBlob
  37. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`, {
  38. Authorization : 'Bearer abde123eqweje'
  39. })
  40. .then((resp) => {
  41. RNTest.prop('image', resp.base64())
  42. report(
  43. <Info key="Response image">
  44. <Image
  45. style={styles.image}
  46. source={{uri : `data:image/png;base64, ${prop('image')}`}}/>
  47. </Info>)
  48. done()
  49. })
  50. })
  51. describe('Upload octet-stream image to Dropbox', (report, done) => {
  52. let image = prop('image')
  53. let tmp = null
  54. let etag = ''
  55. // upload a file to dropbox
  56. RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  57. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  58. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  59. 'Content-Type' : 'application/octet-stream',
  60. }, image)
  61. .then((resp) => {
  62. resp = resp.json()
  63. report(
  64. <Assert key="confirm the file has been uploaded" expect={FILENAME} actual={resp.name}/>
  65. )
  66. // detect range request support
  67. return RNFetchBlob
  68. .config({
  69. path : RNFetchBlob.fs.dirs.DocumentDir + '/part1.png'
  70. })
  71. .fetch('GET', 'https://content.dropboxapi.com/1/files/auto'+'/rn-upload/'+FILENAME, {
  72. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  73. 'Cache-Control' : 'no-store',
  74. 'Range' : 'bytes=0-22975'
  75. })
  76. })
  77. .then((res) => {
  78. console.log('first chunk', res.info().headers,res.info().headers['Content-Length'])
  79. // get first range
  80. return RNFetchBlob
  81. .config({
  82. path : RNFetchBlob.fs.dirs.DocumentDir + '/part2.png'
  83. })
  84. .fetch('GET', 'https://content.dropboxapi.com/1/files/auto/rn-upload/'+FILENAME, {
  85. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  86. 'Cache-Control' : 'no-store',
  87. 'Range' : 'bytes=22976-'
  88. })
  89. })
  90. .then((res) => {
  91. tmp = res
  92. console.log('second chunk', res.info().headers, res.info().headers['Content-Length'])
  93. // get second range
  94. return RNFetchBlob.fs.appendFile(RNFetchBlob.fs.dirs.DocumentDir + '/part1.png', RNFetchBlob.fs.dirs.DocumentDir + '/part2.png', 'uri')
  95. })
  96. .then(() => {
  97. return RNFetchBlob.fs.stat(RNFetchBlob.fs.dirs.DocumentDir + '/part1.png')
  98. })
  99. .then((stat) => {
  100. tmp.flush()
  101. console.log('combined', stat)
  102. report(<Info key="combined image">
  103. <Text>{stat.size}</Text>
  104. <Image key="combined image" style={styles.image} source={{uri :RNFetchBlob.fs.dirs.DocumentDir + '/part1.png' }}/>
  105. </Info>)
  106. done()
  107. })
  108. })
  109. // require('./test-0.1.x-0.4.x')
  110. // require('./test-0.5.1')
  111. // require('./test-0.5.2')
  112. // require('./test-0.6.0')
  113. // require('./test-0.6.2')
  114. // require('./test-0.6.3')
  115. // require('./test-0.7.0')
  116. // require('./test-0.8.0')
  117. // require('./test-0.9.0')
  118. // require('./test-fetch')
  119. // require('./test-fs')
  120. // require('./test-xmlhttp')
  121. // require('./test-blob')
  122. // require('./test-firebase')
  123. // require('./test-android')
  124. // require('./test-readable')
  125. // require('./benchmark')