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: body-transformer plugin return raw body anytime #9446

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion apisix/plugins/body-transformer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ end


local function transform(conf, body, typ, ctx)
local out = {_body = body}
local out = {}
if body then
local err
local format = conf[typ].input_format
Expand All @@ -137,6 +137,7 @@ local function transform(conf, body, typ, ctx)
end

out._ctx = ctx
out._body = body
out._escape_xml = escape_xml
out._escape_json = escape_json
local ok, render_out = pcall(render, out)
Expand Down
57 changes: 57 additions & 0 deletions t/plugin/body-transformer.t
Original file line number Diff line number Diff line change
Expand Up @@ -764,3 +764,60 @@ location /demo {
assert(res2.headers["Apisix-Cache-Status"] == "HIT")
}
}



=== TEST 11: return raw body with _body anytime
--- http_config
--- config
location /demo {
content_by_lua_block {
ngx.header.content_type = "application/json"
ngx.print('{"result": "hello world"}')
}
}

location /t {
content_by_lua_block {
local t = require("lib.test_admin")

local rsp_template = ngx.encode_base64[[
{"raw_body": {*_escape_json(_body)*}, "result": {*_escape_json(result)*}}
]]

local code, body = t.test('/apisix/admin/routes/1',
ngx.HTTP_PUT,
string.format([[{
"uri": "/capital",
"plugins": {
"proxy-rewrite": {
"uri": "/demo"
},
"body-transformer": {
"response": {
"template": "%s"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:%d": 1
}
}
}]], rsp_template, ngx.var.server_port)
)

local core = require("apisix.core")
local http = require("resty.http")
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/capital"
local opt = {method = "GET", headers = {["Content-Type"] = "application/json"}}
local httpc = http.new()

local res = httpc:request_uri(uri, opt)
assert(res.status == 200)
local data = core.json.decode(res.body)
assert(data.result == "hello world")
assert(data.raw_body == '{"result": "hello world"}')
}
}