Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add suport to POST body content in http curl handler #49

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/lib/protocol/http_curl.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
--! @todo support Body content in post
local http_util = require('src/lib/util/http')

local function http_handler(self)
local index = 1
local cmd = 'curl -L \x2D\x2Dsilent \x2D\x2Dinsecure -w "\n%{http_code}" '
local protocol = self.method == 'HEAD' and '\x2D\x2DHEAD' or '-X '..self.method
local protocol = self.method == 'HEAD' and '\x2D\x2DHEAD' or '-X ' .. self.method
local params = http_util.url_search_param(self.param_list, self.param_dict)

local headers, index = ' ', 1
Expand All @@ -16,8 +15,12 @@ local function http_handler(self)
index = index + 1
end


local handle = io and io.popen and io.popen(cmd..protocol..headers..self.url..params)
local body = ''
if self.method == 'POST' and self.body_content then
body = '-d \''..self.body_content..'\' '
end

local handle = io and io.popen and io.popen(cmd..protocol..headers..body..self.url..params)

if handle then
local stdout = handle:read("*a")
Expand Down
21 changes: 21 additions & 0 deletions tests/test_lib_protocol_http_curl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ local mock_popen = mock_io.open({
read=function () return '' end,
close=function () return false, 'no URL specified!' end
},
['curl -L --silent --insecure -w "\n%{http_code}" -X POST -d \'UPPERCASE_CONTENT\' pudim.com.br'] = {
read=function () return 'uppercase_content\n201' end,
close=function () return true, nil end
},
})

function test_http_get_200()
Expand Down Expand Up @@ -109,4 +113,21 @@ function test_http_popen_error()
luaunit.assertEquals(std.http.body, nil)
end

function test_http_post_with_body()
local std = {http={}}
io.popen = mock_popen

protocol_http.handler({
std = std,
url = 'pudim.com.br',
method = 'POST',
body_content = 'UPPERCASE_CONTENT'
})

luaunit.assertEquals(std.http.ok, true)
luaunit.assertEquals(std.http.error, nil)
luaunit.assertEquals(std.http.status, 201)
luaunit.assertEquals(std.http.body, 'uppercase_content')
end

os.exit(luaunit.LuaUnit.run())
Loading