diff --git a/kong/plugins/httplog/log.lua b/kong/plugins/httplog/log.lua index f93a591091..e2d63a025a 100644 --- a/kong/plugins/httplog/log.lua +++ b/kong/plugins/httplog/log.lua @@ -15,9 +15,9 @@ local function generate_post_payload(method, parsed_url, message) return payload end --- Parse host url . +-- Parse host url -- @param `url` host url --- @return a table with host details like domain name, port, path etc +-- @return `parsed_url` a table with host details like domain name, port, path etc local function parse_url(host_url) local parsed_url = url.parse(host_url) if not parsed_url.port then @@ -27,6 +27,9 @@ local function parse_url(host_url) parsed_url.port = 443 end end + if not parsed_url.path then + parsed_url.path = "/" + end return parsed_url end diff --git a/spec/plugins/logging_spec.lua b/spec/plugins/logging_spec.lua index d4adbfef53..36e852cecc 100644 --- a/spec/plugins/logging_spec.lua +++ b/spec/plugins/logging_spec.lua @@ -14,6 +14,8 @@ local TEST_CONF = "kong_TEST.yml" local TCP_PORT = 7777 local UDP_PORT = 8888 +local HTTP_PORT = 8090 +local HTTPS_PORT = 443 describe("Logging Plugins", function() @@ -26,6 +28,7 @@ describe("Logging Plugins", function() plugin_configuration = { { name = "tcplog", value = { host = "127.0.0.1", port = 7777 }, __api = 1 }, { name = "udplog", value = { host = "127.0.0.1", port = 8888 }, __api = 1 }, + { name = "httplog", value = { http_endpoint = "http://localhost:8090", method = "POST"}, __api = 1 }, { name = "filelog", value = {}, __api = 1 } } } @@ -73,6 +76,41 @@ describe("Logging Plugins", function() assert.are.same("127.0.0.1", log_message.ip) end) + it("should log to Http", function() + local thread = spec_helper.start_http_server(HTTP_PORT) -- Starting the mock TCP server + + -- Making the request + local _, status = http_client.get(STUB_GET_URL, nil, { host = "logging.com" }) + assert.are.equal(200, status) + + -- Getting back the TCP server input + local ok, res = thread:join() + assert.truthy(ok) + assert.truthy(res) + + -- Making sure it's alright + assert.are.same("POST / HTTP/1.1", res[1]) + local log_message = cjson.decode(res[7]) + assert.are.same("127.0.0.1", log_message.ip) + end) + + it("should log to Https", function() + local thread = spec_helper.start_http_server(HTTPS_PORT) -- Starting the mock TCP server + + -- Making the request + local _, status = http_client.get(STUB_GET_URL, nil, { host = "logging.com" }) + assert.are.equal(200, status) + + -- Getting back the TCP server input + local ok, res = thread:join() + assert.truthy(ok) + assert.truthy(res) + + -- Making sure it's alright + local log_message = cjson.decode(res[7]) + assert.are.same("127.0.0.1", log_message.ip) + end) + it("should log to file", function() local uuid = string.gsub(uuid(), "-", "") diff --git a/spec/spec_helpers.lua b/spec/spec_helpers.lua index 565a959592..efe7e85c17 100644 --- a/spec/spec_helpers.lua +++ b/spec/spec_helpers.lua @@ -100,6 +100,40 @@ function _M.start_tcp_server(port, ...) return thread end + +-- Starts a HTTP server +-- @param `port` The port where the server will be listening to +-- @return `thread` A thread object +function _M.start_http_server(port, ...) + local thread = Threads.new({ + function(port) + local socket = require "socket" + local server = assert(socket.bind("*", port)) + local client = server:accept() + local lines = {} + local count = 1 + local line, err = nil, nil + while true do + line, err = client:receive() + if not err then + lines[count] = line + line = nil + if count == 7 then + client:send("ok" .. "\n") + break + end + count = count + 1; + end + end + client:close() + return lines + end; + }, port) + + thread:start(...) + return thread +end + -- Starts a UDP server -- @param `port` The port where the server will be listening to -- @return `thread` A thread object