暫無描述

test-xmlhttp.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. const { Assert, Comparer, Info, prop } = RNTest
  21. const describe = RNTest.config({
  22. group : 'XMLHttpRequest',
  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. 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. function onError(method, code) {
  101. let xhr = new XMLHttpRequest()
  102. xhr.open(method, `${TEST_SERVER_URL}/xhr-code/${code}`)
  103. xhr.onreadystatechange = function() {
  104. report(
  105. <Assert
  106. key={`response data of ${method} ${code} should be empty`}
  107. expect=""
  108. actual={xhr.response}/>,
  109. <Assert
  110. key={`status of ${method} ${code} should be ${code}`}
  111. expect={code}
  112. actual={xhr.status}/>
  113. )
  114. }
  115. xhr.onerror = function() {
  116. report(
  117. <Assert
  118. key={'HTTP error should not throw error event'}
  119. expect={false}
  120. actual={true}/>)
  121. }
  122. xhr.send()
  123. }
  124. })
  125. describe('request headers records should be cleared by open()', (report, done) => {
  126. let xhr = new XMLHttpRequest()
  127. xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
  128. xhr.setRequestHeader('header-test', '100')
  129. xhr.open('GET', `${TEST_SERVER_URL}/xhr-header`)
  130. xhr.setRequestHeader('header-test', '200')
  131. xhr.send()
  132. xhr.onreadystatechange = function() {
  133. if(this.readyState == 4) {
  134. report(<Assert key="headers should be cleared by open()"
  135. expect={'200'}
  136. actual={this.response['header-test']}/>)
  137. done()
  138. }
  139. }
  140. })
  141. /**
  142. * {@link https://github.com/w3c/web-platform-tests/blob/master/XMLHttpRequest/setrequestheader-bogus-name.htm}
  143. */
  144. describe('invalid characters should not exists in header field', (report, done) => {
  145. function try_name(name) {
  146. try {
  147. let client = new XMLHttpRequest()
  148. client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
  149. client.setRequestHeader(name, '123')
  150. } catch(err) {
  151. report(
  152. <Assert key={`invalid header type ${name} should throw SyntaxError`}
  153. actual={/syntaxerror/i.test(err)}
  154. expect={true}
  155. />)
  156. }
  157. }
  158. function try_byte_string(name) {
  159. try {
  160. let client = new XMLHttpRequest()
  161. client.open("GET", `${TEST_SERVER_URL}/public/github.png`)
  162. client.setRequestHeader(name, '123')
  163. } catch(err) {
  164. report(
  165. <Assert key={`invalid header field ${name} type should throw TypeError`}
  166. actual={/typeerror/i.test(err)}
  167. expect={true}
  168. />)
  169. }
  170. }
  171. var invalid_headers = ["(", ")", "<", ">", "@", ",", ";", ":", "\\",
  172. "\"", "/", "[", "]", "?", "=", "{", "}", " ",
  173. /* HT already tested in the loop below */
  174. "\u007f", "", "t\rt", "t\nt", "t: t", "t:t",
  175. "t<t", "t t", " tt", ":tt", "\ttt", "\vtt", "t\0t",
  176. "t\"t", "t,t", "t;t", "()[]{}", "a?B", "a=B"]
  177. var invalid_byte_strings = ["テスト", "X-テスト"]
  178. for (var i = 0; i < 32; ++i) {
  179. invalid_headers.push(String.fromCharCode(i))
  180. }
  181. for (var i = 0; i < invalid_headers.length; ++i) {
  182. try_name(invalid_headers[i])
  183. }
  184. for (var i = 0; i < invalid_byte_strings.length; ++i) {
  185. try_byte_string(invalid_byte_strings[i])
  186. }
  187. done()
  188. })
  189. describe('invoke setRequestHeader() before open()', (report, done) => {
  190. try {
  191. new XMLHttpRequest().setRequestHeader('foo', 'bar')
  192. } catch(err) {
  193. report(<Assert key="should throw InvalidStateRrror"
  194. expect={true}
  195. actual={/invalidstateerror/i.test(err)}/>)
  196. done()
  197. }
  198. })