Browse Source

Add XMLHttpRequest tests #44

Ben Hsieh 8 years ago
parent
commit
18da5f3da7
5 changed files with 261 additions and 5 deletions
  1. 5
    1
      test/react-native-testkit/lib/test-context.js
  2. 31
    0
      test/test-blob.js
  3. 2
    2
      test/test-firebase.js
  4. 3
    2
      test/test-init.js
  5. 220
    0
      test/test-xmlhttp.js

+ 5
- 1
test/react-native-testkit/lib/test-context.js View File

@@ -117,7 +117,11 @@ export default class TestContext {
117 117
 
118 118
       // run test body
119 119
       new Promise((done) => {
120
-        this.fn.bind(this)(update, done)
120
+        try {
121
+          this.fn.bind(this)(update, done)
122
+        } catch(err) {
123
+            console.warn(err.stach)
124
+        }
121 125
       })
122 126
       .then((...res) => {
123 127
         if(!expired) {

+ 31
- 0
test/test-blob.js View File

@@ -0,0 +1,31 @@
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
+const { Assert, Comparer, Info, prop } = RNTest
21
+const describe = RNTest.config({
22
+  group : 'Blob',
23
+  run : true,
24
+  expand : true,
25
+  timeout : 20000,
26
+})
27
+const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
28
+const  dirs = RNFetchBlob.fs.dirs
29
+
30
+let prefix = ((Platform.OS === 'android') ? 'file://' : '')
31
+let file = RNTest.prop('image')

+ 2
- 2
test/test-firebase.js View File

@@ -26,7 +26,7 @@ const { Assert, Comparer, Info, prop } = RNTest
26 26
 const describe = RNTest.config({
27 27
   group : 'firebase',
28 28
   run : true,
29
-  expand : true,
29
+  expand : false,
30 30
   timeout : 300000000,
31 31
 })
32 32
 const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
@@ -66,7 +66,7 @@ describe('upload file to firebase', (report, done) => {
66 66
 
67 67
   // create Blob from BASE64 data
68 68
   let blob = new Blob(RNTest.prop('image'), { type : 'image/png;BASE64'})
69
-  let testImage = `firebase-test-${Platform.OS}-${new Date()}.png`
69
+  let testImage = `firebase-test-${Platform.OS}-${Date.now()}.png`
70 70
   RNTest.prop('firebase-image', testImage)
71 71
   // start test after Blob created
72 72
   blob.onCreated(() => {

+ 3
- 2
test/test-init.js View File

@@ -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.0.11:8123')
22
-prop('TEST_SERVER_URL_SSL', 'https://192.168.0.11:8124')
21
+prop('TEST_SERVER_URL', 'http://192.168.16.70:8123')
22
+prop('TEST_SERVER_URL_SSL', 'https://192.168.16.70:8124')
23 23
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
24 24
 prop('styles', {
25 25
   image : {
@@ -67,5 +67,6 @@ describe('GET image from server', (report, done) => {
67 67
 // require('./test-0.7.0')
68 68
 // require('./test-0.8.0')
69 69
 // require('./test-fs')
70
+require('./test-xmlhttp')
70 71
 require('./test-firebase')
71 72
 // require('./test-android')

+ 220
- 0
test/test-xmlhttp.js View File

@@ -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
+})