import RNTest from './react-native-testkit/' import React from 'react' import RNFetchBlob from 'react-native-fetch-blob' import { StyleSheet, Text, View, ScrollView, Platform, Dimensions, Image, } from 'react-native'; const fs = RNFetchBlob.fs const { Assert, Comparer, Info, prop } = RNTest const describe = RNTest.config({ group : '0.5.1', run : true, expand : false, }) const { TEST_SERVER_URL, FILENAME, DROPBOX_TOKEN, styles } = prop() let prefix = ((Platform.OS === 'android') ? 'file://' : '') // added after 0.5.0 let tmpFilePath = null describe('Download file to storage with custom file extension', (report, done) => { RNFetchBlob.config({ fileCache : true, appendExt : 'png' }) .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`) .then((resp) => { console.log(resp.path()) tmpFilePath = resp.path() report( ) done() }) }) describe('Read cached file via file stream', (report, done) => { let data = 'data:image/png;base64, ' fs.readStream(tmpFilePath, 'base64') .then((stream) => { stream.open() stream.onData((chunk) => { data += chunk }) stream.onEnd(() => { report( , ) done() }) stream.onError((err) => { console.log('stream err', err) }) }) }) describe('File stream reader error should be able to handled', (report, done) => { fs.readStream('^_^ not exists', 'base64') .then((stream) => { stream.open() stream.onError((err) => { report( {err} ) done() }) }) }) let localFile = null let sysDirs = RNFetchBlob.fs.dirs let dirs = RNFetchBlob.fs.dirs describe('Upload from file storage', (report, done) => { let filename = '' let filepath = '' filename = Platform.OS + '0.5.0-' + Date.now() + '-from-storage.png' filepath = dirs.DocumentDir + '/' + filename RNFetchBlob .config({ path : filepath }) .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`) .then((resp) => { let path = resp.path() localFile = path return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', { Authorization : `Bearer ${DROPBOX_TOKEN}`, 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}', 'Content-Type' : 'application/octet-stream', }, 'RNFetchBlob-file://' + path) .then((resp) => { resp = resp.json() report( ) done() }) }) }) describe('Upload multipart data with file from storage', (report, done) => { try{ 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-file://' + localFile}, { 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() }) } catch(err) { console.log(err) } }) describe('Upload and download at the same time', (report, done) => { let content = 'POST and PUT calls with headers and body should also work correctly' let filename = 'download-header-test-' + Date.now() let body = RNFetchBlob.base64.encode(content) RNFetchBlob .config({ fileCache : true, }) .fetch('POST', 'https://content.dropboxapi.com/2/files/upload', { Authorization : `Bearer ${DROPBOX_TOKEN}`, 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}', 'Content-Type' : 'application/octet-stream', }, body) .then((resp) => { return RNFetchBlob.fs.readStream(resp.path(), 'utf8') }) .then((stream) => { let actual = '' stream.open() stream.onData((chunk) => { actual += chunk }) stream.onEnd(() => { report( ) done() }) }) }) RNTest.config({ group : '0.5.1', run : true, expand : false, timeout : 600000, })('Upload and download large file', (report, done) => { let filename = '22mb-dummy-' + Date.now() let begin = -1 let begin2 = -1 let deb = Date.now() RNFetchBlob.config({ fileCache : true }) .fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`) .progress((now, total) => { if(begin === -1) begin = Date.now() if(Date.now() - deb < 1000) return deb = Date.now() report( {`download ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s)`} ) }) .then((res) => { try { deb = Date.now() let promise = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', { Authorization : `Bearer ${DROPBOX_TOKEN}`, 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}', 'Content-Type' : 'application/octet-stream', }, RNFetchBlob.wrap(res.path())) if(Platform.OS === 'ios') { promise.progress((now, total) => { if(Date.now() - deb < 1000) return deb = Date.now() if(begin2 === -1) begin2 = Date.now() let speed = Math.floor(now / (Date.now() - begin2)) report( {`upload ${now} / ${total} bytes (${speed} kb/s)`} {` ${Math.floor((total-now)/speed/1000)} seconds left`} ) }) } return promise } catch(err) { console.log(err) } }) .then((res) => { report() done() }) }) describe('Session create mechanism test', (report, done) => { let sessionName = 'foo-' + Date.now() testSessionName = sessionName let p1 = RNFetchBlob.config({ session : sessionName, fileCache : true }) .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`) let p2 = RNFetchBlob.config({ fileCache : true }) .fetch('GET', `${TEST_SERVER_URL}/public/github.png`) let p3 = RNFetchBlob.config({ path : sysDirs.DocumentDir + '/session-test'+Date.now()+'.png' }) .fetch('GET', `${TEST_SERVER_URL}/public/github.png`) let promises = [p1, p2, p3] Promise.all(promises).then((resp) => { let session = RNFetchBlob.session(sessionName).add(resp[1].path()) resp[2].session(sessionName) let actual = session.list() let expect = resp.map((p) => { return p.path() }) report( ) done() }) }) describe('Session API CRUD test', (report, done) => { let sessionName = 'test-session-' + Date.now() let baseDir = sysDirs.DocumentDir + '/' + sessionName fs.mkdir(sysDirs.DocumentDir + '/' + sessionName).then(() => { let promises = [0,1,2,3,4,5,6,7,8,9].map((p) => { return RNFetchBlob.config({ session : sessionName, path : baseDir + '/testfile' + p }) .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`) }) return Promise.all(promises) }) .then((resps) => { let s = RNFetchBlob.session(sessionName) report( ) let modified = [ s.list()[2], s.list()[3], s.list()[4], s.list()[5], s.list()[6], s.list()[7], s.list()[8], s.list()[9], ] let expect = [s.list()[0], s.list()[1]] s.remove(s.list()[0]) s.remove(s.list()[0]) report( ) s.dispose() .then(() => { return fs.ls(baseDir) }) .then((lsRes) => { report( { return baseDir + '/' + p })}/>) done() }) }) })