Skip to content

Commit

Permalink
Closes #344 and addresses some issues with #196
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed Jun 17, 2015
1 parent 3c94eb7 commit d733f3c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
11 changes: 9 additions & 2 deletions kong/api/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ local responses = require "kong.tools.responses"
local app_helpers = require "lapis.application"
local app = lapis.Application()

-- Parses a form value, handling multipart/data values
-- @param `v` The value object
-- @return The parsed value
local function parse_value(v)
return type(v) == "table" and v.content or v -- Handle multipart
end

-- Put nested keys in objects:
-- Normalize dotted keys in objects.
-- Example: {["key.value.sub"]=1234} becomes {key = {value = {sub=1234}}
Expand Down Expand Up @@ -42,9 +49,9 @@ local function normalize_nested_params(obj)
-- normalize sub-keys with dot notation
local keys = stringy.split(k, ".")
if #keys > 1 then -- we have a key containing a dot
attach_dotted_key(keys, new_obj, v)
attach_dotted_key(keys, new_obj, parse_value(v))
else
new_obj[k] = v -- nothing special with that key, simply attaching the value
new_obj[k] = parse_value(v) -- nothing special with that key, simply attaching the value
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/integration/admin_api/admin_api_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local json = require "cjson"
local http_client = require "kong.tools.http_client"
local spec_helper = require "spec.spec_helpers"
local IO = require "kong.tools.io"
local stringy = require "stringy"

local CREATED_IDS = {}
local ENDPOINTS = {
Expand Down Expand Up @@ -169,6 +171,27 @@ describe("Admin API", function()
assert.are.equal(endpoint.error_message, response)
end)

it("should create an entity with a valid body with curl multipart", function()
-- Replace the IDs
attach_ids()

local curl_str = "curl -s "..base_url.."/"
for k, v in pairs(endpoint.entity.form) do
local tmp = os.tmpname()
IO.write_to_file(tmp, v)
curl_str = curl_str.." --form \""..k.."=@"..tmp.."\""
end

local res = IO.os_execute(curl_str)
local body = json.decode(res)
assert.truthy(body)
assert.truthy(body.id)

http_client.delete(base_url.."/"..body.id)

CREATED_IDS[endpoint.collection] = body.id
end)

it("should create an entity with a valid body", function()
-- Replace the IDs
attach_ids()
Expand Down
15 changes: 15 additions & 0 deletions spec/plugins/ratelimiting_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ local cjson = require "cjson"

local STUB_GET_URL = spec_helper.STUB_GET_URL

local function wait()
-- Wait til the beginning of a new second before starting the test
-- to avoid ending up in an edge case when the second is about to end
local now = os.time()
while os.time() < now + 1 do
-- Nothing
end
end

describe("RateLimiting Plugin", function()

setup(function()
Expand Down Expand Up @@ -39,6 +48,8 @@ describe("RateLimiting Plugin", function()
describe("Without authentication (IP address)", function()

it("should get blocked if exceeding limit", function()
wait()

-- Default ratelimiting plugin for this API says 6/minute
local limit = 6

Expand All @@ -63,6 +74,8 @@ describe("RateLimiting Plugin", function()
describe("Default plugin", function()

it("should get blocked if exceeding limit", function()
wait()

-- Default ratelimiting plugin for this API says 6/minute
local limit = 6

Expand All @@ -85,6 +98,8 @@ describe("RateLimiting Plugin", function()
describe("Plugin customized for specific consumer", function()

it("should get blocked if exceeding limit", function()
wait()

-- This plugin says this consumer can make 4 requests/minute, not 6 like the default
local limit = 8

Expand Down
10 changes: 10 additions & 0 deletions spec/plugins/ssl_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local cjson = require "cjson"

local STUB_GET_SSL_URL = spec_helper.STUB_GET_SSL_URL
local STUB_GET_URL = spec_helper.STUB_GET_URL
local API_URL = spec_helper.API_URL

describe("SSL Plugin", function()

Expand All @@ -16,6 +17,7 @@ describe("SSL Plugin", function()
api = {
{ name = "API TESTS 11 (ssl)", public_dns = "ssl1.com", target_url = "http://mockbin.com" },
{ name = "API TESTS 12 (ssl)", public_dns = "ssl2.com", target_url = "http://mockbin.com" },
{ name = "API TESTS 13 (ssl)", public_dns = "ssl3.com", target_url = "http://mockbin.com" }
},
plugin_configuration = {
{ name = "ssl", value = { cert = [[
Expand Down Expand Up @@ -180,5 +182,13 @@ IN2a44ptbkUjN8U0WeTGMBP/XfK3SvV6wAKAE3cDB2c=
end)

end)

describe("should work with curl", function()
local response, status = http_client.get(API_URL.."/apis/", {public_dns="ssl3.com"})
local api_id = cjson.decode(response).data[1].id
local current_path = IO.os_execute("pwd")
local res = IO.os_execute("curl -s -o /dev/null -w \"%{http_code}\" "..API_URL.."/apis/"..api_id.."/plugins/ --form \"name=ssl\" --form \"value.cert=@"..current_path.."/ssl/kong-default.crt\" --form \"value.key=@"..current_path.."/ssl/kong-default.key\"")
assert.are.equal("201", res)
end)

end)

0 comments on commit d733f3c

Please sign in to comment.