Browse Source

Change test cases

Ben Hsieh 8 years ago
parent
commit
a73f4377ea
4 changed files with 357 additions and 249 deletions
  1. 14
    1
      test-server/server.js
  2. 35
    13
      test/test-0.8.2.js
  3. 73
    0
      test/test-fetch.js
  4. 235
    235
      test/test-xmlhttp.js

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

@@ -148,12 +148,25 @@ app.post('/upload', bodyParser.urlencoded({ extended : true }), (req, res) => {
148 148
   res.status(200).send(req.body)
149 149
 })
150 150
 
151
-app.all('/timeout408', (req, res) => {
151
+app.all('/timeout408/:time', (req, res) => {
152 152
   setTimeout(function() {
153 153
     res.status(408).send('request timed out.')
154 154
   }, 5000)
155 155
 })
156 156
 
157
+app.all('/long', (req, res) => {
158
+  var count = 0;
159
+  var it = setInterval(() => {
160
+    console.log('write data', count)
161
+    res.write('a')
162
+    if(++count >60){
163
+      clearInterval(it)
164
+      res.end()
165
+    }
166
+  }, 1000);
167
+
168
+})
169
+
157 170
 app.all('/timeout', (req, res) => {
158 171
 })
159 172
 

+ 35
- 13
test/test-0.8.2.js View File

@@ -41,7 +41,6 @@ describe('#73 unicode response BASE64 content test', (report, done) => {
41 41
     return res.json()
42 42
   })
43 43
   .then((data) => {
44
-    console.log(data)
45 44
     report(<Assert key="data should correct" expect={'你好!'} actual={data.data}/>)
46 45
     done()
47 46
   })
@@ -63,25 +62,48 @@ describe('#73 unicode response content test', (report, done) => {
63 62
     })
64 63
 })
65 64
 
66
-RNTest.config({
65
+describe = RNTest.config({
67 66
   group : '0.8.2',
68 67
   run : true,
69 68
   expand : true,
70 69
   timeout : 24000
71
-})('request should not retry after timed out', (report, done) => {
70
+})
71
+
72
+describe('request should not retry after timed out', (report, done) => {
72 73
 
73 74
   let count = 0
74
-  RNFetchBlob
75
-    // .config({timeout : 2000})
76
-    .fetch('GET', `${TEST_SERVER_URL}/timeout408`)
77
-    .then((res) => {
78
-      report(<Assert key="request should not success" expect={true} actual={false}/>)
79
-    })
80
-    .catch(() => {
81
-      count ++
82
-    })
75
+  let task = RNFetchBlob
76
+    .fetch('GET', `${TEST_SERVER_URL}/timeout408/${Date.now()}`)
77
+  task.then((res) => {
78
+    report(<Assert key="request should not success" expect={true} actual={false}/>)
79
+  })
80
+  .catch(() => {
81
+    task.cancel()
82
+    count ++
83
+  })
83 84
   setTimeout(() => {
84 85
     report(<Assert key="request does not retry" expect={1} actual={count}/>)
85 86
     done()
86
-  }, 8000)
87
+  }, 12000)
88
+})
89
+
90
+describe = RNTest.config({
91
+  group : '0.8.2',
92
+  run : true,
93
+  expand : true,
94
+  timeout : 65000
95
+})
96
+
97
+describe('long live download or upload task won\'t timeout', (report, done) => {
98
+
99
+  RNFetchBlob.config({timeout : 0})
100
+  .fetch('GET', `${TEST_SERVER_URL}/long/`)
101
+  .then((res) => {
102
+    report(
103
+      <Assert key="download not terminated" expect={true} actual={true}/>,
104
+      <Info key={res.text()}/>)
105
+    done()
106
+  })
107
+
108
+
87 109
 })

+ 73
- 0
test/test-fetch.js View File

@@ -0,0 +1,73 @@
1
+import RNTest from './react-native-testkit/'
2
+import React from 'react'
3
+import RNFetchBlob from 'react-native-fetch-blob'
4
+
5
+import {
6
+  StyleSheet,
7
+  Text,
8
+  View,
9
+  ScrollView,
10
+  Platform,
11
+  Dimensions,
12
+  Image,
13
+} from 'react-native';
14
+
15
+window.Blob = RNFetchBlob.polyfill.Blob
16
+window.fetch = new RNFetchBlob.polyfill.Fetch({
17
+  auto : true,
18
+  binaryContentTypes : ['image/', 'video/', 'audio/']
19
+}).build()
20
+
21
+const fs = RNFetchBlob.fs
22
+const { Assert, Comparer, Info, prop } = RNTest
23
+const describe = RNTest.config({
24
+  group : 'Fetch polyfill',
25
+  run : true,
26
+  expand : true,
27
+  timeout : 10000,
28
+})
29
+const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
30
+const dirs = RNFetchBlob.fs.dirs
31
+
32
+let prefix = ((Platform.OS === 'android') ? 'file://' : '')
33
+
34
+describe('GET request test : text -> any', (report, done) => {
35
+
36
+  function get(fn1, fn2) {
37
+    return fetch(`${TEST_SERVER_URL}/unicode`, { method : 'GET'})
38
+    .then((res) => fn1(res))
39
+    .then((data) => fn2(data))
40
+  }
41
+  let promises =
42
+  [
43
+    get((res) => res.json(), (json) => {
44
+      report(<Assert key="json data correct" expect={'你好!'} actual={json.data}/>)
45
+    }),
46
+    get((res) => res.text(), (text) => {
47
+      report(<Assert key="text data correct" expect={'你好!'} actual={JSON.parse(text).data}/>)
48
+    }),
49
+    get((res) => res.blob(), (blob) => {
50
+      let path = blob.getRNFetchBlobRef()
51
+      return fs.readFile(path, 'utf8').then((text) => {
52
+        console.log(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 which has json response', (report, done) => {
68
+
69
+})
70
+
71
+describe('GET request which has blob response', (report, done) => {
72
+
73
+})

+ 235
- 235
test/test-xmlhttp.js View File

@@ -39,241 +39,241 @@ let file = RNTest.prop('image')
39 39
  * {@link https://github.com/w3c/web-platform-tests/blob/master/XMLHttpRequest/}
40 40
  */
41 41
 
42
-// describe('unsent state test', (report, done) => {
43
-//
44
-//   let xhr = new XMLHttpRequest()
45
-//
46
-//   try {
47
-//     xhr.setRequestHeader('x-test', 'test')
48
-//   } catch(err) {
49
-//     report(
50
-//       <Assert key="should throw InvalidState if setRequestHeader()"
51
-//         actual={/invalidstate/i.test(err)}
52
-//         expect={true}
53
-//       />)
54
-//   }
55
-//
56
-//   try {
57
-//     let xhr = new XMLHttpRequest()
58
-//     xhr.send(null)
59
-//   } catch(err) {
60
-//     report(
61
-//       <Assert key="should throw InvalidState if send()"
62
-//         actual={/invalidstate/i.test(err)}
63
-//         expect={true}
64
-//       />)
65
-//   }
66
-//   try {
67
-//     report(
68
-//       <Assert key="status is 0"
69
-//         expect={0}
70
-//         actual={xhr.status} />,
71
-//       <Assert key="statusText is empty"
72
-//         expect={''}
73
-//         actual={xhr.statusText} />,
74
-//       <Assert key="responseHeaders is empty"
75
-//         expect={''}
76
-//         actual={xhr.getAllResponseHeaders()} />,
77
-//       <Assert key="response header should not be set"
78
-//         expect={null}
79
-//         actual={xhr.getResponseHeader('x-test')} />,
80
-//       <Assert key="readyState should correct"
81
-//         expect={XMLHttpRequest.UNSENT}
82
-//         actual={xhr.readyState} />
83
-//     )
84
-//     done()
85
-//   } catch(err) {
86
-//     console.log(err.stack)
87
-//   }
88
-// })
89
-//
90
-// describe('HTTP error should not throw error event', (report, done) => {
91
-//
92
-//   onError('GET', 200)
93
-//   onError('GET', 400)
94
-//   onError('GET', 401)
95
-//   onError('GET', 404)
96
-//   onError('GET', 410)
97
-//   onError('GET', 500)
98
-//   onError('GET', 699)
99
-//
100
-//   onError('HEAD', 200)
101
-//   onError('HEAD', 404)
102
-//   onError('HEAD', 500)
103
-//   onError('HEAD', 699)
104
-//
105
-//   onError('POST', 200)
106
-//   onError('POST', 404)
107
-//   onError('POST', 500)
108
-//   onError('POST', 699)
109
-//
110
-//   onError('PUT', 200)
111
-//   onError('PUT', 404)
112
-//   onError('PUT', 500)
113
-//   onError('PUT', 699)
114
-//
115
-//   done()
116
-//
117
-//   let count = 0
118
-//   function onError(method, code) {
119
-//     let xhr = new XMLHttpRequest()
120
-//     xhr.open(method, `${TEST_SERVER_URL}/xhr-code/${code}`)
121
-//     xhr.onreadystatechange = function() {
122
-//       count++
123
-//       if(this.readyState == XMLHttpRequest.DONE) {
124
-//         report(
125
-//           <Assert
126
-//             key={`#${count} response data of ${method} ${code} should be empty`}
127
-//             expect=""
128
-//             actual={xhr.response}/>,
129
-//           <Assert
130
-//             key={`#${count} status of ${method} ${code} should be ${code}`}
131
-//             expect={code}
132
-//             actual={xhr.status}/>
133
-//         )
134
-//       }
135
-//     }
136
-//     xhr.onerror = function() {
137
-//       report(
138
-//         <Assert
139
-//           key={`HTTP error ${code} should not throw error event`}
140
-//           expect={false}
141
-//           actual={true}/>)
142
-//     }
143
-//     xhr.send()
144
-//   }
145
-//
146
-// })
147
-//
148
-// describe('request headers records should be cleared by open()', (report, done) => {
149
-//   let xhr = new XMLHttpRequest()
150
-//   xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
151
-//   xhr.setRequestHeader('value', '100')
152
-//   xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
153
-//   xhr.setRequestHeader('value', '200')
154
-//   xhr.send()
155
-//   xhr.onreadystatechange = function() {
156
-//     if(this.readyState == 4) {
157
-//       report(<Assert key="headers should be cleared by open()"
158
-//         expect={'200'}
159
-//         actual={this.response['value']}/>)
160
-//       done()
161
-//     }
162
-//   }
163
-// })
164
-//
165
-// /**
166
-//  *  {@link https://github.com/w3c/web-platform-tests/blob/master/XMLHttpRequest/setrequestheader-bogus-name.htm}
167
-//  */
168
-// describe('invalid characters should not exists in header field', (report, done) => {
169
-//   function try_name(name) {
170
-//     try {
171
-//       let client = new XMLHttpRequest()
172
-//       client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
173
-//       client.setRequestHeader(name, '123')
174
-//     } catch(err) {
175
-//       report(
176
-//         <Assert key={`invalid header type ${name} should throw SyntaxError`}
177
-//           actual={/syntaxerror/i.test(err)}
178
-//           expect={true}
179
-//         />)
180
-//     }
181
-//   }
182
-//   function try_byte_string(name) {
183
-//     try {
184
-//       let client = new XMLHttpRequest()
185
-//       client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
186
-//       client.setRequestHeader(name, '123')
187
-//     } catch(err) {
188
-//       report(
189
-//         <Assert key={`invalid header field ${name} type should throw TypeError`}
190
-//           actual={/typeerror/i.test(err)}
191
-//           expect={true}
192
-//         />)
193
-//     }
194
-//   }
195
-//   var invalid_headers = ["(", ")", "<", ">", "@", ",", ";", ":", "\\",
196
-//                          "\"", "/", "[", "]", "?", "=", "{", "}", " ",
197
-//                          "\u007f", "", "t\rt", "t\nt", "t: t", "t:t",
198
-//                          "t<t", "t t", " tt", ":tt", "\ttt", "\vtt", "t\0t",
199
-//                          "t\"t", "t,t", "t;t", "()[]{}", "a?B", "a=B"]
200
-//   var invalid_byte_strings = ["テスト", "X-テスト"]
201
-//   for (var i = 0; i < 32; ++i) {
202
-//     invalid_headers.push(String.fromCharCode(i))
203
-//   }
204
-//   for (var i = 0; i < invalid_headers.length; ++i) {
205
-//     try_name(invalid_headers[i])
206
-//   }
207
-//   for (var i = 0; i < invalid_byte_strings.length; ++i) {
208
-//     try_byte_string(invalid_byte_strings[i])
209
-//   }
210
-//   done()
211
-//
212
-// })
213
-//
214
-// describe('invoke setRequestHeader() before open()', (report, done) => {
215
-//   try {
216
-//     let xhr = new XMLHttpRequest()
217
-//     xhr.setRequestHeader('foo', 'bar')
218
-//   } catch(err) {
219
-//     report(
220
-//       <Info key="error message">
221
-//         <Text>{err}</Text>
222
-//       </Info>,
223
-//       <Assert key="should throw InvalidStateError"
224
-//         expect={true}
225
-//         actual={/invalidstateerror/i.test(err)}/>)
226
-//       done()
227
-//   }
228
-// })
229
-//
230
-// describe('upload progress event test', (report, done) => {
231
-//   let xhr = new XMLHttpRequest()
232
-//   let time = Date.now()
233
-//   let msg =  `time=${time}`
234
-//   xhr.upload.onprogress = function(e) {
235
-//     report(
236
-//       <Assert key="event object is an instance of ProgressEvent"
237
-//         expect={true}
238
-//         actual={e instanceof ProgressEvent}/>)
239
-//   }
240
-//   xhr.onreadystatechange = function() {
241
-//     if(this.readyState == XMLHttpRequest.DONE) {
242
-//       console.log(xhr)
243
-//       report(
244
-//         <Assert key="reponse should correct"
245
-//           expect={time}
246
-//           actual={parseInt(xhr.response.time)}/>,
247
-//         <Assert key="responseType should correct"
248
-//           expect={'json'}
249
-//           actual={xhr.responseType}/>)
250
-//         done()
251
-//     }
252
-//   }
253
-//   xhr.open('POST', `${TEST_SERVER_URL}/upload`)
254
-//   xhr.overrideMimeType('application/x-www-form-urlencoded')
255
-//   xhr.send(msg)
256
-//
257
-// })
258
-//
259
-// describe('timeout event catchable', (report, done) => {
260
-//   let xhr = new XMLHttpRequest()
261
-//   let count = 0
262
-//   xhr.timeout = 1
263
-//   xhr.ontimeout = function() {
264
-//     report(
265
-//       <Info key="event should only trigger once" uid="1000">
266
-//         <Text>{count}</Text>
267
-//       </Info>,
268
-//       <Assert key="event catchable"
269
-//         expect={true}
270
-//         actual={true}/>)
271
-//       done()
272
-//   }
273
-//   xhr.open('GET', `${TEST_SERVER_URL}/timeout/`)
274
-//   xhr.send()
275
-//
276
-// })
42
+describe('unsent state test', (report, done) => {
43
+
44
+  let xhr = new XMLHttpRequest()
45
+
46
+  try {
47
+    xhr.setRequestHeader('x-test', 'test')
48
+  } catch(err) {
49
+    report(
50
+      <Assert key="should throw InvalidState if setRequestHeader()"
51
+        actual={/invalidstate/i.test(err)}
52
+        expect={true}
53
+      />)
54
+  }
55
+
56
+  try {
57
+    let xhr = new XMLHttpRequest()
58
+    xhr.send(null)
59
+  } catch(err) {
60
+    report(
61
+      <Assert key="should throw InvalidState if send()"
62
+        actual={/invalidstate/i.test(err)}
63
+        expect={true}
64
+      />)
65
+  }
66
+  try {
67
+    report(
68
+      <Assert key="status is 0"
69
+        expect={0}
70
+        actual={xhr.status} />,
71
+      <Assert key="statusText is empty"
72
+        expect={''}
73
+        actual={xhr.statusText} />,
74
+      <Assert key="responseHeaders is empty"
75
+        expect={''}
76
+        actual={xhr.getAllResponseHeaders()} />,
77
+      <Assert key="response header should not be set"
78
+        expect={null}
79
+        actual={xhr.getResponseHeader('x-test')} />,
80
+      <Assert key="readyState should correct"
81
+        expect={XMLHttpRequest.UNSENT}
82
+        actual={xhr.readyState} />
83
+    )
84
+    done()
85
+  } catch(err) {
86
+    console.log(err.stack)
87
+  }
88
+})
89
+
90
+describe('HTTP error should not throw error event', (report, done) => {
91
+
92
+  onError('GET', 200)
93
+  onError('GET', 400)
94
+  onError('GET', 401)
95
+  onError('GET', 404)
96
+  onError('GET', 410)
97
+  onError('GET', 500)
98
+  onError('GET', 699)
99
+
100
+  onError('HEAD', 200)
101
+  onError('HEAD', 404)
102
+  onError('HEAD', 500)
103
+  onError('HEAD', 699)
104
+
105
+  onError('POST', 200)
106
+  onError('POST', 404)
107
+  onError('POST', 500)
108
+  onError('POST', 699)
109
+
110
+  onError('PUT', 200)
111
+  onError('PUT', 404)
112
+  onError('PUT', 500)
113
+  onError('PUT', 699)
114
+
115
+  done()
116
+
117
+  let count = 0
118
+  function onError(method, code) {
119
+    let xhr = new XMLHttpRequest()
120
+    xhr.open(method, `${TEST_SERVER_URL}/xhr-code/${code}`)
121
+    xhr.onreadystatechange = function() {
122
+      count++
123
+      if(this.readyState == XMLHttpRequest.DONE) {
124
+        report(
125
+          <Assert
126
+            key={`#${count} response data of ${method} ${code} should be empty`}
127
+            expect=""
128
+            actual={xhr.response}/>,
129
+          <Assert
130
+            key={`#${count} status of ${method} ${code} should be ${code}`}
131
+            expect={code}
132
+            actual={xhr.status}/>
133
+        )
134
+      }
135
+    }
136
+    xhr.onerror = function() {
137
+      report(
138
+        <Assert
139
+          key={`HTTP error ${code} should not throw error event`}
140
+          expect={false}
141
+          actual={true}/>)
142
+    }
143
+    xhr.send()
144
+  }
145
+
146
+})
147
+
148
+describe('request headers records should be cleared by open()', (report, done) => {
149
+  let xhr = new XMLHttpRequest()
150
+  xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
151
+  xhr.setRequestHeader('value', '100')
152
+  xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
153
+  xhr.setRequestHeader('value', '200')
154
+  xhr.send()
155
+  xhr.onreadystatechange = function() {
156
+    if(this.readyState == 4) {
157
+      report(<Assert key="headers should be cleared by open()"
158
+        expect={'200'}
159
+        actual={this.response['value']}/>)
160
+      done()
161
+    }
162
+  }
163
+})
164
+
165
+/**
166
+ *  {@link https://github.com/w3c/web-platform-tests/blob/master/XMLHttpRequest/setrequestheader-bogus-name.htm}
167
+ */
168
+describe('invalid characters should not exists in header field', (report, done) => {
169
+  function try_name(name) {
170
+    try {
171
+      let client = new XMLHttpRequest()
172
+      client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
173
+      client.setRequestHeader(name, '123')
174
+    } catch(err) {
175
+      report(
176
+        <Assert key={`invalid header type ${name} should throw SyntaxError`}
177
+          actual={/syntaxerror/i.test(err)}
178
+          expect={true}
179
+        />)
180
+    }
181
+  }
182
+  function try_byte_string(name) {
183
+    try {
184
+      let client = new XMLHttpRequest()
185
+      client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
186
+      client.setRequestHeader(name, '123')
187
+    } catch(err) {
188
+      report(
189
+        <Assert key={`invalid header field ${name} type should throw TypeError`}
190
+          actual={/typeerror/i.test(err)}
191
+          expect={true}
192
+        />)
193
+    }
194
+  }
195
+  var invalid_headers = ["(", ")", "<", ">", "@", ",", ";", ":", "\\",
196
+                         "\"", "/", "[", "]", "?", "=", "{", "}", " ",
197
+                         "\u007f", "", "t\rt", "t\nt", "t: t", "t:t",
198
+                         "t<t", "t t", " tt", ":tt", "\ttt", "\vtt", "t\0t",
199
+                         "t\"t", "t,t", "t;t", "()[]{}", "a?B", "a=B"]
200
+  var invalid_byte_strings = ["テスト", "X-テスト"]
201
+  for (var i = 0; i < 32; ++i) {
202
+    invalid_headers.push(String.fromCharCode(i))
203
+  }
204
+  for (var i = 0; i < invalid_headers.length; ++i) {
205
+    try_name(invalid_headers[i])
206
+  }
207
+  for (var i = 0; i < invalid_byte_strings.length; ++i) {
208
+    try_byte_string(invalid_byte_strings[i])
209
+  }
210
+  done()
211
+
212
+})
213
+
214
+describe('invoke setRequestHeader() before open()', (report, done) => {
215
+  try {
216
+    let xhr = new XMLHttpRequest()
217
+    xhr.setRequestHeader('foo', 'bar')
218
+  } catch(err) {
219
+    report(
220
+      <Info key="error message">
221
+        <Text>{err}</Text>
222
+      </Info>,
223
+      <Assert key="should throw InvalidStateError"
224
+        expect={true}
225
+        actual={/invalidstateerror/i.test(err)}/>)
226
+      done()
227
+  }
228
+})
229
+
230
+describe('upload progress event test', (report, done) => {
231
+  let xhr = new XMLHttpRequest()
232
+  let time = Date.now()
233
+  let msg =  `time=${time}`
234
+  xhr.upload.onprogress = function(e) {
235
+    report(
236
+      <Assert key="event object is an instance of ProgressEvent"
237
+        expect={true}
238
+        actual={e instanceof ProgressEvent}/>)
239
+  }
240
+  xhr.onreadystatechange = function() {
241
+    if(this.readyState == XMLHttpRequest.DONE) {
242
+      console.log(xhr)
243
+      report(
244
+        <Assert key="reponse should correct"
245
+          expect={time}
246
+          actual={parseInt(xhr.response.time)}/>,
247
+        <Assert key="responseType should correct"
248
+          expect={'json'}
249
+          actual={xhr.responseType}/>)
250
+        done()
251
+    }
252
+  }
253
+  xhr.open('POST', `${TEST_SERVER_URL}/upload`)
254
+  xhr.overrideMimeType('application/x-www-form-urlencoded')
255
+  xhr.send(msg)
256
+
257
+})
258
+
259
+describe('timeout event catchable', (report, done) => {
260
+  let xhr = new XMLHttpRequest()
261
+  let count = 0
262
+  xhr.timeout = 1
263
+  xhr.ontimeout = function() {
264
+    report(
265
+      <Info key="event should only trigger once" uid="1000">
266
+        <Text>{count}</Text>
267
+      </Info>,
268
+      <Assert key="event catchable"
269
+        expect={true}
270
+        actual={true}/>)
271
+      done()
272
+  }
273
+  xhr.open('GET', `${TEST_SERVER_URL}/timeout/`)
274
+  xhr.send()
275
+
276
+})
277 277
 
278 278
 describe('upload progress event should not be triggered when body is empty', (report, done) => {
279 279
   let xhr = new XMLHttpRequest()