From a0b0d4bde01a9e0b191923423f5df764232a4a50 Mon Sep 17 00:00:00 2001 From: lnpotter Date: Tue, 13 Aug 2024 23:31:06 -0300 Subject: [PATCH] feat: add suport to POST body content in http curl handler --- src/lib/protocol/http_curl.lua | 11 +++++++---- tests/test_lib_protocol_http_curl.lua | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/lib/protocol/http_curl.lua b/src/lib/protocol/http_curl.lua index e2a2d2e..44fef0b 100644 --- a/src/lib/protocol/http_curl.lua +++ b/src/lib/protocol/http_curl.lua @@ -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 @@ -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") diff --git a/tests/test_lib_protocol_http_curl.lua b/tests/test_lib_protocol_http_curl.lua index 5d08466..6557025 100644 --- a/tests/test_lib_protocol_http_curl.lua +++ b/tests/test_lib_protocol_http_curl.lua @@ -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() @@ -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())