Browse Source

Fix XMLHttpRequest so that it compatible to WHATWG-Fetch #106

Ben Hsieh 8 years ago
parent
commit
d038101991
3 changed files with 71 additions and 10 deletions
  1. 2
    9
      src/polyfill/XMLHttpRequest.js
  2. 3
    1
      src/polyfill/XMLHttpRequestEventTarget.js
  3. 66
    0
      test/test-0.9.4.js

+ 2
- 9
src/polyfill/XMLHttpRequest.js View File

@@ -11,7 +11,7 @@ import ProgressEvent from './ProgressEvent.js'
11 11
 const log = new Log('XMLHttpRequest')
12 12
 
13 13
 log.disable()
14
-// log.level(2)
14
+// log.level(3)
15 15
 
16 16
 const UNSENT = 0
17 17
 const OPENED = 1
@@ -307,6 +307,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
307 307
     log.debug('XMLHttpRequest done', this._url, resp, this)
308 308
     this._statusText = this._status
309 309
     let responseDataReady = () => {
310
+      log.debug('request done state = 4')
310 311
       this.dispatchEvent('load')
311 312
       this.dispatchEvent('loadend')
312 313
       this._dispatchReadStateChange(XMLHttpRequest.DONE)
@@ -316,14 +317,6 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
316 317
       let info = resp.respInfo || {}
317 318
       log.debug(this._url, info, info.respType)
318 319
       switch(info.respType) {
319
-        case 'json' :
320
-        try{
321
-          this._responseText = resp.text()
322
-          this._response = resp.json()
323
-          responseDataReady()
324
-        } catch(err) {
325
-        }
326
-        break;
327 320
         case 'blob' :
328 321
           resp.blob().then((b) => {
329 322
             this._responseText = resp.text()

+ 3
- 1
src/polyfill/XMLHttpRequestEventTarget.js View File

@@ -8,6 +8,7 @@ import Log from '../utils/log.js'
8 8
 const log = new Log('XMLHttpRequestEventTarget')
9 9
 
10 10
 log.disable()
11
+// log.level(3)
11 12
 
12 13
 export default class XMLHttpRequestEventTarget extends EventTarget {
13 14
 
@@ -25,6 +26,7 @@ export default class XMLHttpRequestEventTarget extends EventTarget {
25 26
   }
26 27
 
27 28
   dispatchEvent(event:string, e:Event) {
29
+    log.debug('dispatch event', event, e)
28 30
     super.dispatchEvent(event, e)
29 31
     switch(event) {
30 32
       case 'abort' :
@@ -69,7 +71,7 @@ export default class XMLHttpRequestEventTarget extends EventTarget {
69 71
   }
70 72
 
71 73
   set onload(fn:(e:Event) => void) {
72
-    log.info('set onload')
74
+    log.info('set onload', fn)
73 75
     this._onload = fn
74 76
   }
75 77
 

+ 66
- 0
test/test-0.9.4.js View File

@@ -0,0 +1,66 @@
1
+import RNTest from './react-native-testkit/'
2
+import React from 'react'
3
+import RNFetchBlob from 'react-native-fetch-blob'
4
+import {
5
+  StyleSheet,
6
+  Text,
7
+  View,
8
+  ScrollView,
9
+  Platform,
10
+  Dimensions,
11
+  Image,
12
+} from 'react-native';
13
+
14
+window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
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 : '0.9.4',
25
+  run : true,
26
+  expand : true,
27
+  timeout : 20000,
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('issue #105', (report, done) => {
35
+  let tmp = null
36
+  RNFetchBlob
37
+    .config({ fileCache : true })
38
+    .fetch('GET', `${TEST_SERVER_URL}/public/github.png`)
39
+    .then((res) => {
40
+      tmp = res.path()
41
+      return RNFetchBlob.fetch('POST', `${TEST_SERVER_URL}/upload-form`, {
42
+        'Content-Type' : 'multipart/form-data'
43
+      }, [
44
+        { name : 'data', data : 'issue#105 test' },
45
+        { name : 'file', filename : 'github.png', data : RNFetchBlob.wrap(tmp) }
46
+      ])
47
+    })
48
+    .then((res) => {
49
+      done()
50
+    })
51
+})
52
+
53
+describe('issue #106', (report, done) => {
54
+
55
+  fetch('https://rnfb-test-app.firebaseapp.com/6m-json.json')
56
+    .then((res) => {
57
+      console.log('##',res)
58
+      console.log('## converted')
59
+      return res.json()
60
+    })
61
+    .then((data) => {
62
+      console.log(data)
63
+      done()
64
+    })
65
+
66
+})