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

fix(proxy-mirror): use with uri rewrite #8718

Merged
merged 2 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 12 additions & 6 deletions apisix/plugins/proxy-mirror.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,20 @@ end


local function enable_mirror(ctx, conf)
if conf.path and conf.path_concat_mode == "prefix" then
ctx.var.upstream_mirror_uri = resolver_host(conf.host) .. conf.path .. ctx.var.uri ..
ctx.var.is_args .. (ctx.var.args or '')
else
ctx.var.upstream_mirror_uri = resolver_host(conf.host) .. (conf.path or ctx.var.uri) ..
ctx.var.is_args .. (ctx.var.args or '')
local uri = (ctx.var.upstream_uri and ctx.var.upstream_uri ~= "") and
ctx.var.upstream_uri or
ctx.var.uri .. ctx.var.is_args .. (ctx.var.args or '')

if conf.path then
if conf.path_concat_mode == "prefix" then
uri = conf.path .. uri
else
uri = conf.path .. ctx.var.is_args .. (ctx.var.args or '')
end
end

ctx.var.upstream_mirror_uri = resolver_host(conf.host) .. uri

if has_mod then
apisix_ngx_client.enable_mirror()
end
Expand Down
63 changes: 63 additions & 0 deletions t/plugin/proxy-mirror.t
Original file line number Diff line number Diff line change
Expand Up @@ -838,3 +838,66 @@ GET /hello?a=1
hello world
--- error_log
uri: /a/hello?a=1



=== TEST 30: use proxy-rewrite to change uri before mirror
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move it to a new file.

When the test file is too large, for example > 800 lines, you should split it to a new file. Please take a look at t/plugin/limit-conn.t and t/plugin/limit-conn2.t.

https://github.com/apache/apisix/blob/master/CONTRIBUTING.md#check-code-style-and-test-case-style

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved

--- 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":{
"_meta": {
"priority": 1010
},
"uri": "/hello"
},
"proxy-mirror": {
"_meta": {
"priority": 1008
},
"host": "http://127.0.0.1:1986"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/nope"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed



=== TEST 31: hit route (with proxy-rewrite)
--- request
GET /nope
--- response_body
hello world
--- error_log
uri: /hello



=== TEST 32: hit route (with proxy-rewrite and args)
--- request
GET /nope?a=b&b=c&c=d
--- response_body
hello world
--- error_log
uri: /hello?a=b&b=c&c=d