Skip to content

Commit

Permalink
chore: introducing ngx_stub.lua for specs
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed May 9, 2015
1 parent 2cf80ea commit 663e130
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 44 deletions.
48 changes: 5 additions & 43 deletions kong/tools/http_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,6 @@ local cjson = require "cjson"

local _M = {}

-- Builds a querystring from a table, separated by `&`
-- @param tab The key/value parameters
-- @param key The parent key if the value is multi-dimensional (optional)
-- @return a string representing the built querystring
local function build_query(tab, key)
if ngx then
return ngx.encode_args(tab)
else
local query = {}
local keys = {}

for k in pairs(tab) do
keys[#keys+1] = k
end

table.sort(keys)

for _,name in ipairs(keys) do
local value = tab[name]
if key then
name = string.format("%s[%s]", tostring(key), tostring(name))
end
if type(value) == "table" then
query[#query+1] = build_query(value, name)
else
value = tostring(value)
if value ~= "" then
query[#query+1] = string.format("%s=%s", name, value)
else
query[#query+1] = name
end
end
end

return table.concat(query, "&")
end
end

local function http_call(options)
-- Set Host header accordingly
if not options.headers["host"] then
Expand All @@ -67,7 +29,7 @@ function _M.get(url, querystring, headers)
if not headers then headers = {} end

if querystring then
url = string.format("%s?%s", url, build_query(querystring))
url = string.format("%s?%s", url, ngx.encode_args(querystring))
end

return http_call {
Expand All @@ -82,7 +44,7 @@ function _M.post(url, form, headers)
if not headers then headers = {} end
if not form then form = {} end

local body = build_query(form)
local body = ngx.encode_args(form)
headers["content-length"] = string.len(body)
if not headers["content-type"] then
headers["content-type"] = "application/x-www-form-urlencoded"
Expand Down Expand Up @@ -148,7 +110,7 @@ function _M.delete(url, querystring, headers)
if not headers then headers = {} end

if querystring then
url = string.format("%s?%s", url, build_query(querystring))
url = string.format("%s?%s", url, ngx.encode_args(querystring))
end

return http_call {
Expand All @@ -163,7 +125,7 @@ function _M.options(url, querystring, headers)
if not headers then headers = {} end

if querystring then
url = string.format("%s?%s", url, build_query(querystring))
url = string.format("%s?%s", url, ngx.encode_args(querystring))
end

return http_call {
Expand All @@ -173,4 +135,4 @@ function _M.options(url, querystring, headers)
}
end

return _M
return _M
36 changes: 35 additions & 1 deletion spec/ngx_stub.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
local reg = require "rex_pcre"

_G.ngx = {
time = function() return os.time() end, -- cassandra.lua
re = {
match = reg.match
}
},
-- Builds a querystring from a table, separated by `&`
-- @param tab The key/value parameters
-- @param key The parent key if the value is multi-dimensional (optional)
-- @return a string representing the built querystring
encode_args = function(tab, key)
local query = {}
local keys = {}

for k in pairs(tab) do
keys[#keys+1] = k
end

table.sort(keys)

for _, name in ipairs(keys) do
local value = tab[name]
if key then
name = string.format("%s[%s]", tostring(key), tostring(name))
end
if type(value) == "table" then
query[#query+1] = build_query(value, name)
else
value = tostring(value)
if value ~= "" then
query[#query+1] = string.format("%s=%s", name, value)
else
query[#query+1] = name
end
end
end

return table.concat(query, "&")
end
}
2 changes: 2 additions & 0 deletions spec/spec_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ local TEST_CONF_FILE = "kong_TEST.yml"
local TEST_PROXY_URL = "http://localhost:8100"
local TEST_API_URL = "http://localhost:8101"

require "spec.ngx_stub"

local _M = {}

_M.API_URL = TEST_API_URL
Expand Down
2 changes: 2 additions & 0 deletions spec/unit/tools/http_client_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local cjson = require "cjson"
local http_client = require "kong.tools.http_client"

require "spec.ngx_stub"

describe("HTTP Client", function()

describe("GET", function()
Expand Down

0 comments on commit 663e130

Please sign in to comment.