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