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-rewrite): when conf.headers are missing,conf.method can make effect #6300

Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 11 additions & 13 deletions apisix/plugins/proxy-rewrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,20 @@ function _M.rewrite(conf, ctx)
ctx.var.upstream_uri = upstream_uri
end

if not conf.headers then
return
end

if not conf.headers_arr then
conf.headers_arr = {}
if conf.headers then
if not conf.headers_arr then
conf.headers_arr = {}

for field, value in pairs(conf.headers) do
core.table.insert_tail(conf.headers_arr, field, value)
for field, value in pairs(conf.headers) do
core.table.insert_tail(conf.headers_arr, field, value)
end
end
end

local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
core.request.set_header(ctx, conf.headers_arr[i],
core.utils.resolve_var(conf.headers_arr[i+1], ctx.var))
local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
core.request.set_header(ctx, conf.headers_arr[i],
core.utils.resolve_var(conf.headers_arr[i+1], ctx.var))
end
end

if conf.method then
Expand Down
54 changes: 54 additions & 0 deletions t/plugin/proxy-rewrite3.t
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,57 @@ plugin_proxy_rewrite get method: GET
--- response_body
property "method" validation failed: matches none of the enum values
done



=== TEST 6: set route(rewrite method with headers)
liangliang4ward 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,
[[{
"methods": ["GET"],
"plugins": {
"proxy-rewrite": {
"uri": "/uri/plugin_proxy_rewrite",
"method": "POST",
"scheme": "http",
"host": "apisix.iresty.com",
"headers":{
"x-api-version":"v1"
}
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

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



=== TEST 7: hit route(with out header)
liangliang4ward marked this conversation as resolved.
Show resolved Hide resolved
--- request
GET /hello HTTP/1.1
--- response_body
uri: /uri/plugin_proxy_rewrite
host: apisix.iresty.com
x-api-version: v1
x-real-ip: 127.0.0.1
--- grep_error_log_out
liangliang4ward marked this conversation as resolved.
Show resolved Hide resolved
plugin_proxy_rewrite get method: POST