Browse Source

#8 #3 Add test cases for upload-from-storage API

Ben Hsieh 8 years ago
parent
commit
b28e6bf30e
3 changed files with 68 additions and 41 deletions
  1. 1
    0
      test-server/.gitignore
  2. 8
    7
      test/test-0.1.x-0.4.x.js
  3. 59
    34
      test/test-0.5.x.js

+ 1
- 0
test-server/.gitignore View File

@@ -1,6 +1,7 @@
1 1
 uploads/*
2 2
 public/*
3 3
 !public/github.png
4
+!public/github2.png
4 5
 !public/22mb-dummy
5 6
 !public/1mb-dummy
6 7
 !uploads/readme

+ 8
- 7
test/test-0.1.x-0.4.x.js View File

@@ -93,11 +93,12 @@ describe('Compare uploaded multipart image', (report, done) => {
93 93
 
94 94
 describe('Progress report test', (report, done) => {
95 95
   let received = 0
96
-  RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/1mb-dummy`, {
96
+  RNFetchBlob
97
+    .fetch('GET', `${TEST_SERVER_URL}/public/1mb-dummy`, {
97 98
       Authorization : 'Bearer abde123eqweje'
98 99
     })
99 100
     .progress((written, total) => {
100
-      report(<Info key={`progress = ${written} bytes / ${total} bytes`}/>)
101
+      // report(<Info key={`progress = ${written} bytes / ${total} bytes`}/>)
101 102
       if(written === total)
102 103
         report(<Assert key="progress goes to 100%" expect={written} actual={total}/>)
103 104
     })
@@ -110,14 +111,14 @@ describe('Progress report test', (report, done) => {
110 111
 })
111 112
 
112 113
 // FIXME : not yet supported
113
-describe('Large file download test', (report, done) => {
114
-  let received = 0
114
+// describe('Large file download test', (report, done) => {
115
+  // let received = 0
115 116
   // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
116 117
   //   Authorization : 'Bearer abde123eqweje'
117 118
   // })
118 119
   // .then((resp) => {
119
-    report(<Assert key="not supported" expect={true} actual={false}/>)
120
-    done()
120
+    // report(<Assert key="not supported" expect={true} actual={false}/>)
121
+    // done()
121 122
   // })
122 123
 
123
-})
124
+// })

+ 59
- 34
test/test-0.5.x.js View File

@@ -27,7 +27,7 @@ describe('Get storage folders', (report, done) => {
27 27
       <Assert key="check properties"
28 28
         expect={dirs}
29 29
         comparer={Comparer.hasProperties}
30
-        actual={['PictureDir', 'MovieDir', 'DocumentDir', 'CacheDir']}
30
+        actual={['PictureDir', 'MovieDir', 'DocumentDir', 'CacheDir', 'MusicDir', 'DCIMDir']}
31 31
       />,
32 32
       <Info key="System Folders">
33 33
         <Text>{`${JSON.stringify(dirs)}`}</Text>
@@ -46,7 +46,7 @@ describe('Download file to storage with custom file extension', (report, done) =
46 46
       fileCache : true,
47 47
       appendExt : 'png'
48 48
     })
49
-    .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
49
+    .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
50 50
     .then((resp) => {
51 51
       tmpFilePath = resp.path()
52 52
       report(<Info key={`image from ${tmpFilePath}`}>
@@ -65,8 +65,6 @@ describe('Read cached file via file stream', (report, done) => {
65 65
     data += chunk
66 66
   })
67 67
   stream.onEnd(() => {
68
-    console.log(prop('image').length, data.length)
69
-    console.log(data)
70 68
     report(
71 69
       <Assert key="image should have value"
72 70
         expect={0}
@@ -95,33 +93,60 @@ describe('File stream reader error should be able to handled', (report, done) =>
95 93
   })
96 94
 })
97 95
 
98
-//
99
-// describe('Upload from file storage', (report, done) => {
100
-//   let filename = ''
101
-//   let filepath = ''
102
-//   RNFetchBlob.getSystemDirs().then((dirs) => {
103
-//     filename = 'ios.5.0-' + Date.now() + '-from-storage.png'
104
-//     filepath = dirs.DocumentDir + '/' + filename
105
-//     return RNFetchBlob.config({ path : filepath })
106
-//                       .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
107
-//   })
108
-//   .then((resp) => {
109
-//       let path = resp.path()
110
-//       return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
111
-//         Authorization : `Bearer ${DROPBOX_TOKEN}`,
112
-//         'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
113
-//         'Content-Type' : 'application/octet-stream',
114
-//       }, 'RNFetchBlob-file://' + path)
115
-//       .then((resp) => {
116
-//         console.log(resp.text())
117
-//         resp = resp.json()
118
-//         report(
119
-//           <Assert key="confirm the file has been uploaded" expect={filename} actual={resp.name}/>
120
-//         )
121
-//         done()
122
-//       })
123
-//   })
124
-//
125
-//
126
-//
127
-// })
96
+let localFile = null
97
+
98
+describe('Upload from file storage', (report, done) => {
99
+  let filename = ''
100
+  let filepath = ''
101
+  RNFetchBlob.getSystemDirs().then((dirs) => {
102
+    filename = Platform.OS + '0.5.0-' + Date.now() + '-from-storage.png'
103
+    filepath = dirs.DocumentDir + '/' + filename
104
+    return RNFetchBlob.config({ path : filepath })
105
+                      .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
106
+  })
107
+  .then((resp) => {
108
+      let path = resp.path()
109
+      localFile = path
110
+      return RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
111
+        Authorization : `Bearer ${DROPBOX_TOKEN}`,
112
+        'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+filename+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
113
+        'Content-Type' : 'application/octet-stream',
114
+      }, 'RNFetchBlob-file://' + path)
115
+      .then((resp) => {
116
+        resp = resp.json()
117
+        report(
118
+          <Assert key="confirm the file has been uploaded" expect={filename} actual={resp.name}/>
119
+        )
120
+        done()
121
+      })
122
+  })
123
+})
124
+
125
+describe('Upload multipart data with file from storage', (report, done) => {
126
+    let filename = 'test-from-storage-img-'+Date.now()+'.png'
127
+    RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
128
+        'Content-Type' : 'multipart/form-data',
129
+      }, [
130
+        { name : 'test-img', filename : filename, data: 'RNFetchBlob-file://' + localFile},
131
+        { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
132
+        { name : 'field1', data : 'hello !!'},
133
+        { name : 'field2', data : 'hello2 !!'}
134
+      ])
135
+    .then((resp) => {
136
+      resp = resp.json()
137
+      report(
138
+        <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
139
+        <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
140
+      )
141
+      return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/${filename}`)
142
+    })
143
+    .then((resp) => {
144
+      report(<Info key="uploaded image">
145
+        <Image
146
+          style={styles.image}
147
+          source={{ uri : 'data:image/png;base64, '+ resp.base64()}}/>
148
+      </Info>)
149
+      done()
150
+    })
151
+
152
+})