|
@@ -0,0 +1,220 @@
|
|
1
|
+import RNTest from './react-native-testkit/'
|
|
2
|
+import React from 'react'
|
|
3
|
+import RNFetchBlob from 'react-native-fetch-blob'
|
|
4
|
+import Timer from 'react-timer-mixin'
|
|
5
|
+
|
|
6
|
+import {
|
|
7
|
+ StyleSheet,
|
|
8
|
+ Text,
|
|
9
|
+ View,
|
|
10
|
+ ScrollView,
|
|
11
|
+ CameraRoll,
|
|
12
|
+ Platform,
|
|
13
|
+ Dimensions,
|
|
14
|
+ Image,
|
|
15
|
+} from 'react-native';
|
|
16
|
+
|
|
17
|
+const fs = RNFetchBlob.fs
|
|
18
|
+const Blob = RNFetchBlob.polyfill.Blob
|
|
19
|
+
|
|
20
|
+window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
|
|
21
|
+window.Blob = Blob
|
|
22
|
+window.FormData = RNFetchBlob.polyfill.FormData
|
|
23
|
+
|
|
24
|
+const { Assert, Comparer, Info, prop } = RNTest
|
|
25
|
+const describe = RNTest.config({
|
|
26
|
+ group : 'XMLHttpRequest',
|
|
27
|
+ run : true,
|
|
28
|
+ expand : true,
|
|
29
|
+ timeout : 20000,
|
|
30
|
+})
|
|
31
|
+const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
|
|
32
|
+const dirs = RNFetchBlob.fs.dirs
|
|
33
|
+
|
|
34
|
+let prefix = ((Platform.OS === 'android') ? 'file://' : '')
|
|
35
|
+let file = RNTest.prop('image')
|
|
36
|
+
|
|
37
|
+/**
|
|
38
|
+ * Test cases are based on W3C github repository.
|
|
39
|
+ * {@link https://github.com/w3c/web-platform-tests/blob/master/XMLHttpRequest/}
|
|
40
|
+ */
|
|
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
|
+ function onError(method, code) {
|
|
118
|
+ let xhr = new XMLHttpRequest()
|
|
119
|
+ xhr.open(method, `${TEST_SERVER_URL}/xhr-code/${code}`)
|
|
120
|
+ xhr.onreadystatechange = function() {
|
|
121
|
+ report(
|
|
122
|
+ <Assert
|
|
123
|
+ key={`response data of ${method} ${code} should be empty`}
|
|
124
|
+ expect=""
|
|
125
|
+ actual={xhr.response}/>,
|
|
126
|
+ <Assert
|
|
127
|
+ key={`status of ${method} ${code} should be ${code}`}
|
|
128
|
+ expect={code}
|
|
129
|
+ actual={xhr.status}/>
|
|
130
|
+ )
|
|
131
|
+ }
|
|
132
|
+ xhr.onerror = function() {
|
|
133
|
+ report(
|
|
134
|
+ <Assert
|
|
135
|
+ key={'HTTP error should not throw error event'}
|
|
136
|
+ expect={false}
|
|
137
|
+ actual={true}/>)
|
|
138
|
+ }
|
|
139
|
+ xhr.send()
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+})
|
|
143
|
+
|
|
144
|
+describe('request headers records should be cleared by open()', (report, done) => {
|
|
145
|
+ let xhr = new XMLHttpRequest()
|
|
146
|
+ xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
|
|
147
|
+ xhr.setRequestHeader('header-test', '100')
|
|
148
|
+ xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
|
|
149
|
+ xhr.setRequestHeader('header-test', '200')
|
|
150
|
+ xhr.send()
|
|
151
|
+ xhr.onreadystatechange = function() {
|
|
152
|
+ if(this.readyState == 4) {
|
|
153
|
+ report(<Assert key="headers should be cleared by open()"
|
|
154
|
+ expect={'200'}
|
|
155
|
+ actual={this.response['header-test']}/>)
|
|
156
|
+ done()
|
|
157
|
+ }
|
|
158
|
+ }
|
|
159
|
+})
|
|
160
|
+
|
|
161
|
+/**
|
|
162
|
+ * {@link https://github.com/w3c/web-platform-tests/blob/master/XMLHttpRequest/setrequestheader-bogus-name.htm}
|
|
163
|
+ */
|
|
164
|
+describe('invalid characters should not exists in header field', (report, done) => {
|
|
165
|
+ function try_name(name) {
|
|
166
|
+ try {
|
|
167
|
+ let client = new XMLHttpRequest()
|
|
168
|
+ client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
|
|
169
|
+ client.setRequestHeader(name, '123')
|
|
170
|
+ } catch(err) {
|
|
171
|
+ report(
|
|
172
|
+ <Assert key={`invalid header type ${name} should throw SyntaxError`}
|
|
173
|
+ actual={/syntaxerror/i.test(err)}
|
|
174
|
+ expect={true}
|
|
175
|
+ />)
|
|
176
|
+ }
|
|
177
|
+ }
|
|
178
|
+ function try_byte_string(name) {
|
|
179
|
+ try {
|
|
180
|
+ let client = new XMLHttpRequest()
|
|
181
|
+ client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
|
|
182
|
+ client.setRequestHeader(name, '123')
|
|
183
|
+ } catch(err) {
|
|
184
|
+ report(
|
|
185
|
+ <Assert key={`invalid header field ${name} type should throw TypeError`}
|
|
186
|
+ actual={/typeerror/i.test(err)}
|
|
187
|
+ expect={true}
|
|
188
|
+ />)
|
|
189
|
+ }
|
|
190
|
+ }
|
|
191
|
+ var invalid_headers = ["(", ")", "<", ">", "@", ",", ";", ":", "\\",
|
|
192
|
+ "\"", "/", "[", "]", "?", "=", "{", "}", " ",
|
|
193
|
+ /* HT already tested in the loop below */
|
|
194
|
+ "\u007f", "", "t\rt", "t\nt", "t: t", "t:t",
|
|
195
|
+ "t<t", "t t", " tt", ":tt", "\ttt", "\vtt", "t\0t",
|
|
196
|
+ "t\"t", "t,t", "t;t", "()[]{}", "a?B", "a=B"]
|
|
197
|
+ var invalid_byte_strings = ["テスト", "X-テスト"]
|
|
198
|
+ for (var i = 0; i < 32; ++i) {
|
|
199
|
+ invalid_headers.push(String.fromCharCode(i))
|
|
200
|
+ }
|
|
201
|
+ for (var i = 0; i < invalid_headers.length; ++i) {
|
|
202
|
+ try_name(invalid_headers[i])
|
|
203
|
+ }
|
|
204
|
+ for (var i = 0; i < invalid_byte_strings.length; ++i) {
|
|
205
|
+ try_byte_string(invalid_byte_strings[i])
|
|
206
|
+ }
|
|
207
|
+ done()
|
|
208
|
+
|
|
209
|
+})
|
|
210
|
+
|
|
211
|
+describe('invoke setRequestHeader() before open()', (report, done) => {
|
|
212
|
+ try {
|
|
213
|
+ new XMLHttpRequest().setRequestHeader('foo', 'bar')
|
|
214
|
+ } catch(err) {
|
|
215
|
+ report(<Assert key="should throw InvalidStateRrror"
|
|
216
|
+ expect={true}
|
|
217
|
+ actual={/invalidstateerror/i.test(err)}/>)
|
|
218
|
+ done()
|
|
219
|
+ }
|
|
220
|
+})
|