request.test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
  2. const isAndroid = platformInfo.startsWith('android')
  3. const isIOS = platformInfo.startsWith('ios')
  4. const isHarmony = platformInfo.startsWith('harmony')
  5. const isWeb = platformInfo.startsWith('web')
  6. const isMp = platformInfo.startsWith('mp')
  7. const isAppWebView = process.env.UNI_AUTOMATOR_APP_WEBVIEW == 'true'
  8. const PAGE_PATH = '/pages/API/request/request'
  9. const methodMap = {
  10. "GET": "/api/http/method/get",
  11. "POST": "/api/http/method/post",
  12. "PUT": "/api/http/method/put",
  13. "DELETE": "/api/http/method/delete",
  14. "PATCH": "/api/http/method/patch",
  15. "OPTIONS": "/api/http/method/options",
  16. "HEAD": "/api/http/method/head"
  17. }
  18. async function request(page, method, header, data, url) {
  19. if (url == null) {
  20. url = methodMap[method]
  21. }
  22. await page.setData({
  23. url: url,
  24. method: method,
  25. data: data,
  26. header: header
  27. })
  28. res = await page.callMethod('jest_request')
  29. await page.waitFor(2000);
  30. res = await page.data('jest_result');
  31. expect(res).toBe(true)
  32. }
  33. describe('ExtApi-Request', () => {
  34. let page;
  35. let res;
  36. beforeAll(async () => {
  37. page = await program.reLaunch(PAGE_PATH)
  38. await page.waitFor('view');
  39. });
  40. beforeEach(async () => {
  41. await page.setData({
  42. jest_result: false,
  43. data: null,
  44. header: null
  45. })
  46. });
  47. it('Check GET', async () => {
  48. await request(page, 'GET');
  49. });
  50. it('Check POST ContentType Json', async () => {
  51. await request(page, 'POST', {
  52. "Content-Type": "application/json"
  53. }, {
  54. "hello": "world"
  55. }, "/api/http/contentType/json");
  56. });
  57. it('Check POST ContentType Form', async () => {
  58. await request(page, 'POST', {
  59. "Content-Type": "application/x-www-form-urlencoded"
  60. }, "hello=world", "/api/http/contentType/xWwwFormUrlencoded");
  61. });
  62. it('Check PUT', async () => {
  63. await request(page, 'PUT');
  64. });
  65. it('Check DELETE', async () => {
  66. await request(page, 'DELETE');
  67. });
  68. // 鸿蒙平台暂不支持PATCH方法
  69. if (!isHarmony) {
  70. it('Check PATCH', async () => {
  71. await request(page, 'PATCH');
  72. });
  73. }
  74. if (process.env.uniTestPlatformInfo.indexOf('web') === -1) {
  75. it('Check OPTIONS', async () => {
  76. await request(page, 'OPTIONS');
  77. });
  78. }
  79. it('Check HEAD', async () => {
  80. await request(page, 'HEAD');
  81. });
  82. it('Request with timeout null', async () => {
  83. res = await page.callMethod('jest_timeout_null')
  84. await page.waitFor(2000);
  85. res = await page.data('jest_result');
  86. expect(res).toBe(true)
  87. });
  88. it('Get Array', async () => {
  89. res = await page.callMethod('jest_get_array')
  90. await page.waitFor(2000);
  91. res = await page.data('jest_result');
  92. expect(res).toBe(true)
  93. })
  94. let shouldTestCookie = false
  95. if (isAndroid && !isAppWebView) {
  96. let version = process.env.uniTestPlatformInfo
  97. version = parseInt(version.split(" ")[1])
  98. shouldTestCookie = version > 9
  99. }
  100. if (isIOS && !isAppWebView) {
  101. shouldTestCookie = true
  102. }
  103. if (shouldTestCookie) {
  104. it('Check Set Cookie', async () => {
  105. res = await page.callMethod('jest_set_cookie')
  106. await page.waitFor(2000);
  107. res = await page.data('jest_result');
  108. expect(res).toBe(true)
  109. });
  110. it('Check Delete Cookie', async () => {
  111. res = await page.callMethod('jest_delete_cookie')
  112. await page.waitFor(2000);
  113. res = await page.data('jest_result');
  114. expect(res).toBe(true)
  115. });
  116. it('Check Set Cookie Expires', async () => {
  117. await page.callMethod('jest_set_cookie_expires')
  118. await page.waitFor(2000);
  119. res = await page.data('jest_result_data');
  120. console.log("request expires cookie data :", res);
  121. res = await page.data('jest_result');
  122. expect(res).toBe(true)
  123. await page.setData({
  124. jest_result: false,
  125. jest_result_data: "",
  126. data: null,
  127. header: null
  128. })
  129. await page.waitFor(5000);
  130. await page.callMethod('jest_cookie_request', false)
  131. await page.waitFor(2000);
  132. res = await page.data('jest_result_data');
  133. console.log("verify request data :", res);
  134. res = await page.data('jest_result');
  135. expect(res).toBe(true)
  136. });
  137. }
  138. it('Check Get With Data', async () => {
  139. res = await page.callMethod('jest_get_with_data')
  140. await page.waitFor(2000);
  141. res = await page.data('jest_result');
  142. expect(res).toBe(true)
  143. })
  144. it('Check Get With Generics', async () => {
  145. res = await page.callMethod('jest_get_with_generics')
  146. await page.waitFor(2000);
  147. res = await page.data('jest_result');
  148. expect(res).toBe(true)
  149. })
  150. // 15以下的模拟器所对应的xcode不能编译自定义插件,大于15是因为某台设备,会用xcode14.1跑15.5的设备
  151. let version = process.env.uniTestPlatformInfo
  152. let split = version.split(" ")
  153. version = parseInt(split[split.length - 1])
  154. if (isIOS && version > 15 || isAndroid || isHarmony) {
  155. it('Check Post In UTS Module', async () => {
  156. res = await page.callMethod('jest_uts_module_invoked')
  157. await page.waitFor(2000);
  158. res = await page.data('jest_result');
  159. expect(res).toBe(true)
  160. })
  161. }
  162. it('Check Respone Json String', async () => {
  163. res = await page.callMethod('jest_respone_json_string')
  164. await page.waitFor(2000);
  165. res = await page.data('jest_result');
  166. expect(res).toBe(true)
  167. })
  168. if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('android')) {
  169. it('Check Respone With String Generics', async () => {
  170. res = await page.callMethod('jest_respone_with_string_generics')
  171. await page.waitFor(2000);
  172. res = await page.data('jest_result');
  173. expect(res).toBe(true)
  174. })
  175. }
  176. if (process.env.uniTestPlatformInfo.toLocaleLowerCase().startsWith('android')) {
  177. it('Check Respone string generics when status is 404', async () => {
  178. res = await page.callMethod('jest_respone_with_404_and_string_generics')
  179. await page.waitFor(2000);
  180. res = await page.data('jest_result');
  181. expect(res).toBe(true)
  182. })
  183. }
  184. if (isAndroid || isIOS) {
  185. it('send arraybuffer', async () => {
  186. res = await page.callMethod('sendArrayBuffer', true)
  187. await page.waitFor(5000);
  188. res = await page.data('res');
  189. expect(res).toEqual('请求结果 : 123,34,104,101,108,108,111,34,58,34,119,111,114,108,100,34,125')
  190. })
  191. }
  192. });