import RNTest from './react-native-testkit/' import React from 'react' import RNFetchBlob from 'react-native-fetch-blob' import { StyleSheet, Text, View, ScrollView, CameraRoll, Platform, Dimensions, Image, } from 'react-native'; const fs = RNFetchBlob.fs const { Assert, Comparer, Info, prop } = RNTest const describe = RNTest.config({ group : '0.6.2', run : true, expand : false, timeout : 30000, }) const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop() const dirs = RNFetchBlob.fs.dirs let prefix = ((Platform.OS === 'android') ? 'file://' : '') let photo = null describe('upload asset from camera roll', (report, done) => { let imgName = `image-from-camera-roll-${Platform.OS}-${Date.now()}.jpg` let tick = Date.now() let tick2 = Date.now() CameraRoll.getPhotos({first : 10}) .then((resp) => { let url = resp.edges[0].node.image.uri console.log('CameraRoll',url) photo = url return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', { Authorization : `Bearer ${DROPBOX_TOKEN}`, 'Dropbox-API-Arg': `{\"path\": \"/rn-upload/${imgName}\",\"mode\": \"add\",\"autorename\": false,\"mute\": false}`, 'Content-Type' : 'application/octet-stream', }, RNFetchBlob.wrap(url)) .uploadProgress((now, total) => { if(Date.now() - tick2 < 1000) return report( {`upload ${now} / ${total} ${Math.floor(now/total*100)}% `} ) tick2 = Date.now() }) }) .then((resp) => { console.log(resp) resp = resp.json() report( ) done() }) }) describe('Upload multipart data with file from CameraRoll', (report, done) => { let filename = 'test-from-storage-img-'+Date.now()+'.png' RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, { 'Content-Type' : 'multipart/form-data', }, [ { name : 'test-img', filename : filename, data: RNFetchBlob.wrap(photo)}, { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')}, { name : 'field1', data : 'hello !!'}, { name : 'field2', data : 'hello2 !!'} ]) .then((resp) => { resp = resp.json() report( , , ) return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/${filename}`) }) .then((resp) => { report( ) done() }) }) describe('access assets from camera roll', (report, done) => { let photo = null CameraRoll.getPhotos({first : 10}) .then((resp) => { photo = resp.edges[0].node.image.uri report( {photo} ) return fs.readFile(photo, 'base64') }) .then((data) => { report( ) done() }) }) describe('read asset in app bundle',(report, done) => { let target = fs.asset('test-asset2.png') fs.readFile(target, 'base64') .then((data) => { report( ) return fs.readFile(fs.asset('test-asset1.json'), 'utf8') }) .then((resp) => { report( ) done() }) }) describe('stat assets in app', (report, done) => { fs.stat(fs.asset('test-asset2.png')) .then((data) => { report( {JSON.stringify(data)} ) done() }) }) describe('copy asset', (report, done) => { let dest = `${dirs.DocumentDir}/test-asset-1-${Date.now()}.json` fs.cp(fs.asset('test-asset1.json'), dest) .then(() => fs.readFile(dest, 'utf8')) .then((data) => { report() return fs.stat(fs.asset('test-asset1.json')) }) .then((stat) => { report(, {JSON.stringify(stat)} ) done() }) }) describe('upload file from assets',(report, done) => { let assetName = fs.asset('test-asset1.json') RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', { Authorization : `Bearer ${DROPBOX_TOKEN}`, 'Dropbox-API-Arg': `{\"path\": \"/rn-upload/file-from-asset-${Platform.OS}.json\",\"mode\": \"add\",\"autorename\": false,\"mute\": false}`, 'Content-Type' : 'application/octet-stream', }, RNFetchBlob.wrap(assetName)) .then((resp) => { resp = resp.json() report( ) done() }) }) describe('Check custom MIME type correctness',(report, done) => { RNFetchBlob .config({fileCache : true}) .fetch('GET', `${TEST_SERVER_URL}/public/beethoven.mp3`) .then((resp) => { return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/mime`, null, [ { name : 'image', filename : 'image', type : 'image/jpeg', data : RNFetchBlob.base64.encode('123456') }, { name : 'mp3', filename : 'mp3', type : 'application/mp3', data : RNFetchBlob.base64.encode('123456') }, { name : 'mp3', filename : 'mp3', data : RNFetchBlob.base64.encode('123456') }, { name : 'asset', filename : 'asset.json', type: 'application/json', data : RNFetchBlob.wrap(fs.asset('test-asset1.json')) }, { name : 'camera-roll', filename : 'cameraRoll.png', type: 'image/png', data : RNFetchBlob.wrap(photo) }, { name : 'storage', filename : 'storage.mp3', type: 'application/mp3', data : RNFetchBlob.wrap(resp.path()) }, ]) }) .then((resp) => { resp = resp.json() report( , , , , , , ) done() }) })