feat: support var in upstream_uri on proxy-rewrite plugin (#3139)

This commit is contained in:
Brandon Fergerson 2021-01-04 21:11:09 -05:00 committed by GitHub
parent 3b69b644a4
commit 05b1db7f53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 10 deletions

View File

@ -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")

View File

@ -251,11 +251,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
@ -290,6 +290,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 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.
Refer the following [document](how-to-build.md#test) to setup the testing framework.
### Attach the test-nginx execution process:

View File

@ -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

View File

@ -1249,3 +1249,51 @@ 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": "/$arg_new_uri"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/test"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
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]