Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apisix/plugins/proxy-rewrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 7 additions & 3 deletions doc/plugin-develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
BFergerson marked this conversation as resolved.
Show resolved Hide resolved
BFergerson marked this conversation as resolved.
Show resolved Hide resolved

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:
Expand Down
12 changes: 6 additions & 6 deletions doc/plugins/proxy-rewrite.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 48 additions & 0 deletions t/plugin/proxy-rewrite.t
Original file line number Diff line number Diff line change
Expand Up @@ -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)
BFergerson marked this conversation as resolved.
Show resolved Hide resolved
--- 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]