No Description

test-xmlhttp.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. import {
  6. StyleSheet,
  7. Text,
  8. View,
  9. ScrollView,
  10. CameraRoll,
  11. Platform,
  12. Dimensions,
  13. Image,
  14. } from 'react-native';
  15. const fs = RNFetchBlob.fs
  16. const Blob = RNFetchBlob.polyfill.Blob
  17. window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
  18. window.Blob = Blob
  19. window.ProgressEvent = RNFetchBlob.polyfill.ProgressEvent
  20. const { Assert, Comparer, Info, prop } = RNTest
  21. const describe = RNTest.config({
  22. group : 'XMLHttpRequest',
  23. run : true,
  24. expand : false,
  25. timeout : 20000,
  26. })
  27. const { TEST_SERVER_URL, TEST_SERVER_URL_SSL, DROPBOX_TOKEN, styles } = prop()
  28. const dirs = RNFetchBlob.fs.dirs
  29. let prefix = ((Platform.OS === 'android') ? 'file://' : '')
  30. let file = RNTest.prop('image')
  31. /**
  32. * Test cases are based on W3C github repository.
  33. * {@link https://github.com/w3c/web-platform-tests/blob/master/XMLHttpRequest/}
  34. */
  35. describe('unsent state test', (report, done) => {
  36. let xhr = new XMLHttpRequest()
  37. try {
  38. xhr.setRequestHeader('x-test', 'test')
  39. } catch(err) {
  40. report(
  41. <Assert key="should throw InvalidState if setRequestHeader()"
  42. actual={/invalidstate/i.test(err)}
  43. expect={true}
  44. />)
  45. }
  46. try {
  47. let xhr = new XMLHttpRequest()
  48. xhr.send(null)
  49. } catch(err) {
  50. report(
  51. <Assert key="should throw InvalidState if send()"
  52. actual={/invalidstate/i.test(err)}
  53. expect={true}
  54. />)
  55. }
  56. try {
  57. report(
  58. <Assert key="status is 0"
  59. expect={0}
  60. actual={xhr.status} />,
  61. <Assert key="statusText is empty"
  62. expect={''}
  63. actual={xhr.statusText} />,
  64. <Assert key="responseHeaders is empty"
  65. expect={''}
  66. actual={xhr.getAllResponseHeaders()} />,
  67. <Assert key="response header should not be set"
  68. expect={null}
  69. actual={xhr.getResponseHeader('x-test')} />,
  70. <Assert key="readyState should correct"
  71. expect={XMLHttpRequest.UNSENT}
  72. actual={xhr.readyState} />
  73. )
  74. done()
  75. } catch(err) {
  76. console.log(err.stack)
  77. }
  78. })
  79. describe('HTTP error should not throw error event', (report, done) => {
  80. onError('GET', 200)
  81. onError('GET', 400)
  82. onError('GET', 401)
  83. onError('GET', 404)
  84. onError('GET', 410)
  85. onError('GET', 500)
  86. onError('GET', 699)
  87. onError('HEAD', 200)
  88. onError('HEAD', 404)
  89. onError('HEAD', 500)
  90. onError('HEAD', 699)
  91. onError('POST', 200)
  92. onError('POST', 404)
  93. onError('POST', 500)
  94. onError('POST', 699)
  95. onError('PUT', 200)
  96. onError('PUT', 404)
  97. onError('PUT', 500)
  98. onError('PUT', 699)
  99. done()
  100. let count = 0
  101. function onError(method, code) {
  102. let xhr = new XMLHttpRequest()
  103. xhr.open(method, `${TEST_SERVER_URL}/xhr-code/${code}`)
  104. xhr.onreadystatechange = function() {
  105. count++
  106. report(
  107. <Assert
  108. key={`#${count} response data of ${method} ${code} should be empty`}
  109. expect=""
  110. actual={xhr.response}/>,
  111. <Assert
  112. key={`#${count} status of ${method} ${code} should be ${code}`}
  113. expect={code}
  114. actual={xhr.status}/>
  115. )
  116. }
  117. xhr.onerror = function() {
  118. report(
  119. <Assert
  120. key={'HTTP error should not throw error event'}
  121. expect={false}
  122. actual={true}/>)
  123. }
  124. xhr.send()
  125. }
  126. })
  127. describe('request headers records should be cleared by open()', (report, done) => {
  128. let xhr = new XMLHttpRequest()
  129. xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
  130. xhr.setRequestHeader('value', '100')
  131. xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
  132. xhr.setRequestHeader('value', '200')
  133. xhr.send()
  134. xhr.onreadystatechange = function() {
  135. if(this.readyState == 4) {
  136. report(<Assert key="headers should be cleared by open()"
  137. expect={'200'}
  138. actual={this.response['value']}/>)
  139. done()
  140. }
  141. }
  142. })
  143. /**
  144. * {@link https://github.com/w3c/web-platform-tests/blob/master/XMLHttpRequest/setrequestheader-bogus-name.htm}
  145. */
  146. describe('invalid characters should not exists in header field', (report, done) => {
  147. function try_name(name) {
  148. try {
  149. let client = new XMLHttpRequest()
  150. client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
  151. client.setRequestHeader(name, '123')
  152. } catch(err) {
  153. report(
  154. <Assert key={`invalid header type ${name} should throw SyntaxError`}
  155. actual={/syntaxerror/i.test(err)}
  156. expect={true}
  157. />)
  158. }
  159. }
  160. function try_byte_string(name) {
  161. try {
  162. let client = new XMLHttpRequest()
  163. client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
  164. client.setRequestHeader(name, '123')
  165. } catch(err) {
  166. report(
  167. <Assert key={`invalid header field ${name} type should throw TypeError`}
  168. actual={/typeerror/i.test(err)}
  169. expect={true}
  170. />)
  171. }
  172. }
  173. var invalid_headers = ["(", ")", "<", ">", "@", ",", ";", ":", "\\",
  174. "\"", "/", "[", "]", "?", "=", "{", "}", " ",
  175. "\u007f", "", "t\rt", "t\nt", "t: t", "t:t",
  176. "t<t", "t t", " tt", ":tt", "\ttt", "\vtt", "t\0t",
  177. "t\"t", "t,t", "t;t", "()[]{}", "a?B", "a=B"]
  178. var invalid_byte_strings = ["テスト", "X-テスト"]
  179. for (var i = 0; i < 32; ++i) {
  180. invalid_headers.push(String.fromCharCode(i))
  181. }
  182. for (var i = 0; i < invalid_headers.length; ++i) {
  183. try_name(invalid_headers[i])
  184. }
  185. for (var i = 0; i < invalid_byte_strings.length; ++i) {
  186. try_byte_string(invalid_byte_strings[i])
  187. }
  188. done()
  189. })
  190. describe('invoke setRequestHeader() before open()', (report, done) => {
  191. try {
  192. let xhr = new XMLHttpRequest()
  193. xhr.setRequestHeader('foo', 'bar')
  194. } catch(err) {
  195. report(
  196. <Info key="error message">
  197. <Text>{err}</Text>
  198. </Info>,
  199. <Assert key="should throw InvalidStateError"
  200. expect={true}
  201. actual={/invalidstateerror/i.test(err)}/>)
  202. done()
  203. }
  204. })
  205. describe('upload progress event test', (report, done) => {
  206. let xhr = new XMLHttpRequest()
  207. let time = Date.now()
  208. let msg = `time=${time}`
  209. xhr.upload.onprogress = function(e) {
  210. report(
  211. <Assert key="event object is an instance of ProgressEvent"
  212. expect={true}
  213. actual={e instanceof ProgressEvent}/>)
  214. }
  215. xhr.onreadystatechange = function() {
  216. if(this.readyState == XMLHttpRequest.DONE) {
  217. report(
  218. <Assert key="reponse should correct"
  219. expect={time}
  220. actual={parseInt(xhr.response.time)}/>,
  221. <Assert key="responseType should correct"
  222. expect={'json'}
  223. actual={xhr.responseType}/>)
  224. done()
  225. }
  226. }
  227. xhr.open('POST', `${TEST_SERVER_URL}/upload`)
  228. xhr.overrideMimeType('application/x-www-form-urlencoded')
  229. xhr.send(msg)
  230. })
  231. describe('timeout event catchable', (report, done) => {
  232. let xhr = new XMLHttpRequest()
  233. let count = 0
  234. xhr.timeout = 1
  235. xhr.ontimeout = function() {
  236. report(
  237. <Info key="event should only trigger once" uid="1000">
  238. <Text>{count}</Text>
  239. </Info>,
  240. <Assert key="event catchable"
  241. expect={true}
  242. actual={true}/>)
  243. done()
  244. }
  245. xhr.open('GET', `${TEST_SERVER_URL}/timeout/`)
  246. xhr.send()
  247. })
  248. describe('upload progress event should not be triggered when body is empty', (report, done) => {
  249. let xhr = new XMLHttpRequest()
  250. let count = 0
  251. xhr.upload.onloadstart = function() {
  252. report(
  253. <Assert key="loadstart event should not triggered"
  254. uid="aaa"
  255. expect={true}
  256. actual={false}/>)
  257. }
  258. xhr.upload.onprogress = function() {
  259. report(
  260. <Assert key="progress event should not triggered"
  261. uid="bbb"
  262. expect={true}
  263. actual={false}/>)
  264. }
  265. xhr.onreadystatechange = function() {
  266. if(this.readyState == XMLHttpRequest.DONE) {
  267. count++
  268. report(
  269. <Assert key="Great! upload event not triggered"
  270. uid="ccc"
  271. expect={true}
  272. actual={true}/>,
  273. <Assert key="This should not triggered multiple times"
  274. uid="ddd"
  275. expect={1}
  276. actual={count}/>)
  277. done()
  278. }
  279. }
  280. xhr.open('GET', `${TEST_SERVER_URL}/pulbic/github.png`)
  281. xhr.send()
  282. })