Nenhuma descrição

test-0.5.2.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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=中文`),
  55. {
  56. 'Content-Type' : 'image/png;BASE64'
  57. }, RNFetchBlob.base64.encode('123'))
  58. .then((resp) => {
  59. let file = resp.path()
  60. return RNFetchBlob.fs.readStream(resp.path(), 'utf8')
  61. })
  62. .then((stream) => {
  63. let result = ''
  64. stream.open()
  65. stream.onData((chunk) => {
  66. result += chunk
  67. })
  68. stream.onEnd(() => {
  69. console.log(result)
  70. result = JSON.parse(result)
  71. report(<Assert key="param#1 should correct"
  72. expect={parseInt(time)}
  73. actual={parseInt(result.time)}/>,
  74. <Assert key="param#2 should correct"
  75. expect={'RNFetchBlobParams'}
  76. actual={result.name}/>,
  77. <Assert key="param contains unicode data should correct"
  78. expect={'中文'}
  79. actual={result.lang}/>)
  80. done()
  81. })
  82. })
  83. })