Skip to content

Commit

Permalink
Merge pull request #66 from Mashape/refactor/utils
Browse files Browse the repository at this point in the history
Refactoring Utils
  • Loading branch information
subnetmarco committed Mar 11, 2015
2 parents 7a59e2a + 37b2903 commit 8ed5e7b
Show file tree
Hide file tree
Showing 21 changed files with 497 additions and 368 deletions.
6 changes: 5 additions & 1 deletion kong-0.0.1beta-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ dependencies = {
"inspect ~> 3.0-1",
"luasocket ~> 2.0.2-5",
"lua_cliargs ~> 2.3-3",
"lua-path ~> 0.2.3-1"
"lua-path ~> 0.2.3-1",
"luatz ~> 0.3-1"
}
build = {
type = "builtin",
Expand All @@ -40,6 +41,9 @@ build = {
["kong.constants"] = "src/kong/constants.lua",

["kong.tools.utils"] = "src/kong/tools/utils.lua",
["kong.tools.timestamp"] = "src/kong/tools/timestamp.lua",
["kong.tools.cache"] = "src/kong/tools/cache.lua",
["kong.tools.http_client"] = "src/kong/tools/http_client.lua",
["kong.tools.faker"] = "src/kong/tools/faker.lua",
["kong.tools.migrations"] = "src/kong/tools/migrations.lua",

Expand Down
20 changes: 10 additions & 10 deletions spec/integration/api/api_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local spec_helper = require "spec.spec_helpers"
local utils = require "kong.tools.utils"
local http_client = require "kong.tools.http_client"
local cjson = require "cjson"

local created_ids = {}
Expand Down Expand Up @@ -81,7 +81,7 @@ describe("Web API #web", function()
describe("/", function()

it("should return Kong's version and a welcome message", function()
local response, status, headers = utils.get(kWebURL)
local response, status, headers = http_client.get(kWebURL)
local body = cjson.decode(response)
assert.are.equal(200, status)
assert.truthy(body.version)
Expand All @@ -95,7 +95,7 @@ describe("Web API #web", function()

it("should not create on POST with invalid parameters", function()
if v.collection ~= "accounts" then
local response, status, headers = utils.post(kWebURL.."/"..v.collection.."/", {})
local response, status, headers = http_client.post(kWebURL.."/"..v.collection.."/", {})
assert.are.equal(400, status)
assert.are.equal(v.error_message, response)
end
Expand All @@ -109,7 +109,7 @@ describe("Web API #web", function()
end
end

local response, status, headers = utils.post(kWebURL.."/"..v.collection.."/", v.entity)
local response, status, headers = http_client.post(kWebURL.."/"..v.collection.."/", v.entity)
local body = cjson.decode(response)
assert.are.equal(201, status)
assert.truthy(body)
Expand All @@ -119,7 +119,7 @@ describe("Web API #web", function()
end)

it("should GET all entities", function()
local response, status, headers = utils.get(kWebURL.."/"..v.collection.."/")
local response, status, headers = http_client.get(kWebURL.."/"..v.collection.."/")
local body = cjson.decode(response)
assert.are.equal(200, status)
assert.truthy(body.data)
Expand All @@ -129,31 +129,31 @@ describe("Web API #web", function()
end)

it("should GET one entity", function()
local response, status, headers = utils.get(kWebURL.."/"..v.collection.."/"..created_ids[v.collection])
local response, status, headers = http_client.get(kWebURL.."/"..v.collection.."/"..created_ids[v.collection])
local body = cjson.decode(response)
assert.are.equal(200, status)
assert.truthy(body)
assert.are.equal(created_ids[v.collection], body.id)
end)

it("should return not found on GET", function()
local response, status, headers = utils.get(kWebURL.."/"..v.collection.."/"..created_ids[v.collection].."blah")
local response, status, headers = http_client.get(kWebURL.."/"..v.collection.."/"..created_ids[v.collection].."blah")
local body = cjson.decode(response)
assert.are.equal(404, status)
assert.truthy(body)
assert.are.equal('{"id":"'..created_ids[v.collection]..'blah is an invalid uuid"}', response)
end)

it("should update a created entity on PUT", function()
local data = utils.get(kWebURL.."/"..v.collection.."/"..created_ids[v.collection])
local data = http_client.get(kWebURL.."/"..v.collection.."/"..created_ids[v.collection])
local body = cjson.decode(data)

-- Create new body
for k,v in pairs(v.update_fields) do
body[k] = v
end

local response, status, headers = utils.put(kWebURL.."/"..v.collection.."/"..created_ids[v.collection], body)
local response, status, headers = http_client.put(kWebURL.."/"..v.collection.."/"..created_ids[v.collection], body)
body = cjson.decode(response)
assert.are.equal(200, status)
assert.truthy(body)
Expand All @@ -171,7 +171,7 @@ describe("Web API #web", function()
describe("#"..v.collection, function()

it("should delete an entity on DELETE", function()
local response, status, headers = utils.delete(kWebURL.."/"..v.collection.."/"..created_ids[v.collection])
local response, status, headers = http_client.delete(kWebURL.."/"..v.collection.."/"..created_ids[v.collection])
assert.are.equal(204, status)
end)

Expand Down
36 changes: 18 additions & 18 deletions spec/integration/proxy/authentication_plugin_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local spec_helper = require "spec.spec_helpers"
local utils = require "kong.tools.utils"
local http_client = require "kong.tools.http_client"
local cjson = require "cjson"

STUB_GET_URL = spec_helper.STUB_GET_URL
Expand All @@ -20,35 +20,35 @@ describe("Authentication Plugin #proxy", function()
describe("Query Authentication", function()

it("should return invalid credentials when the credential value is wrong", function()
local response, status, headers = utils.get(STUB_GET_URL, {apikey = "asd"}, {host = "test.com"})
local response, status, headers = http_client.get(STUB_GET_URL, {apikey = "asd"}, {host = "test.com"})
local body = cjson.decode(response)
assert.are.equal(403, status)
assert.are.equal("Your authentication credentials are invalid", body.message)
end)

it("should return invalid credentials when the credential parameter name is wrong in GET", function()
local response, status, headers = utils.get(STUB_GET_URL, {apikey123 = "apikey123"}, {host = "test.com"})
local response, status, headers = http_client.get(STUB_GET_URL, {apikey123 = "apikey123"}, {host = "test.com"})
local body = cjson.decode(response)
assert.are.equal(403, status)
assert.are.equal("Your authentication credentials are invalid", body.message)
end)

it("should return invalid credentials when the credential parameter name is wrong in POST", function()
local response, status, headers = utils.post(STUB_POST_URL, {apikey123 = "apikey123"}, {host = "test.com"})
local response, status, headers = http_client.post(STUB_POST_URL, {apikey123 = "apikey123"}, {host = "test.com"})
local body = cjson.decode(response)
assert.are.equal(403, status)
assert.are.equal("Your authentication credentials are invalid", body.message)
end)

it("should pass with GET", function()
local response, status, headers = utils.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test.com"})
local response, status, headers = http_client.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test.com"})
assert.are.equal(200, status)
local parsed_response = cjson.decode(response)
assert.are.equal("apikey123", parsed_response.queryString.apikey)
end)

it("should pass with POST", function()
local response, status, headers = utils.post(STUB_POST_URL, {apikey = "apikey123"}, {host = "test.com"})
local response, status, headers = http_client.post(STUB_POST_URL, {apikey = "apikey123"}, {host = "test.com"})
assert.are.equal(200, status)
local parsed_response = cjson.decode(response)
assert.are.equal("apikey123", parsed_response.postData.params.apikey)
Expand All @@ -59,35 +59,35 @@ describe("Authentication Plugin #proxy", function()
describe("Header Authentication", function()

it("should return invalid credentials when the credential value is wrong", function()
local response, status, headers = utils.get(STUB_GET_URL, {}, {host = "test2.com", apikey = "asd"})
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "test2.com", apikey = "asd"})
local body = cjson.decode(response)
assert.are.equal(403, status)
assert.are.equal("Your authentication credentials are invalid", body.message)
end)

it("should return invalid credentials when the credential parameter name is wrong in GET", function()
local response, status, headers = utils.get(STUB_GET_URL, {}, {host = "test2.com", apikey123 = "apikey123"})
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "test2.com", apikey123 = "apikey123"})
local body = cjson.decode(response)
assert.are.equal(403, status)
assert.are.equal("Your authentication credentials are invalid", body.message)
end)

it("should return invalid credentials when the credential parameter name is wrong in POST", function()
local response, status, headers = utils.post(STUB_POST_URL, {}, {host = "test2.com", apikey123 = "apikey123"})
local response, status, headers = http_client.post(STUB_POST_URL, {}, {host = "test2.com", apikey123 = "apikey123"})
local body = cjson.decode(response)
assert.are.equal(403, status)
assert.are.equal("Your authentication credentials are invalid", body.message)
end)

it("should pass with GET", function()
local response, status, headers = utils.get(STUB_GET_URL, {}, {host = "test2.com", apikey = "apikey123"})
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "test2.com", apikey = "apikey123"})
assert.are.equal(200, status)
local parsed_response = cjson.decode(response)
assert.are.equal("apikey123", parsed_response.headers.apikey)
end)

it("should pass with POST", function()
local response, status, headers = utils.post(STUB_POST_URL, {}, {host = "test2.com", apikey = "apikey123"})
local response, status, headers = http_client.post(STUB_POST_URL, {}, {host = "test2.com", apikey = "apikey123"})
assert.are.equal(200, status)
local parsed_response = cjson.decode(response)
assert.are.equal("apikey123", parsed_response.headers.apikey)
Expand All @@ -98,49 +98,49 @@ describe("Authentication Plugin #proxy", function()
describe("Basic Authentication", function()

it("should return invalid credentials when the credential value is wrong", function()
local response, status, headers = utils.get(STUB_GET_URL, {}, {host = "test3.com", authorization = "asd"})
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "test3.com", authorization = "asd"})
local body = cjson.decode(response)
assert.are.equal(403, status)
assert.are.equal("Your authentication credentials are invalid", body.message)
end)

it("should not pass when passing only the password", function()
local response, status, headers = utils.get(STUB_GET_URL, {}, {host = "test3.com", authorization = "Basic OmFwaWtleTEyMw=="})
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "test3.com", authorization = "Basic OmFwaWtleTEyMw=="})
local body = cjson.decode(response)
assert.are.equal(403, status)
assert.are.equal("Your authentication credentials are invalid", body.message)
end)

it("should not pass when passing only the username", function()
local response, status, headers = utils.get(STUB_GET_URL, {}, {host = "test3.com", authorization = "Basic dXNlcjEyMzo="})
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "test3.com", authorization = "Basic dXNlcjEyMzo="})
local body = cjson.decode(response)
assert.are.equal(403, status)
assert.are.equal("Your authentication credentials are invalid", body.message)
end)

it("should return invalid credentials when the credential parameter name is wrong in GET", function()
local response, status, headers = utils.get(STUB_GET_URL, {}, {host = "test3.com", authorization123 = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="})
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "test3.com", authorization123 = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="})
local body = cjson.decode(response)
assert.are.equal(403, status)
assert.are.equal("Your authentication credentials are invalid", body.message)
end)

it("should return invalid credentials when the credential parameter name is wrong in POST", function()
local response, status, headers = utils.post(STUB_POST_URL, {}, {host = "test3.com", authorization123 = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="})
local response, status, headers = http_client.post(STUB_POST_URL, {}, {host = "test3.com", authorization123 = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="})
local body = cjson.decode(response)
assert.are.equal(403, status)
assert.are.equal("Your authentication credentials are invalid", body.message)
end)

it("should pass with GET", function()
local response, status, headers = utils.get(STUB_GET_URL, {}, {host = "test3.com", authorization = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="})
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "test3.com", authorization = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="})
assert.are.equal(200, status)
local parsed_response = cjson.decode(response)
assert.are.equal("Basic dXNlcm5hbWU6cGFzc3dvcmQ=", parsed_response.headers.authorization)
end)

it("should pass with POST", function()
local response, status, headers = utils.post(STUB_POST_URL, {}, {host = "test3.com", authorization = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="})
local response, status, headers = http_client.post(STUB_POST_URL, {}, {host = "test3.com", authorization = "Basic dXNlcm5hbWU6cGFzc3dvcmQ="})
assert.are.equal(200, status)
local parsed_response = cjson.decode(response)
assert.are.equal("Basic dXNlcm5hbWU6cGFzc3dvcmQ=", parsed_response.headers.authorization)
Expand Down
6 changes: 3 additions & 3 deletions spec/integration/proxy/core_access_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local spec_helper = require "spec.spec_helpers"
local utils = require "kong.tools.utils"
local http_client = require "kong.tools.http_client"
local cjson = require "cjson"

local STUB_GET_URL = spec_helper.STUB_GET_URL
Expand All @@ -19,7 +19,7 @@ describe("Proxy API #proxy", function()
describe("Invalid API", function()

it("should return API not found when the API is missing", function()
local response, status, headers = utils.get(spec_helper.PROXY_URL)
local response, status, headers = http_client.get(spec_helper.PROXY_URL)
local body = cjson.decode(response)
assert.are.equal(404, status)
assert.are.equal("API not found", body.message)
Expand All @@ -30,7 +30,7 @@ describe("Proxy API #proxy", function()
describe("Existing API", function()

it("should return API found when the API has been created", function()
local response, status, headers = utils.get(STUB_GET_URL, {}, {host = "test4.com"})
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "test4.com"})
assert.are.equal(200, status)
end)

Expand Down
14 changes: 7 additions & 7 deletions spec/integration/proxy/ratelimiting_plugin_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local spec_helper = require "spec.spec_helpers"
local utils = require "kong.tools.utils"
local http_client = require "kong.tools.http_client"
local cjson = require "cjson"

local STUB_GET_URL = spec_helper.STUB_GET_URL
Expand All @@ -23,14 +23,14 @@ describe("RateLimiting Plugin #proxy", function()
local limit = 2

for i = 1, limit do
local response, status, headers = utils.get(STUB_GET_URL, {}, {host = "test5.com"})
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "test5.com"})
assert.are.equal(200, status)
assert.are.same(tostring(limit), headers["x-ratelimit-limit"])
assert.are.same(tostring(limit - i), headers["x-ratelimit-remaining"])
end

-- Third query, while limit is 2/minute
local response, status, headers = utils.get(STUB_GET_URL, {}, {host = "test5.com"})
local response, status, headers = http_client.get(STUB_GET_URL, {}, {host = "test5.com"})
local body = cjson.decode(response)
assert.are.equal(429, status)
assert.are.equal("API rate limit exceeded", body.message)
Expand All @@ -47,14 +47,14 @@ describe("RateLimiting Plugin #proxy", function()
local limit = 2

for i = 1, limit do
local response, status, headers = utils.get(STUB_GET_URL, {apikey = "apikey122"}, {host = "test6.com"})
local response, status, headers = http_client.get(STUB_GET_URL, {apikey = "apikey122"}, {host = "test6.com"})
assert.are.equal(200, status)
assert.are.same(tostring(limit), headers["x-ratelimit-limit"])
assert.are.same(tostring(limit - i), headers["x-ratelimit-remaining"])
end

-- Third query, while limit is 2/minute
local response, status, headers = utils.get(STUB_GET_URL, {apikey = "apikey122"}, {host = "test6.com"})
local response, status, headers = http_client.get(STUB_GET_URL, {apikey = "apikey122"}, {host = "test6.com"})
local body = cjson.decode(response)
assert.are.equal(429, status)
assert.are.equal("API rate limit exceeded", body.message)
Expand All @@ -69,13 +69,13 @@ describe("RateLimiting Plugin #proxy", function()
local limit = 4

for i = 1, limit do
local response, status, headers = utils.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test6.com"})
local response, status, headers = http_client.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test6.com"})
assert.are.equal(200, status)
assert.are.same(tostring(limit), headers["x-ratelimit-limit"])
assert.are.same(tostring(limit - i), headers["x-ratelimit-remaining"])
end

local response, status, headers = utils.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test6.com"})
local response, status, headers = http_client.get(STUB_GET_URL, {apikey = "apikey123"}, {host = "test6.com"})
local body = cjson.decode(response)
assert.are.equal(429, status)
assert.are.equal("API rate limit exceeded", body.message)
Expand Down
18 changes: 18 additions & 0 deletions spec/unit/cache_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local cache = require "kong.tools.cache"

describe("Cache", function()

it("should return a valid API cache key", function()
assert.are.equal("apis/httpbin.org", cache.api_key("httpbin.org"))
end)

it("should return a valid PLUGIN cache key", function()
assert.are.equal("plugins/authentication/api123/app123", cache.plugin_key("authentication", "api123", "app123"))
assert.are.equal("plugins/authentication/api123", cache.plugin_key("authentication", "api123"))
end)

it("should return a valid Application cache key", function()
assert.are.equal("applications/username", cache.application_key("username"))
end)

end)
Loading

0 comments on commit 8ed5e7b

Please sign in to comment.