Browse Source

Add test case #70

Ben Hsieh 8 years ago
parent
commit
64ac7f2e09
1 changed files with 81 additions and 44 deletions
  1. 81
    44
      test/test-fetch.js

+ 81
- 44
test/test-fetch.js View File

13
 } from 'react-native';
13
 } from 'react-native';
14
 
14
 
15
 window.Blob = RNFetchBlob.polyfill.Blob
15
 window.Blob = RNFetchBlob.polyfill.Blob
16
+window.File = RNFetchBlob.polyfill.File
16
 window.fetch = new RNFetchBlob.polyfill.Fetch({
17
 window.fetch = new RNFetchBlob.polyfill.Fetch({
17
   auto : true,
18
   auto : true,
18
   binaryContentTypes : ['image/', 'video/', 'audio/']
19
   binaryContentTypes : ['image/', 'video/', 'audio/']
23
 const describe = RNTest.config({
24
 const describe = RNTest.config({
24
   group : 'Fetch polyfill',
25
   group : 'Fetch polyfill',
25
   run : true,
26
   run : true,
26
-  expand : true,
27
+  expand : false,
27
   timeout : 10000,
28
   timeout : 10000,
28
 })
29
 })
29
 const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
30
 const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, FILENAME, DROPBOX_TOKEN, styles } = prop()
72
       .catch((err) => fn3(err))
73
       .catch((err) => fn3(err))
73
   }
74
   }
74
   let contentLength = 0
75
   let contentLength = 0
76
+  let isIOS = Platform.OS === 'ios'
75
   let promises = [
77
   let promises = [
76
-    get((res) => res.json(), (data) => {
77
-      report(<Assert key="should not convert blob to JSON" expect={true} actual={false} />)
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} />)
78
     }, (err) => {
86
     }, (err) => {
79
-      report(<Assert key="should not convert blob to JSON" expect={true} actual={true} />)
87
+      report(<Assert key="should not convert blob to JSON (IOS only)" expect={true} actual={true} />)
80
     }),
88
     }),
89
+    // FIXME: IOS only
90
+    // https://github.com/facebook/react-native/issues/9178
81
     get((res) => {
91
     get((res) => {
82
       contentLength = res.headers['Content-Length']
92
       contentLength = res.headers['Content-Length']
83
-      return res.text()
93
+      if(isIOS)
94
+        return res.text()
95
+      return Promise.resolve()
96
+
84
     }, (data) => {
97
     }, (data) => {
85
-      report(
86
-        <Assert key="should convert blob to text" expect={true} actual={true} />,
87
-        <Assert key="content length should correct" expect={Math.floor(contentLength)} actual={data.length} />)
98
+      try {
99
+        report(<Assert key="content length should correct (IOS only)" expect={Math.floor(contentLength)} actual={data ? data.length : 0} />)
100
+      } catch(e){}
88
     }, (err) => {
101
     }, (err) => {
89
       console.warn(err, err.stack)
102
       console.warn(err, err.stack)
90
-      report(<Assert key="should convert blob to text" expect={true} actual={false} />)
91
     }),
103
     }),
92
     get((res) => {
104
     get((res) => {
93
       contentLength = res.headers['Content-Length']
105
       contentLength = res.headers['Content-Length']
94
       return res.blob()
106
       return res.blob()
95
     }, (blob) => {
107
     }, (blob) => {
96
-      return fs.stat(blob.getRNFetchBlobRef()).then((stat) => {
108
+      return fs.stat(blob._ref).then((stat) => {
97
         report(<Assert key="stored file size correct" expect={contentLength} actual={stat.size} />)
109
         report(<Assert key="stored file size correct" expect={contentLength} actual={stat.size} />)
98
         return blob.readBlob('base64')
110
         return blob.readBlob('base64')
99
       })
111
       })
105
 
117
 
106
     }, (err) => {
118
     }, (err) => {
107
       console.warn(err, err.stack)
119
       console.warn(err, err.stack)
108
-      report(<Assert key="should convert blob to blob" expect={true} actual={false} />)
109
     })
120
     })
110
   ]
121
   ]
111
   Promise.all(promises).then( () => done() )
122
   Promise.all(promises).then( () => done() )
112
 
123
 
113
 })
124
 })
114
 
125
 
115
-describe('POST different types of body', (report, done) => {
126
+describe('POST different kinds of body', (report, done) => {
116
 
127
 
117
   let image = RNTest.prop('image')
128
   let image = RNTest.prop('image')
118
   let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
129
   let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
119
 
130
 
120
-  function upload(desc, method, pBody) {
121
-    let name = `fetch-replacement-${Platform.OS}-${Date.now()}.png`
131
+  function upload(desc, pBody) {
132
+    let name = `fetch-replacement-${Platform.OS}-${Date.now()}-${Math.random()}.png`
122
     return pBody.then((body) =>
133
     return pBody.then((body) =>
123
       fetch('https://content.dropboxapi.com/2/files/upload', {
134
       fetch('https://content.dropboxapi.com/2/files/upload', {
124
         method : 'post',
135
         method : 'post',
126
           Authorization : `Bearer ${DROPBOX_TOKEN}`,
137
           Authorization : `Bearer ${DROPBOX_TOKEN}`,
127
           'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
138
           'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
128
           'Content-Type' : 'application/octet-stream'
139
           'Content-Type' : 'application/octet-stream'
129
-        }, body })
140
+        },
141
+        body})
130
     )
142
     )
131
     .then((res) => {
143
     .then((res) => {
132
       return res.json()
144
       return res.json()
136
     })
148
     })
137
   }
149
   }
138
 
150
 
139
-  let tests = [
140
-    upload('upload base64 encoded body', Promise.resolve(image)),
141
-    upload('upload Blob body', Blob.build(image, 'image/png;BASE64')),
142
-    upload('upload file path body', fs.writeFile(tmpPath, image, 'base64').then(() => Promise.resolve(RNFetchBlob.wrap(tmpPath))))
143
-  ]
144
-
145
-  Promise.all(tests).then(() => done())
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
+    })
146
 
162
 
147
 })
163
 })
148
 
164
 
149
-describe('check HTTP body correctness', (report, done) => {
150
-
151
-  let tmpPath = dirs.DocumentDir + '/tmp-' + Date.now()
165
+describe('Request header correctness', (report, done) => {
152
 
166
 
153
-  function upload(pBody) {
154
-    return pBody.then((body) =>
155
-      fetch('https://content.dropboxapi.com/2/files/upload', {
156
-        method : 'POST',
157
-        headers : {
158
-          Authorization : `Bearer ${DROPBOX_TOKEN}`,
159
-          'Dropbox-API-Arg': '{\"path\": \"/rn-upload/'+name+'\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}',
160
-          'Content-Type' : 'application/octet-stream'
161
-        }, body })
162
-      .then((res) => res.json())
163
-      .then((info) => {
164
-        
165
-      })
166
-    )
167
+  let expect = {
168
+    'hello' : 'world',
169
+    'Content-Type' : 'application/json',
170
+    'foo' : encodeURIComponent('福' + Date.now())
167
   }
171
   }
168
 
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
+  })
169
 
185
 
170
-  let pUnicodeBody = fetch(`${TEST_SERVER_URL}/public/utf8-dummy`, { method : 'GET' })
171
-    .then((res) => res.text())
186
+})
172
 
187
 
173
-  let tests = [
174
-    upload(pUnicodeBody)
175
-  ]
188
+describe('Upload form data ', (report, done) => {
189
+
190
+  let form = new FormData()
191
+  let expectName = 'fetch-replacement-test' + new Date()
192
+  File
193
+  .build('test-image.png', RNTest.prop('image'), { type : 'image/png;BASE64' })
194
+  .then((file) => {
195
+
196
+    form.append('name', expectName)
197
+    form.append('file', file)
198
+    return fetch(`${TEST_SERVER_URL}/upload-form`, {
199
+      method : 'POST',
200
+      body : form
201
+    })
202
+
203
+  })
204
+  .then((res) => res.json())
205
+  .then((json) => {
206
+    report(
207
+      <Assert key="form data verify" expect={expectName} actual={json.fields.name}/>,
208
+      <Assert key="file size verify" expect={23975} actual={json.files[0].size}/>,
209
+      <Assert key="form data file name verify" expect={'test-image.png'} actual={json.files[0].originalname}/>
210
+    )
211
+    done()
212
+  })
176
 
213
 
177
 
214
 
178
 })
215
 })