Bläddra i källkod

Add XMLHttpRequest tests and test server API #44

Ben Hsieh 8 år sedan
förälder
incheckning
4e767a78b2
4 ändrade filer med 98 tillägg och 11 borttagningar
  1. 10
    0
      test-server/server.js
  2. 1
    1
      test/react-native-testkit/lib/test-context.js
  3. 2
    2
      test/test-init.js
  4. 85
    8
      test/test-xmlhttp.js

+ 10
- 0
test-server/server.js Visa fil

@@ -119,6 +119,16 @@ app.all('/xhr-header', (req, res) => {
119 119
   res.send(req.headers)
120 120
 })
121 121
 
122
+app.post('/upload', bodyParser.urlencoded({ extended: true }), (req, res) => {
123
+  console.log(JSON.stringify(req.headers))
124
+  console.log(JSON.stringify(req.body))
125
+  res.status(200).send(req.body)
126
+})
127
+
128
+app.all('/timeout', (res, req) => {
129
+
130
+})
131
+
122 132
 function formUpload(req, res) {
123 133
   console.log(req.headers)
124 134
   console.log(req.body)

+ 1
- 1
test/react-native-testkit/lib/test-context.js Visa fil

@@ -120,7 +120,7 @@ export default class TestContext {
120 120
         try {
121 121
           this.fn.bind(this)(update, done)
122 122
         } catch(err) {
123
-            console.warn(err.stach)
123
+            console.warn(err.stack)
124 124
         }
125 125
       })
126 126
       .then((...res) => {

+ 2
- 2
test/test-init.js Visa fil

@@ -18,8 +18,8 @@ const { Assert, Comparer, Info, prop } = RNTest
18 18
 // test environment variables
19 19
 
20 20
 prop('FILENAME', `${Platform.OS}-0.8.0-${Date.now()}.png`)
21
-prop('TEST_SERVER_URL', 'http://192.168.16.70:8123')
22
-prop('TEST_SERVER_URL_SSL', 'https://192.168.16.70:8124')
21
+prop('TEST_SERVER_URL', 'http://192.168.1.229:8123')
22
+prop('TEST_SERVER_URL_SSL', 'https://192.168.1.229:8124')
23 23
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
24 24
 prop('styles', {
25 25
   image : {

+ 85
- 8
test/test-xmlhttp.js Visa fil

@@ -20,12 +20,13 @@ const Blob = RNFetchBlob.polyfill.Blob
20 20
 window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
21 21
 window.Blob = Blob
22 22
 window.FormData = RNFetchBlob.polyfill.FormData
23
+window.ProgressEvent = RNFetchBlob.polyfill.ProgressEvent
23 24
 
24 25
 const { Assert, Comparer, Info, prop } = RNTest
25 26
 const describe = RNTest.config({
26 27
   group : 'XMLHttpRequest',
27 28
   run : true,
28
-  expand : true,
29
+  expand : false,
29 30
   timeout : 20000,
30 31
 })
31 32
 const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
@@ -114,17 +115,19 @@ describe('HTTP error should not throw error event', (report, done) => {
114 115
 
115 116
   done()
116 117
 
118
+  let count = 0
117 119
   function onError(method, code) {
118 120
     let xhr = new XMLHttpRequest()
119 121
     xhr.open(method, `${TEST_SERVER_URL}/xhr-code/${code}`)
120 122
     xhr.onreadystatechange = function() {
123
+      count++
121 124
       report(
122 125
         <Assert
123
-          key={`response data of ${method} ${code} should be empty`}
126
+          key={`#${count} response data of ${method} ${code} should be empty`}
124 127
           expect=""
125 128
           actual={xhr.response}/>,
126 129
         <Assert
127
-          key={`status of ${method} ${code} should be ${code}`}
130
+          key={`#${count} status of ${method} ${code} should be ${code}`}
128 131
           expect={code}
129 132
           actual={xhr.status}/>
130 133
       )
@@ -190,7 +193,6 @@ describe('invalid characters should not exists in header field', (report, done)
190 193
   }
191 194
   var invalid_headers = ["(", ")", "<", ">", "@", ",", ";", ":", "\\",
192 195
                          "\"", "/", "[", "]", "?", "=", "{", "}", " ",
193
-                         /* HT already tested in the loop below */
194 196
                          "\u007f", "", "t\rt", "t\nt", "t: t", "t:t",
195 197
                          "t<t", "t t", " tt", ":tt", "\ttt", "\vtt", "t\0t",
196 198
                          "t\"t", "t,t", "t;t", "()[]{}", "a?B", "a=B"]
@@ -210,11 +212,86 @@ describe('invalid characters should not exists in header field', (report, done)
210 212
 
211 213
 describe('invoke setRequestHeader() before open()', (report, done) => {
212 214
   try {
213
-    new XMLHttpRequest().setRequestHeader('foo', 'bar')
215
+    let xhr = new XMLHttpRequest()
216
+    xhr.setRequestHeader('foo', 'bar')
214 217
   } catch(err) {
215
-    report(<Assert key="should throw InvalidStateRrror"
216
-      expect={true}
217
-      actual={/invalidstateerror/i.test(err)}/>)
218
+    report(
219
+      <Info key="error message">
220
+        <Text>{err}</Text>
221
+      </Info>,
222
+      <Assert key="should throw InvalidStateError"
223
+        expect={true}
224
+        actual={/invalidstateerror/i.test(err)}/>)
225
+      done()
226
+  }
227
+})
228
+
229
+describe('upload progress event test', (report, done) => {
230
+  let xhr = new XMLHttpRequest()
231
+  let time = Date.now()
232
+  let msg =  `time=${time}`
233
+  xhr.upload.onprogress = function(e) {
234
+    report(
235
+      <Assert key="event object is an instance of ProgressEvent"
236
+        expect={true}
237
+        actual={e instanceof ProgressEvent}/>)
238
+  }
239
+  xhr.onreadystatechange = function() {
240
+    if(this.readyState == XMLHttpRequest.DONE) {
241
+      report(
242
+        <Assert key="reponse should correct"
243
+          expect={time}
244
+          actual={parseInt(xhr.response.time)}/>,
245
+        <Assert key="responseType should correct"
246
+          expect={'json'}
247
+          actual={xhr.responseType}/>)
248
+        done()
249
+    }
250
+  }
251
+  xhr.open('POST', `${TEST_SERVER_URL}/upload`)
252
+  xhr.overrideMimeType('application/x-www-form-urlencoded')
253
+  xhr.send(msg)
254
+
255
+})
256
+
257
+describe('timeout event catchable', (report, done) => {
258
+  let xhr = new XMLHttpRequest()
259
+  xhr.timeout = 1
260
+  xhr.ontimeout = function() {
261
+    report(
262
+      <Assert key="event catchable"
263
+        expect={true}
264
+        actual={true}/>)
265
+      done()
266
+  }
267
+  xhr.open('GET', `${TEST_SERVER_URL}/timeout/`)
268
+  xhr.send()
269
+
270
+})
271
+
272
+describe('upload progress event should not be triggered when body is empty', (report, done) => {
273
+  let xhr = new XMLHttpRequest()
274
+  xhr.upload.onloadstart = function() {
275
+    report(
276
+      <Assert key="loadstart event should not triggered"
277
+        expect={true}
278
+        actual={false}/>)
279
+  }
280
+  xhr.upload.onprogress = function() {
281
+    report(
282
+      <Assert key="progress event should not triggered"
283
+        expect={true}
284
+        actual={false}/>)
285
+  }
286
+  xhr.onreadystatechange = function() {
287
+    if(this.readyState == XMLHttpRequest.DONE) {
288
+      report(
289
+        <Assert key="Great! upload event not triggered"
290
+          expect={true}
291
+          actual={true}/>)
218 292
       done()
293
+    }
219 294
   }
295
+  xhr.open('GET', `${TEST_SERVER_URL}/pulbic/github.png`)
296
+  xhr.send()
220 297
 })