No Description

test-0.1.x-0.4.x.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. Image,
  11. } from 'react-native';
  12. const { Assert, Comparer, Info, describe, prop } = RNTest
  13. let { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles, image } = prop()
  14. describe('The check if it follows 301/302 redirection', (report, done) => {
  15. image = RNTest.prop('image')
  16. RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
  17. .then((resp) => {
  18. report(
  19. <Assert key="check image size" expect={image.length} actual={resp.base64().length}/>,
  20. <Info key="Response image">
  21. <Image
  22. style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
  23. source={{uri : `data:image/png;base64, ${image}`}}/>
  24. </Info>)
  25. done()
  26. })
  27. })
  28. describe('Upload octet-stream image to Dropbox', (report, done) => {
  29. RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  30. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  31. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  32. 'Content-Type' : 'application/octet-stream',
  33. }, image)
  34. .then((resp) => {
  35. resp = resp.json()
  36. report(
  37. <Assert key="confirm the file has been uploaded" expect={FILENAME} actual={resp.name}/>
  38. )
  39. done()
  40. })
  41. })
  42. describe('Upload multipart/form-data', (report, done) => {
  43. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  44. Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
  45. 'Content-Type' : 'multipart/form-data',
  46. }, [
  47. { name : 'test-img', filename : 'test-img.png', data: image},
  48. { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  49. { name : 'field1', data : 'hello !!'},
  50. { name : 'field2', data : 'hello2 !!'}
  51. ])
  52. .then((resp) => {
  53. resp = resp.json()
  54. report(
  55. <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  56. <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  57. )
  58. done()
  59. })
  60. })
  61. describe('Compare uploaded multipart image', (report, done) => {
  62. let r1 = null
  63. RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
  64. .then((resp) => {
  65. r1 = resp
  66. return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-text.txt`)
  67. })
  68. .then((resp) => {
  69. report(
  70. <Assert key="check file length" expect={image.length} actual={r1.base64().length}/>,
  71. <Assert key="check file content" expect={'hello.txt'} actual={resp.text()}/>
  72. )
  73. done()
  74. })
  75. })
  76. // added after 0.4.2
  77. describe('Progress report test', (report, done) => {
  78. let received = 0
  79. RNFetchBlob
  80. .fetch('GET', `${TEST_SERVER_URL}/public/1mb-dummy`, {
  81. Authorization : 'Bearer abde123eqweje'
  82. })
  83. .progress((written, total) => {
  84. // report(<Info key={`progress = ${written} bytes / ${total} bytes`}/>)
  85. if(written === total)
  86. report(<Assert key="progress goes to 100%" expect={written} actual={total}/>)
  87. })
  88. .then((resp) => {
  89. report(<Assert key="response data should be correct event with progress listener"
  90. expect={resp.text().substr(0,10)} actual={"1234567890"}/>)
  91. done()
  92. })
  93. })
  94. // FIXME : not yet supported
  95. // describe('Large file download test', (report, done) => {
  96. // let received = 0
  97. // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
  98. // Authorization : 'Bearer abde123eqweje'
  99. // })
  100. // .then((resp) => {
  101. // report(<Assert key="not supported" expect={true} actual={false}/>)
  102. // done()
  103. // })
  104. // })