Browse Source

#2 Add test cases for systemDir and download to file API

Ben Hsieh 8 years ago
parent
commit
9f4ccbcc9c
2 changed files with 130 additions and 81 deletions
  1. 1
    1
      test-server/server.js
  2. 129
    80
      test/tests.js

+ 1
- 1
test-server/server.js View File

@@ -70,7 +70,7 @@ app.listen(8123, function(err){
70 70
 
71 71
 app.use(function(req,res,next){
72 72
 
73
-  console.log(req.headers)
73
+  console.log(chalk.green('request url=') + chalk.magenta(req.url));
74 74
 
75 75
   next()
76 76
 })

+ 129
- 80
test/tests.js View File

@@ -20,90 +20,116 @@ const DROPBOX_TOKEN = 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12h
20 20
 
21 21
 const ctx = new RNTest.TestContext()
22 22
 const Assert = RNTest.Assert
23
+const Comparer = RNTest.Comparer
23 24
 const Info = RNTest.Info
24 25
 
25 26
 let image = null
26 27
 
27
-ctx.describe('GET image from server', async function(report) {
28
-  let resp = await RNFetchBlob
28
+const styles = StyleSheet.create({
29
+  image : {
30
+    width:Dimensions.get('window').width*0.9,
31
+    height : Dimensions.get('window').width*0.9,
32
+    margin :16
33
+  }
34
+})
35
+
36
+ctx.describe('GET image from server', (report, done) => {
37
+  RNFetchBlob
29 38
     .fetch('GET', `${TEST_SERVER_URL}/public/github.png`, {
30 39
       Authorization : 'Bearer abde123eqweje'
31 40
     })
41
+    .then((resp) => {
42
+      image = resp.base64()
43
+      report(
44
+        <Info key="Response image">
45
+          <Image
46
+            style={styles.image}
47
+            source={{uri : `data:image/png;base64, ${image}`}}/>
48
+        </Info>)
49
+        done()
50
+    })
32 51
 
33
-  image = resp.base64()
34
-  report(
35
-      <Info key="Response image">
36
-        <Image
37
-          style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
38
-          source={{uri : `data:image/png;base64, ${image}`}}/>
39
-      </Info>)
40
-
41
-})
42
-
43
-ctx.describe('The check if it follows 301/302 redirection', async function(report) {
44
-
45
-  let resp = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
46
-  report(
47
-    <Assert key="check image size" expect={image.length} actual={resp.base64().length}/>,
48
-      <Info key="Response image">
49
-        <Image
50
-          style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
51
-          source={{uri : `data:image/png;base64, ${image}`}}/>
52
-      </Info>)
53
-
54
-})
55
-
56
-ctx.describe('Upload octet-stream image to Dropbox', async function(report) {
57
-
58
-  let resp = await RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
59
-    Authorization : `Bearer ${DROPBOX_TOKEN}`,
60
-    'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
61
-    'Content-Type' : 'application/octet-stream',
62
-  }, image)
63
-  resp = resp.json()
64
-  report(
65
-      <Assert key="confirm the file has been uploaded" expect={FILENAME} actual={resp.name}/>
66
-  )
67
-
68
-})
69
-
70
-ctx.describe('Upload multipart/form-data', async function(report, data) {
71
-
72
-  let resp = await RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
73
-      Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
74
-      'Content-Type' : 'multipart/form-data',
75
-    }, [
76
-      { name : 'test-img', filename : 'test-img.png', data: image},
77
-      { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
78
-      { name : 'field1', data : 'hello !!'},
79
-      { name : 'field2', data : 'hello2 !!'}
80
-    ])
81
-
82
-  resp = resp.json()
83
-
84
-  report(
85
-      <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
86
-      <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
87
-  )
88 52
 
89 53
 })
90
-
91
-ctx.describe('Compare uploaded multipart image', async function(report) {
92
-  let resp = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
93
-  let resp2 = await RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-text.txt`)
94
-
95
-  report(
96
-      <Assert key="check file length" expect={image.length} actual={resp.base64().length}/>,
97
-      <Assert key="check file content" expect={'hello.txt'} actual={resp2.text()}/>
98
-  )
54
+//
55
+// ctx.describe('The check if it follows 301/302 redirection', (report, done) => {
56
+//
57
+//   RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/redirect`)
58
+//   .then((resp) => {
59
+//     report(
60
+//       <Assert key="check image size" expect={image.length} actual={resp.base64().length}/>,
61
+//       <Info key="Response image">
62
+//         <Image
63
+//           style={{width:Dimensions.get('window').width*0.9, height : Dimensions.get('window').width*0.9,margin :16}}
64
+//           source={{uri : `data:image/png;base64, ${image}`}}/>
65
+//       </Info>)
66
+//       done()
67
+//   })
68
+//
69
+// })
70
+//
71
+// ctx.describe('Upload octet-stream image to Dropbox', (report, done) => {
72
+//
73
+//   RNFetchBlob.fetch('POST', 'https://content.dropboxapi.com/2/files/upload', {
74
+//     Authorization : `Bearer ${DROPBOX_TOKEN}`,
75
+//     'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+FILENAME+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
76
+//     'Content-Type' : 'application/octet-stream',
77
+//   }, image)
78
+//   .then((resp) => {
79
+//     resp = resp.json()
80
+//     report(
81
+//       <Assert key="confirm the file has been uploaded" expect={FILENAME} actual={resp.name}/>
82
+//     )
83
+//     done()
84
+//   })
85
+//
86
+// })
87
+//
88
+// ctx.describe('Upload multipart/form-data', (report, done) => {
89
+//
90
+//   RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
91
+//       Authorization : "Bearer fsXcpmKPrHgAAAAAAAAAEGxFXwhejXM_E8fznZoXPhHbhbNhA-Lytbe6etp1Jznz",
92
+//       'Content-Type' : 'multipart/form-data',
93
+//     }, [
94
+//       { name : 'test-img', filename : 'test-img.png', data: image},
95
+//       { name : 'test-text', filename : 'test-text.txt', data: RNFetchBlob.base64.encode('hello.txt')},
96
+//       { name : 'field1', data : 'hello !!'},
97
+//       { name : 'field2', data : 'hello2 !!'}
98
+//     ])
99
+//   .then((resp) => {
100
+//     resp = resp.json()
101
+//     report(
102
+//       <Assert key="check posted form data #1" expect="hello !!" actual={resp.fields.field1}/>,
103
+//       <Assert key="check posted form data #2" expect="hello2 !!" actual={resp.fields.field2}/>,
104
+//     )
105
+//     done()
106
+//   })
107
+//
108
+//
109
+// })
110
+
111
+ctx.describe('Compare uploaded multipart image', (report, done) => {
112
+  let r1 = null
113
+  RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-img.png`)
114
+    .then((resp) => {
115
+      r1 = resp
116
+      return RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/test-text.txt`)
117
+    })
118
+    .then((resp) => {
119
+      report(
120
+        <Assert key="check file length" expect={image.length} actual={r1.base64().length}/>,
121
+        <Assert key="check file content" expect={'hello.txt'} actual={resp.text()}/>
122
+      )
123
+      done()
124
+    })
99 125
 
100 126
 })
101 127
 
102 128
 // added after 0.4.2
103 129
 
104
-ctx.describe('Progress report test', (report) => new Promise((resolve) => {
130
+ctx.describe('Progress report test', (report, done) => {
105 131
   let received = 0
106
-  let p1 = RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/1mb-dummy`, {
132
+  RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/1mb-dummy`, {
107 133
       Authorization : 'Bearer abde123eqweje'
108 134
     })
109 135
     .progress((written, total) => {
@@ -114,26 +140,49 @@ ctx.describe('Progress report test', (report) => new Promise((resolve) => {
114 140
     .then((resp) => {
115 141
       report(<Assert key="response data should be correct event with progress listener"
116 142
         expect={resp.text().substr(0,10)} actual={"1234567890"}/>)
117
-      resolve()
143
+      done()
118 144
     })
119 145
 
120
-}))
146
+})
121 147
 
122 148
 // FIXME : not yet supported
123
-ctx.describe('Large file download test', (report) => new Promise((resolve) => {
149
+ctx.describe('Large file download test', (report, done) => {
124 150
   let received = 0
125
-  let p1 = RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
126
-      Authorization : 'Bearer abde123eqweje'
127
-    })
128
-    .then((resp) => {
129
-      report(<Assert key="not supported"
130
-        expect={true} actual={false}/>)
131
-      resolve()
132
-    })
151
+  // RNFetchBlob.fetch('GET', `${TEST_SERVER_URL}/public/22mb-dummy`, {
152
+  //   Authorization : 'Bearer abde123eqweje'
153
+  // })
154
+  // .then((resp) => {
155
+    report(<Assert key="not supported" expect={true} actual={false}/>)
156
+    done()
157
+  // })
133 158
 
134
-}))
159
+})
135 160
 
136 161
 // added after 0.5.0
137 162
 
163
+ctx.describe('Get storage folders', (report, done) => {
164
+
165
+  RNFetchBlob.getSystemDirs().then((dirs) => {
166
+    report(
167
+      <Assert key="system folders should exists" expect={dirs} comparer={Comparer.exists} />,
168
+      <Info key="System Folders">
169
+        <Text>{`${JSON.stringify(dirs)}`}</Text>
170
+      </Info>
171
+    )
172
+  })
173
+
174
+})
175
+
176
+ctx.describe('Download file to storage', (report, done) => {
177
+
178
+  RNFetchBlob.config({fileCache : true})
179
+    .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
180
+    .then((resp) => {
181
+      report(<Info key={`image from ${resp.path()}`}>
182
+        <Image ssource={{ uri : 'file://' + resp.path()}} style={styles.image}/>
183
+      </Info>)
184
+      done()
185
+    })
186
+})
138 187
 
139 188
 export default ctx