Skip to content

Instantly share code, notes, and snippets.

@rocklau
Created March 22, 2019 00:55
Show Gist options
  • Save rocklau/b3af047750f4f38cdcc917ed6a992431 to your computer and use it in GitHub Desktop.
Save rocklau/b3af047750f4f38cdcc917ed6a992431 to your computer and use it in GitHub Desktop.
httpbin test for freeswitch curl
json = freeswitch.JSON()
curl = freeswitch.cURL()
GET ='get'
POST ='post'
PUT = 'put'
DELETE ='delete'
CONTENT_TYPE ='application/json'
proto = "http://"
domain = "httpbin.org"
do_debug = false
function kvlog(k, v)
if not do_debug then
return
end
if v == nil then
v = k
k = ''
end
if not v then
v = "__nil__"
end
if type(v) == "boolean" then
v = serialize(v)
end
freeswitch.consoleLog("INFO", k .. ' ' .. v .. "\n")
end
function do_assert(test_name, test_result)
kvlog(test_name)
assert(test_result)
end
function serialize(o)
s = ""
if type(o) == "number" then
s = s .. o
elseif type(o) == "string" then
s = s .. string.format("%q", o)
elseif type(o) == "table" then
s = s .. "{\n"
for k, v in pairs(o) do
s = s .. " " .. k .. " = "
s = s .. serialize(v)
s = s .. ",\n"
end
s = s .. "}"
elseif type(o) == "boolean" then
if o then
s = s .. "true"
else
s = s .. "false"
end
else
s = s .. " [" .. type(o) .. "]"
end
return s
end
local methods = {
[GET] = function(url)
return curl:get(url)
end,
[POST] = function(url)
return curl:post(url)
end,
[PUT] = function(url)
return curl:put(url)
end,
[DELETE] = function(url)
return curl:delete(url)
end
}
function fs_cURL(method,path,data)
curl:reset()
local url = proto .. domain .. path
local call = methods[method]
if data then
ret = call({url=url,data=data,dataType=CONTENT_TYPE})
else
ret = call({url=url,dataType=CONTENT_TYPE})
end
kvlog('url', url)
--kvlog(curl:get_body())
--kvlog('response code ' .. ret.code)
do_assert('response status code should be 200', ret.code == 200)
local body = curl:get_body()
curl:reset()
return body
end
function os_cURL(method,path,data)
local url = proto .. domain .. path
request_data = ''
kvlog('data type ',type(data))
if type(data) == 'string' then
request_data = ' -d "' .. data:gsub('%"','\\"') ..'" '
elseif type(data) == 'table' then
request_data = ' -d "' .. json:encode(data):gsub('%"','\\"') ..'" '
end
exe = io.popen('curl --silent -X '.. method .. ' "'.. url .. '" -H "Content-Type: '.. CONTENT_TYPE .. '" '.. request_data .. '', 'r')
local response_data = exe:read("*all")
exe.close()
return response_data
end
function get_json_field_str(str)
local json_field = json:decode(str).json
if json_field then
return json:encode(json_field)
end
if do_debug then
kvlog('assert failure: no return json field')
end
return str
end
function get_url_field_str(str)
kvlog(str)
return json:decode(str).url
end
function assert_request_json(method,path,data)
kvlog('new test!!!')
kvlog('(A) - should be')
local should_value = get_json_field_str(os_cURL(method,path,data))
kvlog(should_value)
kvlog('(B) - test result')
local test_value = get_json_field_str(fs_cURL(method,path,data))
kvlog(test_value)
kvlog('-------')
do_assert(method .. ' request response json should be same', test_value == should_value)
end
function assert_get_json(method,path,data)
kvlog('new test get !!!')
kvlog('(A) - should be')
local should_url = get_url_field_str(os_cURL(method,path,data))
kvlog(should_url)
kvlog('(B) - test result')
local test_url = get_url_field_str(fs_cURL(method,path,data))
kvlog(test_url)
kvlog('-------')
do_assert('get response url field in json should be same', test_url == should_url)
end
kvlog('test start')
do_debug =true
assert_request_json(POST,"/post",{a=1,b={c=2,d="this is post"}})
assert_request_json(PUT,"/put",'{"a":1,"b":{"c":"this is put"}}')
assert_get_json(GET,"/get")
assert_request_json(PUT,"/put",{a=1,b={c=2}})
assert_request_json(POST,"/post",'{"a":1,"b":{"c":2}}')
assert_get_json(GET,"/get")
kvlog('test end')
stream:write("+OK")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment