|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +use t::APISIX 'no_plan'; |
| 19 | + |
| 20 | +repeat_each(1); |
| 21 | +no_long_string(); |
| 22 | +no_shuffle(); |
| 23 | +no_root_location(); |
| 24 | + |
| 25 | +add_block_preprocessor(sub { |
| 26 | + my ($block) = @_; |
| 27 | + |
| 28 | + if (!$block->request) { |
| 29 | + $block->set_value("request", "GET /t"); |
| 30 | + } |
| 31 | + |
| 32 | + if (!$block->error_log && !$block->no_error_log) { |
| 33 | + $block->set_value("no_error_log", "[error]\n[alert]"); |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +run_tests; |
| 38 | + |
| 39 | +__DATA__ |
| 40 | +
|
| 41 | +=== TEST 1: redis-cluster policy with sliding window - basic N per window |
| 42 | +--- config |
| 43 | + location /t { |
| 44 | + content_by_lua_block { |
| 45 | + local t = require("lib.test_admin").test |
| 46 | + local code, body = t('/apisix/admin/routes/1', |
| 47 | + ngx.HTTP_PUT, |
| 48 | + [[{ |
| 49 | + "uri": "/hello", |
| 50 | + "plugins": { |
| 51 | + "limit-count": { |
| 52 | + "count": 2, |
| 53 | + "time_window": 2, |
| 54 | + "window_type": "sliding", |
| 55 | + "rejected_code": 503, |
| 56 | + "key": "remote_addr", |
| 57 | + "policy": "redis-cluster", |
| 58 | + "redis_cluster_nodes": [ |
| 59 | + "127.0.0.1:5000", |
| 60 | + "127.0.0.1:5001" |
| 61 | + ], |
| 62 | + "redis_cluster_name": "redis-cluster-1" |
| 63 | + } |
| 64 | + }, |
| 65 | + "upstream": { |
| 66 | + "nodes": { |
| 67 | + "127.0.0.1:1980": 1 |
| 68 | + }, |
| 69 | + "type": "roundrobin" |
| 70 | + } |
| 71 | + }]] |
| 72 | + ) |
| 73 | +
|
| 74 | + if code >= 300 then |
| 75 | + ngx.status = code |
| 76 | + end |
| 77 | + ngx.say(body) |
| 78 | + } |
| 79 | + } |
| 80 | +--- response_body |
| 81 | +passed |
| 82 | +
|
| 83 | +
|
| 84 | +=== TEST 2: redis-cluster policy with sliding window - enforce N per window |
| 85 | +--- pipelined_requests eval |
| 86 | +["GET /hello", "GET /hello", "GET /hello"] |
| 87 | +--- error_code eval |
| 88 | +[200, 200, 503] |
| 89 | +
|
| 90 | +
|
| 91 | +=== TEST 3: redis-cluster policy with sliding window - remaining header on reject |
| 92 | +--- config |
| 93 | + location /t { |
| 94 | + content_by_lua_block { |
| 95 | + local json = require "t.toolkit.json" |
| 96 | + local http = require "resty.http" |
| 97 | + local uri = "http://127.0.0.1:" .. ngx.var.server_port |
| 98 | + .. "/hello" |
| 99 | + local ress = {} |
| 100 | +
|
| 101 | + -- ensure previous windows are expired before starting this test |
| 102 | + ngx.sleep(2.2) |
| 103 | +
|
| 104 | + -- first request: allowed, remaining should be 1 |
| 105 | + do |
| 106 | + local httpc = http.new() |
| 107 | + local res, err = httpc:request_uri(uri) |
| 108 | + if not res then |
| 109 | + ngx.say(err) |
| 110 | + return |
| 111 | + end |
| 112 | + table.insert(ress, {res.status, res.headers["X-RateLimit-Remaining"]}) |
| 113 | + end |
| 114 | +
|
| 115 | + -- second request: allowed, remaining should be 0 |
| 116 | + do |
| 117 | + local httpc = http.new() |
| 118 | + local res, err = httpc:request_uri(uri) |
| 119 | + if not res then |
| 120 | + ngx.say(err) |
| 121 | + return |
| 122 | + end |
| 123 | + table.insert(ress, {res.status, res.headers["X-RateLimit-Remaining"]}) |
| 124 | + end |
| 125 | +
|
| 126 | + -- third request: rejected, remaining header should stay at 0 |
| 127 | + do |
| 128 | + local httpc = http.new() |
| 129 | + local res, err = httpc:request_uri(uri) |
| 130 | + if not res then |
| 131 | + ngx.say(err) |
| 132 | + return |
| 133 | + end |
| 134 | + table.insert(ress, {res.status, res.headers["X-RateLimit-Remaining"]}) |
| 135 | + end |
| 136 | +
|
| 137 | + ngx.say(json.encode(ress)) |
| 138 | + } |
| 139 | + } |
| 140 | +--- response_body |
| 141 | +[[200,"1"],[200,"0"],[503,"0"]] |
| 142 | +
|
| 143 | +
|
| 144 | +=== TEST 4: redis-cluster policy with sliding window - allow after window passes |
| 145 | +--- config |
| 146 | + location /t { |
| 147 | + content_by_lua_block { |
| 148 | + local json = require "t.toolkit.json" |
| 149 | + local http = require "resty.http" |
| 150 | + local uri = "http://127.0.0.1:" .. ngx.var.server_port |
| 151 | + .. "/hello" |
| 152 | + local codes = {} |
| 153 | +
|
| 154 | + -- ensure previous windows are expired before starting this test |
| 155 | + ngx.sleep(2.2) |
| 156 | +
|
| 157 | + -- consume full quota |
| 158 | + for i = 1, 2 do |
| 159 | + local httpc = http.new() |
| 160 | + local res, err = httpc:request_uri(uri) |
| 161 | + if not res then |
| 162 | + ngx.say(err) |
| 163 | + return |
| 164 | + end |
| 165 | + table.insert(codes, res.status) |
| 166 | + end |
| 167 | +
|
| 168 | + -- wait longer than the sliding window (2s) |
| 169 | + ngx.sleep(2.2) |
| 170 | +
|
| 171 | + -- should be allowed again after window has passed |
| 172 | + do |
| 173 | + local httpc = http.new() |
| 174 | + local res, err = httpc:request_uri(uri) |
| 175 | + if not res then |
| 176 | + ngx.say(err) |
| 177 | + return |
| 178 | + end |
| 179 | + table.insert(codes, res.status) |
| 180 | + end |
| 181 | +
|
| 182 | + ngx.say(json.encode(codes)) |
| 183 | + } |
| 184 | + } |
| 185 | +--- response_body |
| 186 | +[200,200,200] |
| 187 | +
|
| 188 | +
|
0 commit comments