Ingen beskrivning

tests.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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://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. export default ctx