diff --git a/CHANGELOG.md b/CHANGELOG.md index a53f35ad0..68e1fde42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - Added upstream Mutual TLS policy [THREESCALE-672](https://issues.jboss.org/browse/THREESCALE-672) [PR #1182](https://github.com/3scale/APIcast/pull/1182) -- Added Rate-limit headers policy [THREESCALE-3795](https://issues.jboss.org/browse/THREESCALE-3795) [PR #1166](https://github.com/3scale/APIcast/pull/1166) [PR #1197](https://github.com/3scale/APIcast/pull/1197) +- Added Rate-limit headers policy [THREESCALE-3795](https://issues.jboss.org/browse/THREESCALE-3795) [PR #1166](https://github.com/3scale/APIcast/pull/1166) [PR #1197](https://github.com/3scale/APIcast/pull/1197) [PR #1209](https://github.com/3scale/APIcast/pull/1209) - Added Content-caching policy [THREESCALE-2894](https://issues.jboss.org/browse/THREESCALE-2894) [PR #1182](https://github.com/3scale/APIcast/pull/1182) - Added Nginx request_id variable to context [PR #1184](https://github.com/3scale/APIcast/pull/1184) - Added HTTP verb on url_rewriten [PR #1187](https://github.com/3scale/APIcast/pull/1187) [THREESCALE-5259](https://issues.jboss.org/browse/THREESCALE-5259) [PR #1202](https://github.com/3scale/APIcast/pull/1202) diff --git a/gateway/src/apicast/policy/rate_limit_headers/rate_limit_headers.lua b/gateway/src/apicast/policy/rate_limit_headers/rate_limit_headers.lua index 637b56b3f..f71ef4834 100644 --- a/gateway/src/apicast/policy/rate_limit_headers/rate_limit_headers.lua +++ b/gateway/src/apicast/policy/rate_limit_headers/rate_limit_headers.lua @@ -2,6 +2,7 @@ local policy = require('apicast.policy') local _M = policy.new('Rate Limit Headers', 'builtin') local usage_cache = require "cache" +local math_max = math.max local new = _M.new local get_phase = ngx.get_phase @@ -55,9 +56,18 @@ local function decrement(self, usage) return self.cache:decrement_usage_metric(usage) end +-- return 0 if the number is negative +local function positive_number(number) + return math_max(tonumber(number), 0) +end + local function add_headers(info) - ngx.header[limit_header] = info.limit - ngx.header[remaining_header] = info.remaining + if info.reset <= 0 then + -- filter is not defined at all, so skip it + return nil + end + ngx.header[limit_header] = positive_number(info.limit) + ngx.header[remaining_header] = positive_number(info.remaining) ngx.header[reset_header] = info.reset end diff --git a/t/apicast-policy-rate_limit_headers.t b/t/apicast-policy-rate_limit_headers.t index 1a6f29d3f..82aa14a8f 100644 --- a/t/apicast-policy-rate_limit_headers.t +++ b/t/apicast-policy-rate_limit_headers.t @@ -346,3 +346,60 @@ location / { [200, 200] --- no_error_log [error] + +=== TEST 7: No rate-limit information if no limits by app +--- configuration +{ + "services": [ + { + "id": 42, + "backend_version": 1, + "backend_authentication_type": "service_token", + "backend_authentication_value": "token-value", + "proxy": { + "hosts": [ + "localhost" + ], + "api_backend": "http://test:$TEST_NGINX_SERVER_PORT/", + "proxy_rules": [ + { "pattern": "/", "http_method": "GET", "metric_system_name": "hits", "delta": 1 } + ], + "policy_chain": [ + { + "name": "apicast.policy.rate_limit_headers" + }, + { + "name": "apicast", + "version": "builtin", + "configuration": {} + } + ] + } + } + ] +} +--- backend +location /transactions/authrep.xml { + content_by_lua_block { + ngx.exit(200) + } +} +--- upstream env +location / { + access_by_lua_block { + ngx.say("OK") + } +} +--- request +GET /?user_key=123 +--- response_headers +RateLimit-Limit: +RateLimit-Remaining: +RateLimit-Reset: +--- response_body env +OK +--- error_code: 200 +--- no_error_log +[error] + +