air_http.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. local air_http = {}
  2. local test_type = "http_test"
  3. local url = "http://httpbin.air32.cn"
  4. -- 读取证书和私钥的函数
  5. local function read_file(file_path)
  6. local file, err = io.open(file_path, "r")
  7. if not file then
  8. error("Failed to open file: " .. err)
  9. end
  10. local data = file:read("*a")
  11. file:close()
  12. return data
  13. end
  14. -- 读取证书和私钥
  15. local ca_server = read_file("/luadb/http_ca.crt") -- 服务器 CA 证书数据
  16. local ca_client = read_file("/luadb/http_client.crt") -- 客户端 CA 证书数据
  17. local client_private_key_encrypts_data = read_file("/luadb/http_client.key") -- 客户端私钥
  18. local client_private_key_password = "123456789" -- 客户端私钥口令
  19. -- 测试用例函数
  20. function air_http.run_tests()
  21. -- sys.waitUntil("net_ready")
  22. local tests = { -- GET 方法测试
  23. {
  24. method = "GET",
  25. url = url .. "/get",
  26. headers = nil,
  27. body = nil,
  28. opts = {},
  29. expected_code = 200,
  30. description = "GET方法,无请求头、body以及额外的附加数据"
  31. }, {
  32. method = "GET",
  33. url = url .. "/get",
  34. headers = {
  35. ["Content-Type"] = "application/json"
  36. },
  37. body = nil,
  38. opts = {},
  39. expected_code = 200,
  40. description = "GET方法,有请求头,无body和额外的附加数据"
  41. }, {
  42. method = "GET",
  43. url = url .. "/get",
  44. headers = nil,
  45. body = nil,
  46. opts = {
  47. timeout = 5000,
  48. debug = true
  49. },
  50. expected_code = 200,
  51. description = "GET方法,无请求头,无body,超时时间5S,debug日志打开"
  52. }, -- GET 方法测试(下载 2K 小文件)
  53. {
  54. method = "GET",
  55. url = "http://airtest.openluat.com:2900/download/2K", -- 返回 2K 字节的文件
  56. headers = nil,
  57. body = nil,
  58. opts = {},
  59. expected_code = 200,
  60. description = "GET方法 2K小文件下载"
  61. }, {
  62. method = "GET",
  63. url = "http://invalid-url",
  64. headers = nil,
  65. body = nil,
  66. opts = {},
  67. expected_code = -4,
  68. description = "GET方法,无效的url,域名"
  69. }, {
  70. method = "GET",
  71. -- url = "http://invalid-url",
  72. url = "192.168.1",
  73. headers = nil,
  74. body = nil,
  75. opts = {},
  76. expected_code = -4,
  77. description = "GET方法,无效的url,IP地址"
  78. }, -- POST 方法测试
  79. {
  80. method = "POST",
  81. url = url .. "/post",
  82. headers = {
  83. ["Content-Type"] = "application/json"
  84. },
  85. body = '{"key": "value"}',
  86. opts = {},
  87. expected_code = 200,
  88. description = "POST方法,有请求头,body为json"
  89. }, {
  90. method = "POST",
  91. url = url .. "/post",
  92. headers = nil,
  93. body = nil,
  94. opts = {},
  95. expected_code = 200,
  96. description = "POST方法,无请求头,无body,无额外的数据"
  97. }, {
  98. method = "POST",
  99. url = "clahflkjfpsjvlsvnlohvioehoi",
  100. headers = nil,
  101. body = nil,
  102. opts = {},
  103. expected_code = -4,
  104. description = "POST方法,无效的url,域名"
  105. }, {
  106. method = "POST",
  107. -- url = "http://invalid-url",
  108. url = "192.168.1",
  109. headers = nil,
  110. body = nil,
  111. opts = {},
  112. expected_code = -4,
  113. description = "POST方法,无效的url,IP地址"
  114. }, -- POST 方法测试(上传 30K 大文件)
  115. {
  116. method = "POST",
  117. url = url .. "/post",
  118. headers = {
  119. ["Content-Type"] = "application/octet-stream"
  120. },
  121. body = string.rep("A", 30 * 1024), -- 生成 30K 的数据
  122. opts = {},
  123. expected_code = 200,
  124. description = "POST 30K大数据上传"
  125. }, {
  126. method = "POST",
  127. url = url .. "/post",
  128. headers = {
  129. ["Content-Type"] = "application/x-www-form-urlencoded"
  130. },
  131. body = "key=value",
  132. opts = {
  133. timeout = 5000
  134. },
  135. expected_code = 200,
  136. description = "POST方法,有请求头,body为json,超时时间5S"
  137. }, -- HTTPS GET 方法测试(双向认证)
  138. {
  139. method = "GET",
  140. url = url .. "/get",
  141. headers = nil,
  142. body = nil,
  143. opts = {
  144. ca = ca_server,
  145. client_cert = ca_client,
  146. client_key = client_private_key_encrypts_data,
  147. client_key_password = client_private_key_password
  148. },
  149. expected_code = 200,
  150. description = "HTTPS GET方法,无请求头,无body,带双向认证的服务器证书、客户端证书、客户端key,客户端password"
  151. }, -- HTTPS POST 方法测试(双向认证)
  152. {
  153. method = "POST",
  154. url = url .. "/post",
  155. headers = {
  156. ["Content-Type"] = "application/json"
  157. },
  158. body = '{"key": "value"}',
  159. opts = {
  160. ca = ca_server,
  161. client_cert = ca_client,
  162. client_key = client_private_key_encrypts_data,
  163. client_key_password = client_private_key_password
  164. },
  165. expected_code = 200,
  166. description = "HTTPS POST方法,有请求头,body为json,带双向认证的服务器证书、客户端证书、客户端key,客户端password"
  167. }}
  168. local tests_len = #tests
  169. local len = 0
  170. local pass = true
  171. local failing_item = nil
  172. local test_mode
  173. for i, test in ipairs(tests) do
  174. len = len + 1
  175. lcd.clear()
  176. local code, headers, body = http.request(test.method, test.url, test.headers, test.body, test.opts).wait()
  177. -- 调试输出
  178. print(string.format("Test %d: %s", i, test.description))
  179. print("Returned values: code =", code, ", headers =", headers, ", body =", body)
  180. -- 验证返回值
  181. if code == test.expected_code then
  182. log.info("本次测试的是", test.description, "code 符合预期结果 code = ", code)
  183. -- lcd.drawStr(50,50,"testrady")
  184. lcd.drawStr(10, 200,
  185. string.format("本次测试的是%s,code符合预期结果code=%d", test.description, code))
  186. else
  187. log.info("本次测试的是", test.description, "预期结果为", test.expected_code,
  188. "code 不符合预期结果 code =", code)
  189. lcd.drawStr(50, 50,
  190. string.format("本次测试的是%s,code不符合预期结果code=%d", test.description, code))
  191. pass = false
  192. failing_item = test.description
  193. -- break
  194. end
  195. sys.wait(5000)
  196. --http所有测试项目完成
  197. if len == tests_len then
  198. lcd.clear()
  199. log.info("测试结束")
  200. lcd.drawStr(50, 50, "测试结束")
  201. if pass then
  202. test_mode = "测试所有项目通过"
  203. log.info(test_mode)
  204. lcd.drawStr(70, 40, test_mode)
  205. else
  206. test_mode = "测试有项目未通过"
  207. log.info("测试有项目未通过")
  208. lcd.drawStr(70, 60, "测试有项目未通过")
  209. lcd.drawStr(100, 80, "失败的项目是:" .. failing_item)
  210. end
  211. local data = {
  212. type = test_type,
  213. test_mode = test_mode,
  214. test_end = pass or failing_item,
  215. fail_res = pass or "code不符合预期结果code=" .. code,
  216. imei = mobile.imei()
  217. }
  218. local data = json.encode(data)
  219. sys.publish("OTHER_FILE_SENDMSG",data)
  220. -- sys.wait(5000)
  221. break
  222. end
  223. end
  224. end
  225. return air_http