Нет описания

test-0.9.2.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
  14. window.Blob = RNFetchBlob.polyfill.Blob
  15. window.fetch = new RNFetchBlob.polyfill.Fetch({
  16. auto : true,
  17. binaryContentTypes : ['image/', 'video/', 'audio/']
  18. }).build()
  19. const fs = RNFetchBlob.fs
  20. const { Assert, Comparer, Info, prop } = RNTest
  21. const describe = RNTest.config({
  22. group : '0.9.2',
  23. run : true,
  24. expand : true,
  25. timeout : 20000,
  26. })
  27. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
  28. const dirs = RNFetchBlob.fs.dirs
  29. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  30. describe('content-length header test', (report, done) => {
  31. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/content-length`, {
  32. 'Content-Type' : 'text/plain',
  33. 'Content-Length' : '5'
  34. }, 'hello')
  35. .then((res) => {
  36. report(
  37. <Info key="response data">
  38. <Text>{res.text()}</Text>
  39. </Info>)
  40. done()
  41. })
  42. })
  43. describe('slice test', (report, done) => {
  44. let str = "PASSSTRING"
  45. let tmp = fs.dirs.DocumentDir + '/slice-tmp-'
  46. let testData = [
  47. {start: 4, contents: "STRING"},
  48. {start: 12, contents: ""},
  49. {start: 0, end: 4, contents: "PASS"},
  50. {start: 0, end: 12, contents: "PASSSTRING"},
  51. {start: 7, end: 4, contents: ""},
  52. {start: -6, contents: "STRING"},
  53. {start: -12, contents: "PASSSTRING"},
  54. {start: 0, end: -6, contents: "PASS"},
  55. {start: 0, end: -12, contents: ""},
  56. ]
  57. fs.writeFile(tmp, str, 'utf8')
  58. .then(() => {
  59. let promises = []
  60. for(let t in testData) {
  61. let p = fs.slice(tmp, tmp + t, testData[t].start, testData[t].end)
  62. .then(function(num) {
  63. console.log('slice finished', num)
  64. return fs.readFile(tmp + num, 'utf8')
  65. .then((data) => {
  66. report(<Assert key={`assertion-${num}`} expect={testData[num].contents} actual={data}/>)
  67. return Promise.resolve()
  68. })
  69. }.bind(this, t))
  70. promises.push(p)
  71. }
  72. Promise.all(promises).then((res) => {
  73. done()
  74. })
  75. })
  76. })
  77. describe('Upload multipart/form-data', (report, done) => {
  78. let image = RNTest.prop('image')
  79. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  80. Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
  81. 'Content-Type' : 'multipart/form-data',
  82. }, [
  83. { name : 'test-img', filename : 'test-img.png', data: image},
  84. { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  85. { name : 'field1', data : 'hello !!'},
  86. { name : 'field2', data : 'hello2 !!'}
  87. ])
  88. .then((resp) => {
  89. console.log(resp.json())
  90. resp = resp.json()
  91. report(
  92. <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  93. <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  94. )
  95. done()
  96. })
  97. })
  98. describe('app should not crash when sending formdata without data field', (report, done) => {
  99. RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  100. Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
  101. 'Content-Type' : 'multipart/form-data',
  102. }, [
  103. { name : 'empty-file', filename : 'test-img.png'},
  104. { name : 'empty-data'},
  105. { name : 'field2', data : 'hello2 !!'}
  106. ])
  107. .then((resp) => {
  108. console.log(resp.json())
  109. resp = resp.json()
  110. report(
  111. <Assert key="check posted form data #1" expect={undefined} actual={resp.fields['empty-file']}/>,
  112. <Assert key="check posted form data #2" expect={undefined} actual={resp.fields['empty-data']}/>,
  113. <Assert key="check posted form data #3" expect="hello2 !!" actual={resp.fields.field2}/>,
  114. )
  115. done()
  116. })
  117. })