Sin descripción

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. console.log(resp.json())
  59. resp = resp.json()
  60. report(
  61. <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  62. <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  63. )
  64. done()
  65. })
  66. })
  67. describe('Compare uploaded multipart image', (report, done) => {
  68. let r1 = null
  69. RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
  70. .then((resp) => {
  71. r1 = resp
  72. return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-text.txt`)
  73. })
  74. .then((resp) => {
  75. report(
  76. <Assert key="check file length" expect={image.length} actual={r1.base64().length}/>,
  77. <Assert key="check file content" expect={'hello.txt'} actual={resp.text()}/>
  78. )
  79. done()
  80. })
  81. })
  82. // added after 0.4.2
  83. describe('Progress report test', (report, done) => {
  84. let actual = 0, expect = -1
  85. RNFetchBlob
  86. .fetch('GET', `${TEST_SERVER_URL}/public/1mb-dummy`, {
  87. Authorization : 'Bearer abde123eqweje'
  88. })
  89. .progress((received, total) => {
  90. expect = total
  91. })
  92. .then((resp) => {
  93. actual = resp.text().length
  94. report(
  95. <Assert key="download progress correct" expect={Math.floor(expect)} actual={Math.floor(actual)}/>,
  96. <Assert key="response data should be correct event with progress listener"
  97. expect={resp.text().substr(0,10)} actual={"1234567890"}/>)
  98. done()
  99. })
  100. })
  101. describe('PUT request test', (report, done) => {
  102. let actual = 0, expect = -1
  103. RNFetchBlob.fetch('PUT', `${TEST_SERVER_URL}/upload-form`, {
  104. Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
  105. 'Content-Type' : 'multipart/form-data',
  106. }, [
  107. { name : 'test-img', filename : 'test-img.png', data: image},
  108. { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  109. { name : 'field1', data : 'hello !!'},
  110. { name : 'field2', data : 'hello2 !!'}
  111. ])
  112. .uploadProgress((written, total) => {
  113. actual = written
  114. expect = total
  115. })
  116. .then((resp) => {
  117. resp = resp.json()
  118. report(
  119. <Assert key="check put form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  120. <Assert key="check put form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  121. )
  122. done()
  123. })
  124. })
  125. describe('DELETE request test', (report, done) => {
  126. RNFetchBlob.fetch('DELETE', `${TEST_SERVER_URL}/hey`)
  127. .then((resp) => {
  128. report(
  129. <Assert key="check DELETE request result" expect={'man'} actual={resp.text()}/>)
  130. done()
  131. })
  132. })