Keine Beschreibung

test-0.5.2.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. const fs = RNFetchBlob.fs
  14. const { Assert, Comparer, Info, prop } = RNTest
  15. const describe = RNTest.config({
  16. group : '0.5.2',
  17. run : true,
  18. expand : false,
  19. })
  20. const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  21. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  22. describe('GET request with params', (report, done) => {
  23. let time = Date.now()
  24. RNFetchBlob.config({ fileCache : true })
  25. .fetch('GET', encodeURI(`${TEST_SERVER_URL}/params?time=${time}&name=RNFetchBlobParams&lang=中文`))
  26. .then((resp) => {
  27. let file = resp.path()
  28. return RNFetchBlob.fs.readStream(resp.path(), 'utf8')
  29. })
  30. .then((stream) => {
  31. let result = ''
  32. stream.open()
  33. stream.onData((chunk) => {
  34. result += chunk
  35. })
  36. stream.onEnd(() => {
  37. result = JSON.parse(result)
  38. report(<Assert key="param#1 should correct"
  39. expect={parseInt(time)}
  40. actual={parseInt(result.time)}/>,
  41. <Assert key="param#2 should correct"
  42. expect={'RNFetchBlobParams'}
  43. actual={result.name}/>,
  44. <Assert key="param contains unicode data should correct"
  45. expect={'中文'}
  46. actual={result.lang}/>)
  47. done()
  48. })
  49. })
  50. })
  51. describe('POST request with params', (report, done) => {
  52. let time = Date.now()
  53. RNFetchBlob.config({ fileCache : true })
  54. .fetch('POST', encodeURI(`${TEST_SERVER_URL}/params?time=${time}&name=RNFetchBlobParams&lang=中文`), {}, RNFetchBlob.base64.encode('123'))
  55. .then((resp) => {
  56. let file = resp.path()
  57. return RNFetchBlob.fs.readStream(resp.path(), 'utf8')
  58. })
  59. .then((stream) => {
  60. let result = ''
  61. stream.open()
  62. stream.onData((chunk) => {
  63. result += chunk
  64. })
  65. stream.onEnd(() => {
  66. console.log(result)
  67. result = JSON.parse(result)
  68. report(<Assert key="param#1 should correct"
  69. expect={parseInt(time)}
  70. actual={parseInt(result.time)}/>,
  71. <Assert key="param#2 should correct"
  72. expect={'RNFetchBlobParams'}
  73. actual={result.name}/>,
  74. <Assert key="param contains unicode data should correct"
  75. expect={'中文'}
  76. actual={result.lang}/>)
  77. done()
  78. })
  79. })
  80. })