Ver código fonte

Change test cases

Ben Hsieh 8 anos atrás
pai
commit
b9fe58131e
4 arquivos alterados com 247 adições e 243 exclusões
  1. 1
    0
      test/test-android.js
  2. 156
    153
      test/test-fetch.js
  3. 86
    86
      test/test-firebase.js
  4. 4
    4
      test/test-init.js

+ 1
- 0
test/test-android.js Ver arquivo

@@ -196,3 +196,4 @@ describe('download file to specific location using DownloadManager', (report, do
196 196
         expect="23975" actual={stat.size}/>)
197 197
     done()
198 198
   })
199
+})

+ 156
- 153
test/test-fetch.js Ver arquivo

@@ -24,7 +24,7 @@ const { Assert, Comparer, Info, prop } = RNTest
24 24
 const describe = RNTest.config({
25 25
   group : 'Fetch polyfill',
26 26
   run : true,
27
-  expand : false,
27
+  expand : true,
28 28
   timeout : 10000,
29 29
 })
30 30
 const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
@@ -32,158 +32,161 @@ const dirs = RNFetchBlob.fs.dirs
32 32
 
33 33
 let prefix = ((Platform.OS === 'android') ? 'file://' : '')
34 34
 
35
-describe('GET request test : unicode text -> any', (report, done) => {
36
-
37
-  function get(fn1, fn2) {
38
-    return fetch(`${TEST_SERVER_URL}/unicode`, { method : 'GET'})
39
-    .then((res) => fn1(res))
40
-    .then((data) => fn2(data))
41
-  }
42
-
43
-  let promises =
44
-  [
45
-    get((res) => res.json(), (json) => {
46
-      report(<Assert key="json data correct" expect={'你好!'} actual={json.data}/>)
47
-    }),
48
-    get((res) => res.text(), (text) => {
49
-      report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
50
-    }),
51
-    get((res) => res.blob(), (blob) => {
52
-      return blob.readBlob('utf8').then((text) => {
53
-        report(<Assert key="blob data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
54
-      })
55
-    }),
56
-    // get((res) => res.arrayBuffer(), (text) => {
57
-    //   report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
58
-    // })
59
-  ]
60
-
61
-  Promise.all(promises).then(() => {
62
-    done()
63
-  })
64
-
65
-})
66
-
67
-describe('GET request test : path -> any', (report, done) => {
68
-
69
-  function get(fn1, fn2, fn3) {
70
-    fetch(`${TEST_SERVER_URL}/public/github.png`, { method : 'GET'})
71
-      .then((res) => fn1(res))
72
-      .then((data) => fn2(data))
73
-      .catch((err) => fn3(err))
74
-  }
75
-  let contentLength = 0
76
-  let isIOS = Platform.OS === 'ios'
77
-  let promises = [
78
-    // FIXME: IOS only
79
-    // https://github.com/facebook/react-native/issues/9178
80
-    get((res) => {
81
-      if(isIOS)
82
-        return res.json()
83
-      return Promise.resolve()
84
-    }, (data) => {
85
-      report(<Assert key="should not convert blob to JSON (IOS only)" expect={true} actual={false} />)
86
-    }, (err) => {
87
-      report(<Assert key="should not convert blob to JSON (IOS only)" expect={true} actual={true} />)
88
-    }),
89
-    // FIXME: IOS only
90
-    // https://github.com/facebook/react-native/issues/9178
91
-    get((res) => {
92
-      contentLength = res.headers['Content-Length']
93
-      if(isIOS)
94
-        return res.text()
95
-      return Promise.resolve()
96
-
97
-    }, (data) => {
98
-      try {
99
-        report(<Assert key="content length should correct (IOS only)" expect={Math.floor(contentLength)} actual={data ? data.length : 0} />)
100
-      } catch(e){}
101
-    }, (err) => {
102
-      console.warn(err, err.stack)
103
-    }),
104
-    get((res) => {
105
-      contentLength = res.headers['Content-Length']
106
-      return res.blob()
107
-    }, (blob) => {
108
-      return fs.stat(blob._ref).then((stat) => {
109
-        report(<Assert key="stored file size correct" expect={contentLength} actual={stat.size} />)
110
-        return blob.readBlob('base64')
111
-      })
112
-      .then((b64) => {
113
-        report(<Info key="stored image">
114
-          <Image style={styles.image} source={{uri : 'data:image/png;BASE64 ,' + b64}}/>
115
-        </Info>)
116
-      })
117
-
118
-    }, (err) => {
119
-      console.warn(err, err.stack)
120
-    })
121
-  ]
122
-  Promise.all(promises).then( () => done() )
123
-
124
-})
125
-
126
-describe('POST different kinds of body', (report, done) => {
127
-
128
-  let image = RNTest.prop('image')
129
-  let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
130
-
131
-  function upload(desc, pBody) {
132
-    let name = `fetch-replacement-${Platform.OS}-${Date.now()}-${Math.random()}.png`
133
-    return pBody.then((body) =>
134
-      fetch('https://content.dropboxapi.com/2/files/upload', {
135
-        method : 'post',
136
-        headers : {
137
-          Authorization : `Bearer ${DROPBOX_TOKEN}`,
138
-          'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
139
-          'Content-Type' : 'application/octet-stream'
140
-        },
141
-        body})
142
-    )
143
-    .then((res) => {
144
-      return res.json()
145
-    })
146
-    .then((info) => {
147
-      report(<Assert key={desc} expect={name} actual={info.name}/>)
148
-    })
149
-  }
150
-
151
-  fetch(`${TEST_SERVER_URL}/public/github2.jpg`)
152
-    .then((res) => res.blob())
153
-    .then((b) => b.readBlob('base64'))
154
-    .then((image) => {
155
-      let tests = [
156
-        upload('upload base64 encoded body', Promise.resolve(image)),
157
-        upload('upload Blob body', Blob.build(image, 'image/png;BASE64')),
158
-        upload('upload file path body', fs.writeFile(tmpPath, image, 'base64').then(() => Promise.resolve(RNFetchBlob.wrap(tmpPath))))
159
-      ]
160
-      Promise.all(tests).then(() => done())
161
-    })
162
-
163
-})
164
-
165
-describe('Request header correctness', (report, done) => {
166
-
167
-  let expect = {
168
-    'hello' : 'world',
169
-    'Content-Type' : 'application/json',
170
-    'foo' : encodeURIComponent('福' + Date.now())
171
-  }
172
-
173
-  fetch(`${TEST_SERVER_URL}/xhr-header`, {
174
-    method : 'GET',
175
-    headers : expect
176
-  })
177
-  .then((res) => res.json())
178
-  .then((actual) => {
179
-    report(<Info key={JSON.stringify(actual)}/>)
180
-    report(<Assert key="header field test #1" expect={expect.hello} actual={actual.hello}/>)
181
-    report(<Assert key="header field test #2" expect={expect['content-type']} actual={actual['content-type']}/>)
182
-    report(<Assert key="header field test #3" expect={expect.foo} actual={actual.foo}/>)
183
-    done()
184
-  })
185
-
186
-})
35
+// describe('GET request test : unicode text -> any', (report, done) => {
36
+//
37
+//   function get(fn1, fn2) {
38
+//     return fetch(`${TEST_SERVER_URL}/unicode`, { method : 'GET'})
39
+//     .then((res) => fn1(res))
40
+//     .then((data) => fn2(data))
41
+//   }
42
+//
43
+//   let promises =
44
+//   [
45
+//     get((res) => res.json(), (json) => {
46
+//       report(<Assert key="json data correct" expect={'你好!'} actual={json.data}/>)
47
+//     }),
48
+//     get((res) => res.text(), (text) => {
49
+//       report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
50
+//     }),
51
+//     get((res) => res.blob(), (blob) => {
52
+//       return blob.readBlob('utf8').then((text) => {
53
+//         report(<Assert key="blob data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
54
+//       })
55
+//     }),
56
+//     // get((res) => res.arrayBuffer(), (text) => {
57
+//     //   report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
58
+//     // })
59
+//   ]
60
+//
61
+//   Promise.all(promises).then(() => {
62
+//     done()
63
+//   })
64
+//
65
+// })
66
+//
67
+// describe('GET request test : path -> any', (report, done) => {
68
+//
69
+//   function get(fn1, fn2, fn3) {
70
+//     fetch(`${TEST_SERVER_URL}/public/github.png`, { method : 'GET'})
71
+//       .then((res) => fn1(res))
72
+//       .then((data) => fn2(data))
73
+//       .catch((err) => fn3(err))
74
+//   }
75
+//   let contentLength = 0
76
+//   let isIOS = Platform.OS === 'ios'
77
+//   let promises = [
78
+//     // FIXME: IOS only
79
+//     // https://github.com/facebook/react-native/issues/9178
80
+//     get((res) => {
81
+//       if(isIOS)
82
+//         return res.json()
83
+//       return Promise.resolve()
84
+//     }, (data) => {
85
+//       report(<Assert key="should not convert blob to JSON (IOS only)" expect={true} actual={false} />)
86
+//     }, (err) => {
87
+//       report(<Assert key="should not convert blob to JSON (IOS only)" expect={true} actual={true} />)
88
+//     }),
89
+//     // FIXME: IOS only
90
+//     // https://github.com/facebook/react-native/issues/9178
91
+//     get((res) => {
92
+//       contentLength = res.headers['Content-Length']
93
+//       if(isIOS)
94
+//         return res.text()
95
+//       return Promise.resolve()
96
+//
97
+//     }, (data) => {
98
+//       try {
99
+//         report(<Assert key="content length should correct (IOS only)" expect={Math.floor(contentLength)} actual={data ? data.length : 0} />)
100
+//       } catch(e){}
101
+//     }, (err) => {
102
+//       console.warn(err, err.stack)
103
+//     }),
104
+//     get((res) => {
105
+//       console.log('##',res.headers['Content-Length'], res)
106
+//       contentLength = res.headers['Content-Length']
107
+//       return res.blob()
108
+//     }, (blob) => {
109
+//       return fs.stat(blob._ref).then((stat) => {
110
+//         report(<Assert key="stored file size correct" expect={contentLength} actual={stat.size} />)
111
+//         return blob.readBlob('base64')
112
+//       })
113
+//       .then((b64) => {
114
+//         report(<Info key="stored image">
115
+//           <Image style={styles.image} source={{uri : 'data:image/png;BASE64 ,' + b64}}/>
116
+//         </Info>)
117
+//       })
118
+//
119
+//     }, (err) => {
120
+//       console.warn(err, err.stack)
121
+//     })
122
+//   ]
123
+//   Promise.all(promises).then( () => done() )
124
+//
125
+// })
126
+//
127
+// describe('POST different kinds of body', (report, done) => {
128
+//
129
+//   let image = RNTest.prop('image')
130
+//   let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
131
+//
132
+//   function upload(desc, pBody) {
133
+//     let name = `fetch-replacement-${Platform.OS}-${Date.now()}-${Math.random()}.png`
134
+//     return pBody.then((body) =>
135
+//       fetch('https://content.dropboxapi.com/2/files/upload', {
136
+//         method : 'post',
137
+//         headers : {
138
+//           Authorization : `Bearer ${DROPBOX_TOKEN}`,
139
+//           'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
140
+//           'Content-Type' : 'application/octet-stream'
141
+//         },
142
+//         body})
143
+//     )
144
+//     .then((res) => {
145
+//       return res.json()
146
+//     })
147
+//     .then((info) => {
148
+//       report(<Assert key={desc} expect={name} actual={info.name}/>)
149
+//     })
150
+//   }
151
+//
152
+//   fetch(`${TEST_SERVER_URL}/public/github2.jpg`)
153
+//     .then((res) => res.blob())
154
+//     .then((b) => b.readBlob('base64'))
155
+//     .then((image) => {
156
+//       let tests = [
157
+//         upload('upload base64 encoded body', Promise.resolve(image)),
158
+//         upload('upload Blob body', Blob.build(image, 'image/png;BASE64')),
159
+//         upload('upload file path body', fs.writeFile(tmpPath, image, 'base64').then(() => {
160
+//           Promise.resolve(RNFetchBlob.wrap(tmpPath))
161
+//         }))
162
+//       ]
163
+//       Promise.all(tests).then(() => done())
164
+//     })
165
+//
166
+// })
167
+//
168
+// describe('Request header correctness', (report, done) => {
169
+//
170
+//   let expect = {
171
+//     'hello' : 'world',
172
+//     'Content-Type' : 'application/json',
173
+//     'foo' : encodeURIComponent('福' + Date.now())
174
+//   }
175
+//
176
+//   fetch(`${TEST_SERVER_URL}/xhr-header`, {
177
+//     method : 'GET',
178
+//     headers : expect
179
+//   })
180
+//   .then((res) => res.json())
181
+//   .then((actual) => {
182
+//     report(<Info key={JSON.stringify(actual)}/>)
183
+//     report(<Assert key="header field test #1" expect={expect.hello} actual={actual.hello}/>)
184
+//     report(<Assert key="header field test #2" expect={expect['content-type']} actual={actual['content-type']}/>)
185
+//     report(<Assert key="header field test #3" expect={expect.foo} actual={actual.foo}/>)
186
+//     done()
187
+//   })
188
+//
189
+// })
187 190
 
188 191
 describe('Upload form data ', (report, done) => {
189 192
 

+ 86
- 86
test/test-firebase.js Ver arquivo

@@ -61,89 +61,89 @@ describe('firebase login', (report, done) => {
61 61
   })
62 62
 })
63 63
 
64
-describe('upload file to firebase', (report, done) => {
65
-
66
-  // create Blob from BASE64 data
67
-  let blob = new Blob(RNTest.prop('image'), { type : 'image/png;BASE64'})
68
-  let testImage = `firebase-test-${Platform.OS}-${Date.now()}.png`
69
-  RNTest.prop('firebase-image', testImage)
70
-  // start test after Blob created
71
-  blob.onCreated(() => {
72
-    let storage = firebase.storage().ref('rnfbtest')
73
-    let task = storage
74
-      .child(RNTest.prop('firebase-image'))
75
-      .put(blob, { contentType : 'image/png' })
76
-      .then((snapshot) => {
77
-        console.log(snapshot.metadata)
78
-        report(<Assert key="upload success"
79
-          expect={true}
80
-          actual={true}/>,
81
-        <Info key="uploaded file stat" >
82
-          <Text>{snapshot.totalBytes}</Text>
83
-          <Text>{JSON.stringify(snapshot.metadata)}</Text>
84
-        </Info>)
85
-        done()
86
-      })
87
-  })
88
-})
89
-
90
-describe('download firebase storage item', (report, done) => {
91
-  let storage = firebase.storage().ref('rnfbtest/' + RNTest.prop('firebase-image'))
92
-  storage.getDownloadURL().then((url) => {
93
-    console.log(url)
94
-    report(<Info key="image viewer">
95
-      <Image style={styles.image} source={{uri : url}}/>
96
-    </Info>)
97
-    done()
98
-  })
99
-})
100
-
101
-let tier2FileName = `firebase-test-${Platform.OS}-github2.jpg`
102
-
103
-describe('upload using file path', (report, done) => {
104
-  RNFetchBlob
105
-    .config({ fileCache : true, appendExt : 'jpg' })
106
-    .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
107
-    .then((resp) => {
108
-      report(<Info key="test image">
109
-        <Image style={styles.image} source={{uri : prefix + resp.path()}}/>
110
-      </Info>)
111
-      let blob = new Blob(RNFetchBlob.wrap(resp.path()), { type : 'image/jpg' })
112
-      blob.onCreated(() => {
113
-        firebase.storage().ref('rnfbtest')
114
-          .child(tier2FileName)
115
-          .put(blob, { contentType : 'image/jpg' })
116
-          .then(() => {
117
-            report(<Assert key="upload finished" />)
118
-            done()
119
-          })
120
-      })
121
-    })
122
-})
123
-
124
-let directURL = null
125
-
126
-describe('verify uploaded file', (report, done) => {
127
-  firebase.storage().ref('rnfbtest/' + tier2FileName)
128
-    .getDownloadURL()
129
-    .then((url) => {
130
-      directURL = url
131
-      report(
132
-        <Info key="image viewer">
133
-          <Image style={styles.image} source={{uri : url}}/>
134
-        </Info>)
135
-      done()
136
-    })
137
-})
138
-
139
-describe('download to base64', (report, done) => {
140
-  RNFetchBlob.fetch('GET', directURL).then((resp) => {
141
-    report(
142
-      <Info key="image data">
143
-        <Image
144
-          style={styles.image}
145
-          source={{uri : 'data:image/jpg;base64 ,'+ resp.base64()}}/>
146
-      </Info>)
147
-    done()
148
-  })
149
-})
64
+// describe('upload file to firebase', (report, done) => {
65
+//
66
+//   // create Blob from BASE64 data
67
+//   let blob = new Blob(RNTest.prop('image'), { type : 'image/png;BASE64'})
68
+//   let testImage = `firebase-test-${Platform.OS}-${Date.now()}.png`
69
+//   RNTest.prop('firebase-image', testImage)
70
+//   // start test after Blob created
71
+//   blob.onCreated(() => {
72
+//     let storage = firebase.storage().ref('rnfbtest')
73
+//     let task = storage
74
+//       .child(RNTest.prop('firebase-image'))
75
+//       .put(blob, { contentType : 'image/png' })
76
+//       .then((snapshot) => {
77
+//         console.log(snapshot.metadata)
78
+//         report(<Assert key="upload success"
79
+//           expect={true}
80
+//           actual={true}/>,
81
+//         <Info key="uploaded file stat" >
82
+//           <Text>{snapshot.totalBytes}</Text>
83
+//           <Text>{JSON.stringify(snapshot.metadata)}</Text>
84
+//         </Info>)
85
+//         done()
86
+//       })
87
+//   })
88
+// })
89
+//
90
+// describe('download firebase storage item', (report, done) => {
91
+//   let storage = firebase.storage().ref('rnfbtest/' + RNTest.prop('firebase-image'))
92
+//   storage.getDownloadURL().then((url) => {
93
+//     console.log(url)
94
+//     report(<Info key="image viewer">
95
+//       <Image style={styles.image} source={{uri : url}}/>
96
+//     </Info>)
97
+//     done()
98
+//   })
99
+// })
100
+//
101
+// let tier2FileName = `firebase-test-${Platform.OS}-github2.jpg`
102
+//
103
+// describe('upload using file path', (report, done) => {
104
+//   RNFetchBlob
105
+//     .config({ fileCache : true, appendExt : 'jpg' })
106
+//     .fetch('GET', `${TEST_SERVER_URL}/public/github2.jpg`)
107
+//     .then((resp) => {
108
+//       report(<Info key="test image">
109
+//         <Image style={styles.image} source={{uri : prefix + resp.path()}}/>
110
+//       </Info>)
111
+//       let blob = new Blob(RNFetchBlob.wrap(resp.path()), { type : 'image/jpg' })
112
+//       blob.onCreated(() => {
113
+//         firebase.storage().ref('rnfbtest')
114
+//           .child(tier2FileName)
115
+//           .put(blob, { contentType : 'image/jpg' })
116
+//           .then(() => {
117
+//             report(<Assert key="upload finished" />)
118
+//             done()
119
+//           })
120
+//       })
121
+//     })
122
+// })
123
+//
124
+// let directURL = null
125
+//
126
+// describe('verify uploaded file', (report, done) => {
127
+//   firebase.storage().ref('rnfbtest/' + tier2FileName)
128
+//     .getDownloadURL()
129
+//     .then((url) => {
130
+//       directURL = url
131
+//       report(
132
+//         <Info key="image viewer">
133
+//           <Image style={styles.image} source={{uri : url}}/>
134
+//         </Info>)
135
+//       done()
136
+//     })
137
+// })
138
+//
139
+// describe('download to base64', (report, done) => {
140
+//   RNFetchBlob.fetch('GET', directURL).then((resp) => {
141
+//     report(
142
+//       <Info key="image data">
143
+//         <Image
144
+//           style={styles.image}
145
+//           source={{uri : 'data:image/jpg;base64 ,'+ resp.base64()}}/>
146
+//       </Info>)
147
+//     done()
148
+//   })
149
+// })

+ 4
- 4
test/test-init.js Ver arquivo

@@ -66,12 +66,12 @@ describe('GET image from server', (report, done) => {
66 66
 // require('./test-0.6.2')
67 67
 // require('./test-0.6.3')
68 68
 // require('./test-0.7.0')
69
-require('./test-0.8.0')
70
-// require('./test-0.8.2')
71
-// require('./test-fetch')
69
+// require('./test-0.8.0')
70
+// require('./test-0.9.0')
71
+require('./test-fetch')
72 72
 // require('./test-fs')
73 73
 // require('./test-xmlhttp')
74 74
 // require('./test-blob')
75
-// require('./test-firebase')
75
+require('./test-firebase')
76 76
 // require('./test-android')
77 77
 // require('./benchmark')