|
@@ -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
|
|