-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a01fbfa
commit 55352bc
Showing
11 changed files
with
263 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,44 @@ | ||
local DaoError = require "kong.dao.error" | ||
local constants = require "kong.constants" | ||
|
||
return { | ||
fields = { | ||
limit = { required = true, type = "number" }, | ||
period = { required = true, type = "string", enum = constants.RATELIMIT.PERIODS } | ||
} | ||
} | ||
second = { type = "number" }, | ||
minute = { type = "number" }, | ||
hour = { type = "number" }, | ||
day = { type = "number" }, | ||
month = { type = "number" }, | ||
year = { type = "number" } | ||
}, | ||
self_check = function(schema, plugin_t, dao, is_update) | ||
local ordered_periods = { "second", "minute", "hour", "day", "month", "year"} | ||
local has_value | ||
local invalid_order | ||
local invalid_value | ||
|
||
for i, v in ipairs(ordered_periods) do | ||
if plugin_t[v] then | ||
has_value = true | ||
if plugin_t[v] <=0 then | ||
invalid_value = "Value for "..v.." must be greater than zero" | ||
else | ||
for t = i, #ordered_periods do | ||
if plugin_t[ordered_periods[t]] and plugin_t[ordered_periods[t]] < plugin_t[v] then | ||
invalid_order = "The value for "..ordered_periods[t].." cannot be lower than the value for "..v | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
if not has_value then | ||
return false, DaoError("You need to set at least one limit: second, minute, hour, day, month, year", constants.DATABASE_ERROR_TYPES.SCHEMA) | ||
elseif invalid_value then | ||
return false, DaoError(invalid_value, constants.DATABASE_ERROR_TYPES.SCHEMA) | ||
elseif invalid_order then | ||
return false, DaoError(invalid_order, constants.DATABASE_ERROR_TYPES.SCHEMA) | ||
end | ||
|
||
return true | ||
end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
local json = require "cjson" | ||
local http_client = require "kong.tools.http_client" | ||
local spec_helper = require "spec.spec_helpers" | ||
|
||
local BASE_URL = spec_helper.API_URL.."/apis/%s/plugins/" | ||
|
||
describe("Rate Limiting API", function() | ||
setup(function() | ||
spec_helper.prepare_db() | ||
spec_helper.insert_fixtures { | ||
api = { | ||
{ name = "tests ratelimiting 1", public_dns = "test1.com", target_url = "http://mockbin.com" } | ||
} | ||
} | ||
spec_helper.start_kong() | ||
|
||
local response = http_client.get(spec_helper.API_URL.."/apis/") | ||
BASE_URL = string.format(BASE_URL, json.decode(response).data[1].id) | ||
end) | ||
|
||
teardown(function() | ||
spec_helper.stop_kong() | ||
end) | ||
|
||
describe("POST", function() | ||
|
||
it("should not save with empty value", function() | ||
local response, status = http_client.post(BASE_URL, { name = "ratelimiting" }) | ||
local body = json.decode(response) | ||
assert.are.equal(400, status) | ||
assert.are.equal("You need to set at least one limit: second, minute, hour, day, month, year", body.message) | ||
end) | ||
|
||
it("should save with proper value", function() | ||
local response, status = http_client.post(BASE_URL, { name = "ratelimiting", ["value.second"] = 10 }) | ||
local body = json.decode(response) | ||
assert.are.equal(201, status) | ||
assert.are.equal(10, body.value.second) | ||
end) | ||
|
||
end) | ||
|
||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
local schemas = require "kong.dao.schemas_validation" | ||
local validate_entity = schemas.validate_entity | ||
|
||
local plugin_schema = require "kong.plugins.ratelimiting.schema" | ||
|
||
describe("Rate Limiting schema", function() | ||
|
||
it("should be invalid when no value is being set", function() | ||
local values = {} | ||
local valid, _, err = validate_entity(values, plugin_schema) | ||
assert.falsy(valid) | ||
assert.are.equal("You need to set at least one limit: second, minute, hour, day, month, year", err.message) | ||
end) | ||
|
||
it("should work when the proper value is being set", function() | ||
local values = { second = 10 } | ||
local valid, _, err = validate_entity(values, plugin_schema) | ||
assert.truthy(valid) | ||
assert.falsy(err) | ||
end) | ||
|
||
it("should work when the proper value are being set", function() | ||
local values = { second = 10, hour = 20 } | ||
local valid, _, err = validate_entity(values, plugin_schema) | ||
assert.truthy(valid) | ||
assert.falsy(err) | ||
end) | ||
|
||
it("should not work when invalid data is being set", function() | ||
local values = { second = 20, hour = 10 } | ||
local valid, _, err = validate_entity(values, plugin_schema) | ||
assert.falsy(valid) | ||
assert.are.equal("The value for hour cannot be lower than the value for second", err.message) | ||
end) | ||
|
||
end) |
Oops, something went wrong.