Skip to content

Commit

Permalink
fix(body-transformer): add nil check to conf (#11768)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreemaan-abhishek authored Nov 25, 2024
1 parent e38dab1 commit a0b1a45
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apisix/plugins/body-transformer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ end

function _M.body_filter(_, ctx)
local conf = ctx.body_transformer_conf
if not conf then
return
end
if conf.response then
local body = core.response.hold_body_chunk(ctx)
if ngx.arg[2] == false and not body then
Expand Down
45 changes: 45 additions & 0 deletions t/plugin/body-transformer2.t
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,48 @@ POST /echo
{"name": "foo", "address":"LA", "age": 18}
-- response_body
{"name": "bar", "age": 19}
=== TEST 3: body transformer plugin with key-auth that fails
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin")
local core = require("apisix.core")
local code, body = t.test('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/foobar",
"plugins": {
"body-transformer": {
"request": {
"template": "some-template"
}
},
"key-auth": {}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}]]
)
if code >= 300 then
ngx.status = code
return
end
ngx.sleep(0.5)
local http = require("resty.http")
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/foobar"
local opt = {method = "POST", body = "body", headers = {["Content-Type"] = "application/json"}}
local httpc = http.new()
local res = httpc:request_uri(uri, opt)
assert(res.status == 401)
ngx.say(res.reason)
}
}
--- response_body
Unauthorized

0 comments on commit a0b1a45

Please sign in to comment.