Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashi Ranjan committed May 20, 2015
1 parent eea1cb2 commit 78647bd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
7 changes: 5 additions & 2 deletions kong/plugins/httplog/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
38 changes: 38 additions & 0 deletions spec/plugins/logging_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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 }
}
}
Expand Down Expand Up @@ -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(), "-", "")

Expand Down
34 changes: 34 additions & 0 deletions spec/spec_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 78647bd

Please sign in to comment.