From f3469b3f42e613642f4ea1a5ab3f86c5a3d4381f Mon Sep 17 00:00:00 2001 From: Brandon Fergerson Date: Sun, 27 Dec 2020 11:50:55 -0500 Subject: [PATCH 1/6] Update proxy-rewrite.lua added ability to use ctx.var in upstream_uri --- apisix/plugins/proxy-rewrite.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/plugins/proxy-rewrite.lua b/apisix/plugins/proxy-rewrite.lua index 1a5e7017fe32..6a4024f7b608 100644 --- a/apisix/plugins/proxy-rewrite.lua +++ b/apisix/plugins/proxy-rewrite.lua @@ -143,7 +143,7 @@ function _M.rewrite(conf, ctx) local upstream_uri = ctx.var.uri if conf.uri ~= nil then - upstream_uri = conf.uri + upstream_uri = core.utils.resolve_var(conf.uri, ctx.var) elseif conf.regex_uri ~= nil then local uri, _, err = re_sub(ctx.var.uri, conf.regex_uri[1], conf.regex_uri[2], "jo") From 715fa116c2a69cf7a87e603904c82472157e0e74 Mon Sep 17 00:00:00 2001 From: Brandon Fergerson Date: Mon, 28 Dec 2020 21:27:41 -0500 Subject: [PATCH 2/6] started test --- t/plugin/proxy-rewrite.t | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/t/plugin/proxy-rewrite.t b/t/plugin/proxy-rewrite.t index cdb86194671b..ae303865993d 100644 --- a/t/plugin/proxy-rewrite.t +++ b/t/plugin/proxy-rewrite.t @@ -1249,3 +1249,41 @@ version: nginx_var_does_not_exist x-real-ip: 127.0.0.1 --- no_error_log [error] + + + +=== TEST 42: set route(rewrite uri based on ctx.var) +--- 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": { + "proxy-rewrite": { + "uri": "/$consumer_name" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] \ No newline at end of file From bae2e05bc7b49e59544b38999eb856e71976cc02 Mon Sep 17 00:00:00 2001 From: Brandon Fergerson Date: Mon, 28 Dec 2020 21:34:25 -0500 Subject: [PATCH 3/6] EOL --- t/plugin/proxy-rewrite.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/plugin/proxy-rewrite.t b/t/plugin/proxy-rewrite.t index ae303865993d..234ecee7f8c3 100644 --- a/t/plugin/proxy-rewrite.t +++ b/t/plugin/proxy-rewrite.t @@ -1286,4 +1286,4 @@ GET /t --- response_body passed --- no_error_log -[error] \ No newline at end of file +[error] From 5730e2204fc9612cd36c2498fdaa357838b8b0dc Mon Sep 17 00:00:00 2001 From: Brandon Fergerson Date: Sun, 3 Jan 2021 16:24:56 -0500 Subject: [PATCH 4/6] added validation test and some clarifications in documentation --- doc/plugin-develop.md | 10 +++++++--- doc/plugins/proxy-rewrite.md | 12 ++++++------ t/plugin/proxy-rewrite.t | 14 ++++++++++++-- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/doc/plugin-develop.md b/doc/plugin-develop.md index d3c3f6720725..b906ae9c0733 100644 --- a/doc/plugin-develop.md +++ b/doc/plugin-develop.md @@ -246,11 +246,11 @@ Write the logic of the plugin in the corresponding phase. ## write test case -For functions, write and improve the test cases of various dimensions, do a comprehensive test for your plugin ! The +For functions, write and improve the test cases of various dimensions, do a comprehensive test for your plugin! The test cases of plugins are all in the "__t/plugin__" directory. You can go ahead to find out. APISIX uses -[****test-nginx****](https://github.com/openresty/test-nginx) as the test framework. A test case,.t file is usually +[****test-nginx****](https://github.com/openresty/test-nginx) as the test framework. A test case (.t file) is usually divided into prologue and data parts by \__data\__. Here we will briefly introduce the data part, that is, the part -of the real test case. For example, the key-auth plugin : +of the real test case. For example, the key-auth plugin: ```perl === TEST 1: sanity @@ -285,6 +285,10 @@ When we request __/t__, which config in the configuration file, the Nginx will c "__no_error_log__" means to check the "__error.log__" of Nginx. There must be no ERROR level record. The log files for the unit test are located in the following folder: 't/servroot/logs'. +The above test case represents a simple scenario. Most scenarios will require multiple tests to validate. To do this, create multiple tests `=== TEST 1`, `=== TEST 2`, and so on. These tests will be executed sequentially, allowing you to break down scenarios into a sequence of atomic steps. + +Additionally, there are some convenience testing endpoints which can be found [here](https://github.com/apache/apisix/blob/master/t/lib/server.lua#L36). For example, see [proxy-rewrite](https://github.com/apache/apisix/blob/master/t/plugin/proxy-rewrite.lua). In test 42, the upstream `uri` is made to redirect `/test?new_uri=hello` to `/hello` (which always returns `hello world`). In test 43, the response body is validated to equal to `hello world`, meaning the proxy-rewrite configuration added with test 42 worked correctly. + Refer the following [document](how-to-build.md#test) to setup the testing framework. ### Attach the test-nginx execution process: diff --git a/doc/plugins/proxy-rewrite.md b/doc/plugins/proxy-rewrite.md index 0f9bb01acb3d..8dcf2a321bb9 100644 --- a/doc/plugins/proxy-rewrite.md +++ b/doc/plugins/proxy-rewrite.md @@ -33,13 +33,13 @@ The `proxy-rewrite` is an upstream proxy information rewriting plugin, which sup ## Attributes -| Name | Type | Requirement | Default | Valid | Description | -| --------- | ------------- | ----------- | ------- | ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| scheme | string | optional | "http" | ["http", "https"] | Upstream new `schema` forwarding protocol. | -| uri | string | optional | | | Upstream new `uri` forwarding address. | +| Name | Type | Requirement | Default | Valid | Description | +| --------- | ------------- | ----------- | ------- | ----------------- | ------------------------------------------------------------ | +| scheme | string | optional | "http" | ["http", "https"] | Upstream new `schema` forwarding protocol. | +| uri | string | optional | | | Upstream new `uri` forwarding address. Supports the use of [Nginx variables](https://nginx.org/en/docs/http/ngx_http_core_module.html). Variables must start with `$`, such as `$arg_name`. | | regex_uri | array[string] | optional | | | Upstream new `uri` forwarding address. Use regular expression to match URL from client, when the match is successful, the URL template will be forwarded upstream. If the match is not successful, the URL from the client will be forwarded to the upstream. When `uri` and` regex_uri` are both exist, `uri` is used first. For example: [" ^/iresty/(.*)/(.*)/(.*)", "/$1-$2-$3"], the first element represents the matching regular expression and the second element represents the URL template that is forwarded to the upstream. | -| host | string | optional | | | Upstream new `host` forwarding address, example `iresty.com`. | -| headers | object | optional | | | Forward to the new `headers` of the upstream, can set up multiple. If it exists, will rewrite the header, otherwise will add the header. You can set the corresponding value to an empty string to remove a header. Support the use of Nginx variables. Need to start with `$`, such as `client_addr: $remote_addr`: it means that the request header `client_addr` is the client IP. | +| host | string | optional | | | Upstream new `host` forwarding address, example `iresty.com`. | +| headers | object | optional | | | Forward to the new `headers` of the upstream, can set up multiple. If it exists, will rewrite the header, otherwise will add the header. You can set the corresponding value to an empty string to remove a header. Support the use of Nginx variables. Need to start with `$`, such as `client_addr: $remote_addr`: it means that the request header `client_addr` is the client IP. | ## How To Enable diff --git a/t/plugin/proxy-rewrite.t b/t/plugin/proxy-rewrite.t index 234ecee7f8c3..a7430109e686 100644 --- a/t/plugin/proxy-rewrite.t +++ b/t/plugin/proxy-rewrite.t @@ -1262,7 +1262,7 @@ x-real-ip: 127.0.0.1 [[{ "plugins": { "proxy-rewrite": { - "uri": "/$consumer_name" + "uri": "/$arg_new_uri" } }, "upstream": { @@ -1271,7 +1271,7 @@ x-real-ip: 127.0.0.1 }, "type": "roundrobin" }, - "uri": "/hello" + "uri": "/test" }]] ) @@ -1287,3 +1287,13 @@ GET /t passed --- no_error_log [error] + + + +=== TEST 43: hit route(upstream uri: should be /hello) +--- request +GET /test?new_uri=hello +--- response_body +hello world +--- no_error_log +[error] From 32d71a779090f4904d56b7c994447b3a6328773c Mon Sep 17 00:00:00 2001 From: Brandon Fergerson Date: Sun, 3 Jan 2021 16:27:08 -0500 Subject: [PATCH 5/6] wording --- doc/plugin-develop.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/plugin-develop.md b/doc/plugin-develop.md index b906ae9c0733..d71702d7ab22 100644 --- a/doc/plugin-develop.md +++ b/doc/plugin-develop.md @@ -287,7 +287,7 @@ are located in the following folder: 't/servroot/logs'. The above test case represents a simple scenario. Most scenarios will require multiple tests to validate. To do this, create multiple tests `=== TEST 1`, `=== TEST 2`, and so on. These tests will be executed sequentially, allowing you to break down scenarios into a sequence of atomic steps. -Additionally, there are some convenience testing endpoints which can be found [here](https://github.com/apache/apisix/blob/master/t/lib/server.lua#L36). For example, see [proxy-rewrite](https://github.com/apache/apisix/blob/master/t/plugin/proxy-rewrite.lua). In test 42, the upstream `uri` is made to redirect `/test?new_uri=hello` to `/hello` (which always returns `hello world`). In test 43, the response body is validated to equal to `hello world`, meaning the proxy-rewrite configuration added with test 42 worked correctly. +Additionally, there are some convenience testing endpoints which can be found [here](https://github.com/apache/apisix/blob/master/t/lib/server.lua#L36). For example, see [proxy-rewrite](https://github.com/apache/apisix/blob/master/t/plugin/proxy-rewrite.lua). In test 42, the upstream `uri` is made to redirect `/test?new_uri=hello` to `/hello` (which always returns `hello world`). In test 43, the response body is confirmed to equal `hello world`, meaning the proxy-rewrite configuration added with test 42 worked correctly. Refer the following [document](how-to-build.md#test) to setup the testing framework. From b7fda5caaa5bbad61f2f12e1c60fd20f2acd9ab7 Mon Sep 17 00:00:00 2001 From: Brandon Fergerson Date: Sun, 3 Jan 2021 21:35:48 -0500 Subject: [PATCH 6/6] Update doc/plugin-develop.md --- doc/plugin-develop.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/plugin-develop.md b/doc/plugin-develop.md index d71702d7ab22..b5e379278bc8 100644 --- a/doc/plugin-develop.md +++ b/doc/plugin-develop.md @@ -285,7 +285,7 @@ When we request __/t__, which config in the configuration file, the Nginx will c "__no_error_log__" means to check the "__error.log__" of Nginx. There must be no ERROR level record. The log files for the unit test are located in the following folder: 't/servroot/logs'. -The above test case represents a simple scenario. Most scenarios will require multiple tests to validate. To do this, create multiple tests `=== TEST 1`, `=== TEST 2`, and so on. These tests will be executed sequentially, allowing you to break down scenarios into a sequence of atomic steps. +The above test case represents a simple scenario. Most scenarios will require multiple steps to validate. To do this, create multiple tests `=== TEST 1`, `=== TEST 2`, and so on. These tests will be executed sequentially, allowing you to break down scenarios into a sequence of atomic steps. Additionally, there are some convenience testing endpoints which can be found [here](https://github.com/apache/apisix/blob/master/t/lib/server.lua#L36). For example, see [proxy-rewrite](https://github.com/apache/apisix/blob/master/t/plugin/proxy-rewrite.lua). In test 42, the upstream `uri` is made to redirect `/test?new_uri=hello` to `/hello` (which always returns `hello world`). In test 43, the response body is confirmed to equal `hello world`, meaning the proxy-rewrite configuration added with test 42 worked correctly.