浏览代码

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

Ben Hsieh 9 年前
父节点
当前提交
b28e6bf30e
共有 3 个文件被更改,包括 68 次插入41 次删除
  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 查看文件

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

+ 8
- 7
test/test-0.1.x-0.4.x.js 查看文件

93
 
93
 
94
 describe('Progress report test', (report, done) => {
94
 describe('Progress report test', (report, done) => {
95
   let received = 0
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
       Authorization : 'Bearer abde123eqweje'
98
       Authorization : 'Bearer abde123eqweje'
98
     })
99
     })
99
     .progress((written, total) => {
100
     .progress((written, total) => {
100
-      report(<Info key={`progress = ${written} bytes / ${total} bytes`}/>)
101
+      // report(<Info key={`progress = ${written} bytes / ${total} bytes`}/>)
101
       if(written === total)
102
       if(written === total)
102
         report(<Assert key="progress goes to 100%" expect={written} actual={total}/>)
103
         report(<Assert key="progress goes to 100%" expect={written} actual={total}/>)
103
     })
104
     })
110
 })
111
 })
111
 
112
 
112
 // FIXME : not yet supported
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
   // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
116
   // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
116
   //   Authorization : 'Bearer abde123eqweje'
117
   //   Authorization : 'Bearer abde123eqweje'
117
   // })
118
   // })
118
   // .then((resp) => {
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 查看文件

27
       <Assert key="check properties"
27
       <Assert key="check properties"
28
         expect={dirs}
28
         expect={dirs}
29
         comparer={Comparer.hasProperties}
29
         comparer={Comparer.hasProperties}
30
-        actual={['PictureDir', 'MovieDir', 'DocumentDir', 'CacheDir']}
30
+        actual={['PictureDir', 'MovieDir', 'DocumentDir', 'CacheDir', 'MusicDir', 'DCIMDir']}
31
       />,
31
       />,
32
       <Info key="System Folders">
32
       <Info key="System Folders">
33
         <Text>{`${JSON.stringify(dirs)}`}</Text>
33
         <Text>{`${JSON.stringify(dirs)}`}</Text>
46
       fileCache : true,
46
       fileCache : true,
47
       appendExt : 'png'
47
       appendExt : 'png'
48
     })
48
     })
49
-    .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
49
+    .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
50
     .then((resp) => {
50
     .then((resp) => {
51
       tmpFilePath = resp.path()
51
       tmpFilePath = resp.path()
52
       report(<Info key={`image from ${tmpFilePath}`}>
52
       report(<Info key={`image from ${tmpFilePath}`}>
65
     data += chunk
65
     data += chunk
66
   })
66
   })
67
   stream.onEnd(() => {
67
   stream.onEnd(() => {
68
-    console.log(prop('image').length, data.length)
69
-    console.log(data)
70
     report(
68
     report(
71
       <Assert key="image should have value"
69
       <Assert key="image should have value"
72
         expect={0}
70
         expect={0}
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
+})