暫無描述

tests.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import RNTest from './react-native-testkit/'
  2. import React from 'react'
  3. import RNFetchBlob from 'react-native-fetch-blob'
  4. import {
  5. AppRegistry,
  6. StyleSheet,
  7. Text,
  8. View,
  9. Platform,
  10. ScrollView,
  11. Dimensions,
  12. Image,
  13. } from 'react-native';
  14. const FILENAME = `${Platform.OS}-0.5.0-${Date.now()}.png`
  15. // paste your test config here
  16. const TEST_SERVER_URL = 'http://192.168.17.193:8123'
  17. const DROPBOX_TOKEN = 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4'
  18. const ctx = new RNTest.TestContext()
  19. const Assert = RNTest.Assert
  20. const Info = RNTest.Info
  21. let image = null
  22. ctx.describe('GET image from server', async function(report) {
  23. let resp = await RNFetchBlob
  24. .fetch('GET', `${TEST_SERVER_URL}/public/github.png`, {
  25. Authorization : 'Bearer abde123eqweje'
  26. })
  27. image = resp.base64()
  28. report(
  29. <Info key="Response image">
  30. <Image
  31. style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
  32. source={{uri : `data:image/png;base64, ${image}`}}/>
  33. </Info>)
  34. })
  35. ctx.describe('The check if it follows 301/302 redirection', async function(report) {
  36. let resp = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
  37. report(
  38. <Assert key="check image size" expect={image.length} actual={resp.base64().length}/>,
  39. <Info key="Response image">
  40. <Image
  41. style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
  42. source={{uri : `data:image/png;base64, ${image}`}}/>
  43. </Info>)
  44. })
  45. ctx.describe('Upload octet-stream image to Dropbox', async function(report) {
  46. let resp = await RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  47. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  48. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  49. 'Content-Type' : 'application/octet-stream',
  50. }, image)
  51. resp = resp.json()
  52. report(
  53. <Assert key="confirm the file has been uploaded" expect={FILENAME} actual={resp.name}/>
  54. )
  55. })
  56. ctx.describe('Upload multipart/form-data', async function(report, data) {
  57. let resp = await RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  58. Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
  59. 'Content-Type' : 'multipart/form-data',
  60. }, [
  61. { name : 'test-img', filename : 'test-img.png', data: image},
  62. { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  63. { name : 'field1', data : 'hello !!'},
  64. { name : 'field2', data : 'hello2 !!'}
  65. ])
  66. resp = resp.json()
  67. report(
  68. <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
  69. <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
  70. )
  71. })
  72. ctx.describe('Compare uploaded multipart image', async function(report) {
  73. let resp = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
  74. let resp2 = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-text.txt`)
  75. report(
  76. <Assert key="check file length" expect={image.length} actual={resp.base64().length}/>,
  77. <Assert key="check file content" expect={'hello.txt'} actual={resp2.text()}/>
  78. )
  79. })
  80. // added after 0.4.2
  81. ctx.describe('Progress report test', (report) => new Promise((resolve) => {
  82. let received = 0
  83. let p1 = RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/1mb-dummy`, {
  84. Authorization : 'Bearer abde123eqweje'
  85. })
  86. .progress((written, total) => {
  87. report(<Info key={`progress = ${written} bytes / ${total} bytes`}/>)
  88. if(written === total)
  89. report(<Assert key="progress goes to 100%" expect={written} actual={total}/>)
  90. })
  91. .then((resp) => {
  92. report(<Assert key="response data should be correct event with progress listener"
  93. expect={resp.text().substr(0,10)} actual={"1234567890"}/>)
  94. resolve()
  95. })
  96. }))
  97. // FIXME : not yet supported
  98. ctx.describe('Large file download test', (report) => new Promise((resolve) => {
  99. let received = 0
  100. let p1 = RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
  101. Authorization : 'Bearer abde123eqweje'
  102. })
  103. .then((resp) => {
  104. report(<Assert key="not supported"
  105. expect={true} actual={false}/>)
  106. resolve()
  107. })
  108. }))
  109. // added after 0.5.0
  110. export default ctx