|
@@ -0,0 +1,83 @@
|
|
1
|
+import RNTest from './react-native-testkit/'
|
|
2
|
+import React from 'react'
|
|
3
|
+import RNFetchBlob from 'react-native-fetch-blob'
|
|
4
|
+import Timer from 'react-timer-mixin'
|
|
5
|
+
|
|
6
|
+import {
|
|
7
|
+ StyleSheet,
|
|
8
|
+ Text,
|
|
9
|
+ View,
|
|
10
|
+ ScrollView,
|
|
11
|
+ CameraRoll,
|
|
12
|
+ Platform,
|
|
13
|
+ Dimensions,
|
|
14
|
+ Image,
|
|
15
|
+} from 'react-native';
|
|
16
|
+
|
|
17
|
+const fs = RNFetchBlob.fs
|
|
18
|
+const { Assert, Comparer, Info, prop } = RNTest
|
|
19
|
+const describe = RNTest.config({
|
|
20
|
+ group : '0.7.0',
|
|
21
|
+ run : true,
|
|
22
|
+ expand : true,
|
|
23
|
+ timeout : 300000000,
|
|
24
|
+})
|
|
25
|
+const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
|
|
26
|
+const dirs = RNFetchBlob.fs.dirs
|
|
27
|
+
|
|
28
|
+let prefix = ((Platform.OS === 'android') ? 'file://' : '')
|
|
29
|
+
|
|
30
|
+describe('Upload and download large file', (report, done) => {
|
|
31
|
+ let filename = '22mb-dummy-' + Date.now()
|
|
32
|
+ let begin = -1
|
|
33
|
+ let begin2 = -1
|
|
34
|
+ let deb = Date.now()
|
|
35
|
+ RNFetchBlob.config({
|
|
36
|
+ fileCache : true
|
|
37
|
+ })
|
|
38
|
+ .fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`)
|
|
39
|
+ .progress((now, total) => {
|
|
40
|
+ if(begin === -1)
|
|
41
|
+ begin = Date.now()
|
|
42
|
+ if(Date.now() - deb < 1000)
|
|
43
|
+ return
|
|
44
|
+ deb = Date.now()
|
|
45
|
+ report(<Info uid="200" key="progress">
|
|
46
|
+ <Text>
|
|
47
|
+ {`download ${now} / ${total} bytes (${Math.floor(now / (Date.now() - begin))} kb/s)`}
|
|
48
|
+ </Text>
|
|
49
|
+ </Info>)
|
|
50
|
+ })
|
|
51
|
+ .then((res) => {
|
|
52
|
+ try {
|
|
53
|
+ deb = Date.now()
|
|
54
|
+ let promise = RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
|
|
55
|
+ Authorization : `Bearer ${DROPBOX_TOKEN}`,
|
|
56
|
+ 'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
|
|
57
|
+ 'Content-Type' : 'application/octet-stream',
|
|
58
|
+ }, RNFetchBlob.wrap(res.path()))
|
|
59
|
+ promise.uploadProgress((now, total) => {
|
|
60
|
+ if(Date.now() - deb < 1000)
|
|
61
|
+ return
|
|
62
|
+ deb = Date.now()
|
|
63
|
+ if(begin2 === -1)
|
|
64
|
+ begin2 = Date.now()
|
|
65
|
+ let speed = Math.floor(now / (Date.now() - begin2))
|
|
66
|
+ report(<Info uid="100" key="progress">
|
|
67
|
+ <Text>
|
|
68
|
+ {`upload ${now} / ${total} bytes (${speed} kb/s)`}
|
|
69
|
+ {` ${Math.floor((total-now)/speed/1000)} seconds left`}
|
|
70
|
+ </Text>
|
|
71
|
+ </Info>)
|
|
72
|
+ })
|
|
73
|
+ return promise
|
|
74
|
+ } catch(err) { console.log(err) }
|
|
75
|
+ })
|
|
76
|
+ .then((res) => {
|
|
77
|
+ report(<Assert
|
|
78
|
+ key="upload should success without crashing app"
|
|
79
|
+ expect={filename}
|
|
80
|
+ actual={res.json().name}/>)
|
|
81
|
+ done()
|
|
82
|
+ })
|
|
83
|
+})
|