Geen omschrijving

test-0.5.2.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. result = JSON.parse(result)
  67. report(<Assert key="param#1 should correct"
  68. expect={parseInt(time)}
  69. actual={parseInt(result.time)}/>,
  70. <Assert key="param#2 should correct"
  71. expect={'RNFetchBlobParams'}
  72. actual={result.name}/>,
  73. <Assert key="param contains unicode data should correct"
  74. expect={'中文'}
  75. actual={result.lang}/>)
  76. done()
  77. })
  78. })
  79. })