Nenhuma descrição

tests.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import RNTest from './in-app-test-runner'
  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.4.0-${Date.now()}.png`
  15. // paste your test config here
  16. const TEST_SERVER_URL = 'http://your-local-ip:8123'
  17. const DROPBOX_TOKEN = 'drop-box-api-token'
  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. status : 'pass',
  30. result : [
  31. <Info key="11" description="Response image">
  32. <Image key="1"
  33. style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
  34. source={{uri : `data:image/png;base64, ${image}`}}/>
  35. </Info>
  36. ]
  37. })
  38. return image
  39. })
  40. ctx.describe('Upload octet-stream image to Dropbox', async function(report) {
  41. let resp = await RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
  42. Authorization : `Bearer ${DROPBOX_TOKEN}`,
  43. 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
  44. 'Content-Type' : 'application/octet-stream',
  45. }, image)
  46. resp = resp.json()
  47. report({
  48. status : 'pass',
  49. result : [
  50. <Assert key="1" expect={FILENAME} actual={resp.name}/>
  51. ],
  52. })
  53. })
  54. ctx.describe('Upload multipart/form-data', async function(report, data) {
  55. let resp = await RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
  56. Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
  57. 'Content-Type' : 'multipart/form-data',
  58. }, [
  59. { name : 'test-img', filename : 'test-img.png', data: image},
  60. { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
  61. { name : 'field1', data : 'hello !!'},
  62. { name : 'field2', data : 'hello2 !!'}
  63. ])
  64. resp = resp.json()
  65. report({
  66. status : 'pass',
  67. result : [
  68. <Assert key="1" expect="hello !!" actual={resp.fields.field1}/>,
  69. <Assert key="2" expect="hello2 !!" actual={resp.fields.field2}/>,
  70. ],
  71. })
  72. })
  73. ctx.describe('Compare uploaded multipart image', async function(report) {
  74. // try {
  75. let resp = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`, {})
  76. let resp2 = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-text.txt`, {})
  77. console.log(resp)
  78. console.log(resp2)
  79. report({
  80. status : 'pass',
  81. result : [
  82. <Assert key="1" expect={image.length} actual={resp.base64().length}/>,
  83. <Assert key="2" expect={'hello.txt'} actual={resp2.text()}/>
  84. ]
  85. })
  86. // } catch(err) {
  87. //
  88. // report({
  89. // status : 'fail',
  90. // result :[
  91. // <Info key="a" description="Detail">
  92. // <Text>{JSON.stringify(err)}</Text>
  93. // </Info>]
  94. // })
  95. // }
  96. })
  97. export default ctx