wan_http.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. -- 引入必要的库文件(lua编写), 内部库不需要require
  2. sys = require("sys")
  3. sysplus = require("sysplus")
  4. sys.taskInit(function()
  5. sys.wait(3000)
  6. local result = spi.setup(0, -- 串口id
  7. nil, 0, -- CPHA
  8. 0, -- CPOL
  9. 8, -- 数据宽度
  10. 25600000 -- ,--频率
  11. -- spi.MSB,--高低位顺序 可选,默认高位在前
  12. -- spi.master,--主模式 可选,默认主
  13. -- spi.full--全双工 可选,默认全双工
  14. )
  15. log.info("main", "open", result)
  16. if result ~= 0 then -- 返回值为0,表示打开成功
  17. log.info("main", "spi open error", result)
  18. return
  19. end
  20. netdrv.setup(socket.LWIP_ETH, netdrv.CH390, {
  21. spiid = 0,
  22. cs = 8
  23. })
  24. netdrv.dhcp(socket.LWIP_ETH, true)
  25. -- sys.wait(3000)
  26. while 1 do
  27. local ipv4ip, aaa, bbb = netdrv.ipv4(socket.LWIP_ETH, "", "", "")
  28. log.info("ipv4地址,掩码,网关为", ipv4ip, aaa, bbb)
  29. local netdrv_start = netdrv.ready(socket.LWIP_ETH)
  30. if netdrv_start and ipv4ip and ipv4ip ~= "0.0.0.0" then
  31. log.info("条件都满足")
  32. sys.publish("CH390_IP_READY")
  33. return
  34. end
  35. sys.wait(1000)
  36. end
  37. end)
  38. local url = "http://httpbin.air32.cn"
  39. -- 读取证书和私钥的函数
  40. local function read_file(file_path)
  41. local file, err = io.open(file_path, "r")
  42. if not file then
  43. error("Failed to open file: " .. err)
  44. end
  45. local data = file:read("*a")
  46. file:close()
  47. return data
  48. end
  49. -- 读取证书和私钥
  50. local ca_server = read_file("/luadb/ca.crt") -- 服务器 CA 证书数据
  51. local ca_client = read_file("/luadb/client.crt") -- 客户端 CA 证书数据
  52. local client_private_key_encrypts_data = read_file("/luadb/client.key") -- 客户端私钥
  53. local client_private_key_password = "123456789" -- 客户端私钥口令
  54. -- 测试用例函数
  55. local function run_tests()
  56. log.info("启动HTTP多项测试")
  57. local tests = { -- GET 方法测试
  58. {
  59. method = "GET",
  60. url = url .. "/get",
  61. headers = nil,
  62. body = nil,
  63. opts = {
  64. adapter = socket.LWIP_ETH
  65. },
  66. expected_code = 200,
  67. description = "GET方法,无请求头、body以及额外的附加数据"
  68. }, {
  69. method = "GET",
  70. url = url .. "/get",
  71. headers = {
  72. ["Content-Type"] = "application/json"
  73. },
  74. body = nil,
  75. opts = {
  76. adapter = socket.LWIP_ETH
  77. },
  78. expected_code = 200,
  79. description = "GET方法,有请求头,无body和额外的附加数据"
  80. }, {
  81. method = "GET",
  82. url = url .. "/get",
  83. headers = nil,
  84. body = nil,
  85. opts = {
  86. adapter = socket.LWIP_ETH,
  87. timeout = 5000,
  88. debug = true
  89. },
  90. expected_code = 200,
  91. description = "GET方法,无请求头,无body,超时时间5S,debug日志打开"
  92. }, -- GET 方法测试(下载 2K 小文件)
  93. {
  94. method = "GET",
  95. url = "http://airtest.openluat.com:2900/download/2K", -- 返回 2K 字节的文件
  96. headers = nil,
  97. body = nil,
  98. opts = {
  99. adapter = socket.LWIP_ETH,
  100. },
  101. expected_code = 200,
  102. description = "GET 2K小文件下载"
  103. }, {
  104. method = "GET",
  105. url = "http://invalid-url",
  106. headers = nil,
  107. body = nil,
  108. opts = {
  109. adapter = socket.LWIP_ETH,
  110. },
  111. expected_code = -4,
  112. description = "GET方法,无效的url,域名"
  113. }, {
  114. method = "GET",
  115. -- url = "http://invalid-url",
  116. url = "192.168.1",
  117. headers = nil,
  118. body = nil,
  119. opts = {
  120. adapter = socket.LWIP_ETH,
  121. },
  122. expected_code = -4,
  123. description = "GET方法,无效的url,IP地址"
  124. }, -- POST 方法测试
  125. {
  126. method = "POST",
  127. url = url .. "/post",
  128. headers = {
  129. ["Content-Type"] = "application/json"
  130. },
  131. body = '{"key": "value"}',
  132. opts = {
  133. adapter = socket.LWIP_ETH,
  134. },
  135. expected_code = 200,
  136. description = "POST方法,有请求头,body为json"
  137. }, {
  138. method = "POST",
  139. url = url .. "/post",
  140. headers = nil,
  141. body = nil,
  142. opts = {
  143. adapter = socket.LWIP_ETH,
  144. },
  145. expected_code = 200,
  146. description = "POST方法,无请求头,无body,无额外的数据"
  147. }, {
  148. method = "POST",
  149. url = "clahflkjfpsjvlsvnlohvioehoi",
  150. headers = nil,
  151. body = nil,
  152. opts = {
  153. adapter = socket.LWIP_ETH,
  154. },
  155. expected_code = -4,
  156. description = "POST方法,无效的url,域名"
  157. }, {
  158. method = "POST",
  159. -- url = "http://invalid-url",
  160. url = "192.168.1",
  161. headers = nil,
  162. body = nil,
  163. opts = {
  164. adapter = socket.LWIP_ETH,
  165. },
  166. expected_code = -4,
  167. description = "POST方法,无效的url,IP地址"
  168. }, -- POST 方法测试(上传 30K 大文件)
  169. {
  170. method = "POST",
  171. url = url .. "/post",
  172. headers = {
  173. ["Content-Type"] = "application/octet-stream"
  174. },
  175. body = string.rep("A", 30 * 1024), -- 生成 30K 的数据
  176. opts = {
  177. adapter = socket.LWIP_ETH,
  178. },
  179. expected_code = 200,
  180. description = "POST 30K大数据上传"
  181. }, {
  182. method = "POST",
  183. url = url .. "/post",
  184. headers = {
  185. ["Content-Type"] = "application/x-www-form-urlencoded"
  186. },
  187. body = "key=value",
  188. opts = {
  189. adapter = socket.LWIP_ETH,
  190. timeout = 5000
  191. },
  192. expected_code = 200,
  193. description = "POST方法,有请求头,body为json,超时时间5S"
  194. }, -- HTTPS GET 方法测试(双向认证)
  195. {
  196. method = "GET",
  197. url = url .. "/get",
  198. headers = nil,
  199. body = nil,
  200. opts = {
  201. adapter = socket.LWIP_ETH,
  202. ca = ca_server,
  203. client_cert = ca_client,
  204. client_key = client_private_key_encrypts_data,
  205. client_key_password = client_private_key_password
  206. },
  207. expected_code = 200,
  208. description = "HTTPS GET方法,无请求头,无body,带双向认证的服务器证书、客户端证书、客户端key,客户端password"
  209. }, -- HTTPS POST 方法测试(双向认证)
  210. {
  211. method = "POST",
  212. url = url .. "/post",
  213. headers = {
  214. ["Content-Type"] = "application/json"
  215. },
  216. body = '{"key": "value"}',
  217. opts = {
  218. adapter = socket.LWIP_ETH,
  219. ca = ca_server,
  220. client_cert = ca_client,
  221. client_key = client_private_key_encrypts_data,
  222. client_key_password = client_private_key_password
  223. },
  224. expected_code = 200,
  225. description = "HTTPS POST方法,有请求头,body为json,带双向认证的服务器证书、客户端证书、客户端key,客户端password"
  226. }}
  227. for i, test in ipairs(tests) do
  228. log.info("HTTP第" .. i .. "次测试")
  229. -- sys.wait(5000)
  230. local code, headers, body = http.request(test.method, test.url, test.headers, test.body, test.opts).wait()
  231. -- 调试输出
  232. print(string.format("Test %d: %s", i, test.description))
  233. print("Returned values: code =", code, ", headers =", headers, ", body =", body)
  234. -- 验证返回值
  235. if code == test.expected_code then
  236. log.info("本次测试的是", test.description, "code 符合预期结果 code = ", code)
  237. else
  238. log.info("本次测试的是", test.description, "预期结果为", test.expected_code,
  239. "code 不符合预期结果 code =", code)
  240. end
  241. end
  242. end
  243. -- 运行测试用例
  244. sys.taskInit(function()
  245. sys.waitUntil("CH390_IP_READY")
  246. log.info("CH390 联网成功,开始测试")
  247. socket.dft(socket.LWIP_ETH)
  248. -- 如果自带的DNS不好用,可以用下面的公用DNS,但是一定是要在CH390联网成功后使用
  249. -- socket.setDNS(socket.LWIP_ETH,1,"223.5.5.5")
  250. -- socket.setDNS(nil,1,"114.114.114.114")
  251. run_tests()
  252. end)