No Description

test-xmlhttp.js 8.1KB

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