Bez popisu

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

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