No Description

test-fetch.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.Blob = RNFetchBlob.polyfill.Blob
  14. window.fetch = new RNFetchBlob.polyfill.Fetch({
  15. auto : true,
  16. binaryContentTypes : ['image/', 'video/', 'audio/']
  17. }).build()
  18. const fs = RNFetchBlob.fs
  19. const { Assert, Comparer, Info, prop } = RNTest
  20. const describe = RNTest.config({
  21. group : 'Fetch polyfill',
  22. run : true,
  23. expand : true,
  24. timeout : 10000,
  25. })
  26. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  27. const dirs = RNFetchBlob.fs.dirs
  28. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  29. describe('GET request test : unicode text -> any', (report, done) => {
  30. function get(fn1, fn2) {
  31. return fetch(`${TEST_SERVER_URL}/unicode`, { method : 'GET'})
  32. .then((res) => fn1(res))
  33. .then((data) => fn2(data))
  34. }
  35. let promises =
  36. [
  37. get((res) => res.json(), (json) => {
  38. report(<Assert key="json data correct" expect={'你好!'} actual={json.data}/>)
  39. }),
  40. get((res) => res.text(), (text) => {
  41. report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
  42. }),
  43. get((res) => res.blob(), (blob) => {
  44. return blob.readBlob('utf8').then((text) => {
  45. report(<Assert key="blob data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
  46. })
  47. }),
  48. // get((res) => res.arrayBuffer(), (text) => {
  49. // report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
  50. // })
  51. ]
  52. Promise.all(promises).then(() => {
  53. done()
  54. })
  55. })
  56. describe('GET request test : path -> any', (report, done) => {
  57. function get(fn1, fn2, fn3) {
  58. fetch(`${TEST_SERVER_URL}/public/github.png`, { method : 'GET'})
  59. .then((res) => fn1(res))
  60. .then((data) => fn2(data))
  61. .catch((err) => fn3(err))
  62. }
  63. let contentLength = 0
  64. let promises = [
  65. get((res) => res.json(), (data) => {
  66. report(<Assert key="should not convert blob to JSON" expect={true} actual={false} />)
  67. }, (err) => {
  68. report(<Assert key="should not convert blob to JSON" expect={true} actual={true} />)
  69. }),
  70. get((res) => {
  71. contentLength = res.headers['Content-Length']
  72. return res.text()
  73. }, (data) => {
  74. report(
  75. <Assert key="should convert blob to text" expect={true} actual={true} />,
  76. <Assert key="content length should correct" expect={Math.floor(contentLength)} actual={data.length} />)
  77. }, (err) => {
  78. console.warn(err, err.stack)
  79. report(<Assert key="should convert blob to text" expect={true} actual={false} />)
  80. }),
  81. get((res) => {
  82. contentLength = res.headers['Content-Length']
  83. return res.blob()
  84. }, (blob) => {
  85. return fs.stat(blob.getRNFetchBlobRef()).then((stat) => {
  86. report(<Assert key="stored file size correct" expect={contentLength} actual={stat.size} />)
  87. return blob.readBlob('base64')
  88. })
  89. .then((b64) => {
  90. report(<Info key="stored image">
  91. <Image style={styles.image} source={{uri : 'data:image/png;BASE64 ,' + b64}}/>
  92. </Info>)
  93. })
  94. }, (err) => {
  95. console.warn(err, err.stack)
  96. report(<Assert key="should convert blob to blob" expect={true} actual={false} />)
  97. })
  98. ]
  99. Promise.all(promises).then( () => done() )
  100. })
  101. describe('POST different types of body', (report, done) => {
  102. let image = RNTest.prop('image')
  103. let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
  104. function upload(desc, method, pBody) {
  105. let name = `fetch-replacement-${Platform.OS}-${Date.now()}.png`
  106. return pBody.then((body) =>
  107. fetch('https://content.dropboxapi.com/2/files/upload', {
  108. method : 'post',
  109. headers : {
  110. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  111. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  112. 'Content-Type' : 'application/octet-stream'
  113. }, body })
  114. )
  115. .then((res) => {
  116. return res.json()
  117. })
  118. .then((info) => {
  119. report(<Assert key={desc} expect={name} actual={info.name}/>)
  120. })
  121. }
  122. let tests = [
  123. upload('upload base64 encoded body', Promise.resolve(image)),
  124. upload('upload Blob body', Blob.build(image, 'image/png;BASE64')),
  125. upload('upload file path body', fs.writeFile(tmpPath, image, 'base64').then(() => Promise.resolve(RNFetchBlob.wrap(tmpPath))))
  126. ]
  127. Promise.all(tests).then(() => done())
  128. })
  129. describe('check HTTP body correctness', (report, done) => {
  130. let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
  131. function upload(pBody) {
  132. return pBody.then((body) =>
  133. fetch('https://content.dropboxapi.com/2/files/upload', {
  134. method : 'POST',
  135. headers : {
  136. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  137. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  138. 'Content-Type' : 'application/octet-stream'
  139. }, body })
  140. .then((res) => res.json())
  141. .then((info) => {
  142. })
  143. )
  144. }
  145. let pUnicodeBody = fetch(`${TEST_SERVER_URL}/public/utf8-dummy`, { method : 'GET' })
  146. .then((res) => res.text())
  147. let tests = [
  148. upload(pUnicodeBody)
  149. ]
  150. })