Keine Beschreibung

test-xmlhttp.js 8.9KB

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