From dca121080c0f262c6b3828d476ba311d1d63936d Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Fri, 13 Oct 2023 17:38:14 +0800 Subject: [PATCH 1/5] test: use http2 to test limit-req plugin --- t/plugin/limit-req3.t | 132 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 t/plugin/limit-req3.t diff --git a/t/plugin/limit-req3.t b/t/plugin/limit-req3.t new file mode 100644 index 000000000000..92a8d1c76cce --- /dev/null +++ b/t/plugin/limit-req3.t @@ -0,0 +1,132 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +log_level('info'); +repeat_each(1); +no_long_string(); +no_shuffle(); +no_root_location(); + +add_block_preprocessor(sub { + my ($block) = @_; + my $port = $ENV{TEST_NGINX_SERVER_PORT}; + + my $TEST_NGINX_HTML_DIR ||= html_dir(); + my $config = $block->config // <<_EOC_; + listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl; +_EOC_ + + $block->set_value("config", $config); + + if (!$block->request) { + $block->set_value("request", "GET /t"); + } + + if (!$block->error_log && !$block->no_error_log) { + $block->set_value("no_error_log", "[error]\n[alert]"); + } +}); + +run_tests; + +__DATA__ + +=== TEST 1: create route with limit-req plugin +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "limit-req": { + "rate": 0.1, + "burst": 0.1, + "rejected_code": 503, + "key": "$remote_addr", + "key_type": "var_combination" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello", + "host": "www.test.com" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed + + + +=== TEST 2: create ssl(sni: www.test.com) +--- yaml_config +apisix: + node_listen: 1984 +--- config +location /t { + content_by_lua_block { + local core = require("apisix.core") + local t = require("lib.test_admin") + local ssl_cert = t.read_file("t/certs/apisix.crt") + local ssl_key = t.read_file("t/certs/apisix.key") + local data = {cert = ssl_cert, key = ssl_key, sni = "www.test.com"} + local code, body = t.test('/apisix/admin/ssls/1', + ngx.HTTP_PUT, + core.json.encode(data), + [[{ + "value": { + "sni": "www.test.com" + }, + "key": "/apisix/ssls/1" + }]] + ) + ngx.status = code + ngx.say(body) + } +} +--- response_body +passed + + + +=== TEST 3: use HTTP version 2 to request +--- yaml_config +apisix: + ssl: + enable: true + listen: # APISIX listening port for HTTPS traffic. + - port: 9443 + enable_http2: true +--- exec +curl --http2 --parallel -k https://www.test.com:9443/hello https://www.test.com:9443/hello --resolve www.test.com:9443:127.0.0.1 +--- response_body_like +503 Service Temporarily Unavailable.*.hello world From 93406e83338e6372550acf1ad7ec9eed4e22289c Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Fri, 13 Oct 2023 17:42:47 +0800 Subject: [PATCH 2/5] fix --- t/plugin/limit-req3.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/plugin/limit-req3.t b/t/plugin/limit-req3.t index 92a8d1c76cce..98c0f351bf59 100644 --- a/t/plugin/limit-req3.t +++ b/t/plugin/limit-req3.t @@ -123,7 +123,7 @@ passed apisix: ssl: enable: true - listen: # APISIX listening port for HTTPS traffic. + listen: - port: 9443 enable_http2: true --- exec From 63ff59a57498a7d06f2c1b09b9c16021d2869563 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Fri, 13 Oct 2023 18:16:01 +0800 Subject: [PATCH 3/5] fix --- t/plugin/limit-req3.t | 3 --- 1 file changed, 3 deletions(-) diff --git a/t/plugin/limit-req3.t b/t/plugin/limit-req3.t index 98c0f351bf59..ada41c396e74 100644 --- a/t/plugin/limit-req3.t +++ b/t/plugin/limit-req3.t @@ -27,9 +27,6 @@ add_block_preprocessor(sub { my $port = $ENV{TEST_NGINX_SERVER_PORT}; my $TEST_NGINX_HTML_DIR ||= html_dir(); - my $config = $block->config // <<_EOC_; - listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl; -_EOC_ $block->set_value("config", $config); From 12e9c2971efcc5b9944538fdeca36a1d349831d7 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Tue, 17 Oct 2023 14:54:09 +0800 Subject: [PATCH 4/5] fix --- t/plugin/limit-req3.t | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/t/plugin/limit-req3.t b/t/plugin/limit-req3.t index ada41c396e74..10e593a66bbc 100644 --- a/t/plugin/limit-req3.t +++ b/t/plugin/limit-req3.t @@ -28,8 +28,6 @@ add_block_preprocessor(sub { my $TEST_NGINX_HTML_DIR ||= html_dir(); - $block->set_value("config", $config); - if (!$block->request) { $block->set_value("request", "GET /t"); } @@ -85,9 +83,6 @@ passed === TEST 2: create ssl(sni: www.test.com) ---- yaml_config -apisix: - node_listen: 1984 --- config location /t { content_by_lua_block { @@ -116,14 +111,7 @@ passed === TEST 3: use HTTP version 2 to request ---- yaml_config -apisix: - ssl: - enable: true - listen: - - port: 9443 - enable_http2: true --- exec -curl --http2 --parallel -k https://www.test.com:9443/hello https://www.test.com:9443/hello --resolve www.test.com:9443:127.0.0.1 +curl --http2 --parallel -k https://www.test.com:1994/hello https://www.test.com:1994/hello --resolve www.test.com:1994:127.0.0.1 --- response_body_like 503 Service Temporarily Unavailable.*.hello world From 8e4512bfda9d3b839496d63cc88560f20dde8068 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Wed, 18 Oct 2023 09:52:05 +0800 Subject: [PATCH 5/5] fix --- t/plugin/limit-req3.t | 3 --- 1 file changed, 3 deletions(-) diff --git a/t/plugin/limit-req3.t b/t/plugin/limit-req3.t index 10e593a66bbc..684eb9d1b405 100644 --- a/t/plugin/limit-req3.t +++ b/t/plugin/limit-req3.t @@ -24,9 +24,6 @@ no_root_location(); add_block_preprocessor(sub { my ($block) = @_; - my $port = $ENV{TEST_NGINX_SERVER_PORT}; - - my $TEST_NGINX_HTML_DIR ||= html_dir(); if (!$block->request) { $block->set_value("request", "GET /t");