test.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. local https = require "https"
  2. local json
  3. -- Helper
  4. local function hexencode(c)
  5. return string.format("%%%02x", string.byte(c))
  6. end
  7. local function escape(s)
  8. return (string.gsub(s, "([^A-Za-z0-9_])", hexencode))
  9. end
  10. local function urlencode(list)
  11. local result = {}
  12. for k, v in pairs(list) do
  13. result[#result + 1] = escape(k).."="..escape(v)
  14. end
  15. return table.concat(result, "&")
  16. end
  17. local function checkcode(code, expected)
  18. if code ~= expected then
  19. error("expected code "..expected..", got "..tostring(code))
  20. end
  21. end
  22. math.randomseed(os.time())
  23. -- Tests function
  24. local function test_download_json()
  25. local code, response = https.request("https://raw.githubusercontent.com/rxi/json.lua/master/json.lua")
  26. checkcode(code, 200)
  27. json = assert(loadstring(response, "=json.lua"))()
  28. end
  29. local function test_head()
  30. local code, response = https.request("https://postman-echo.com/get", {method = "HEAD"})
  31. assert(code == 200, "expected code 200, got "..code)
  32. assert(#response == 0, "expected empty response")
  33. end
  34. local function test_custom_header()
  35. local headerName = "RandomNumber"
  36. local random = math.random(1, 1000)
  37. local code, response = https.request("https://postman-echo.com/get", {
  38. headers = {
  39. [headerName] = tostring(random)
  40. }
  41. })
  42. checkcode(code, 200)
  43. local root = json.decode(response)
  44. -- Headers are case-insensitive
  45. local found = false
  46. for k, v in pairs(root.headers) do
  47. if k:lower() == headerName:lower() then
  48. assert(tonumber(v) == random, "random number does not match, expected "..random..", got "..v)
  49. found = true
  50. end
  51. end
  52. assert(found, "custom header RandomNumber not found")
  53. end
  54. local function test_send(method, kind)
  55. local data = {Foo = "Bar", Key = "Value"}
  56. local input, contentType
  57. if kind == "json" then
  58. input = json.encode
  59. contentType = "application/json"
  60. else
  61. input = urlencode
  62. contentType = "application/x-www-form-urlencoded"
  63. end
  64. local code, response = https.request("https://postman-echo.com/"..method:lower(), {
  65. headers = {["Content-Type"] = contentType},
  66. data = input(data),
  67. method = method
  68. })
  69. checkcode(code, 200)
  70. local root = json.decode(response)
  71. for k, v in pairs(data) do
  72. local v0 = assert(root[kind][k], "Missing key "..k.." for "..kind)
  73. assert(v0 == v, "Key "..k.." value mismatch, expected '"..v.."' got '"..v0.."'")
  74. end
  75. end
  76. -- Tests call
  77. print("test downloading json library") test_download_json()
  78. print("test custom header") test_custom_header()
  79. print("test HEAD") test_head()
  80. for _, method in ipairs({"POST", "PUT", "PATCH", "DELETE"}) do
  81. for _, kind in ipairs({"form", "json"}) do
  82. print("test "..method.." with data send as "..kind)
  83. test_send(method, kind)
  84. end
  85. end
  86. print("Test successful!")