You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by mo...@apache.org on 2023/05/10 03:06:28 UTC

[apisix] branch master updated: fix: body-transformer plugin return raw body anytime (#9446)

This is an automated email from the ASF dual-hosted git repository.

monkeydluffy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new c02abcfb4 fix: body-transformer plugin return raw body anytime (#9446)
c02abcfb4 is described below

commit c02abcfb47a9905210c6235082107ef6d31d2689
Author: Sn0rt <wa...@gmail.com>
AuthorDate: Wed May 10 11:06:22 2023 +0800

    fix: body-transformer plugin return raw body anytime (#9446)
---
 apisix/plugins/body-transformer.lua |  3 +-
 t/plugin/body-transformer.t         | 57 +++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/apisix/plugins/body-transformer.lua b/apisix/plugins/body-transformer.lua
index 9ca6b86ba..1d1afa06e 100644
--- a/apisix/plugins/body-transformer.lua
+++ b/apisix/plugins/body-transformer.lua
@@ -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
@@ -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)
diff --git a/t/plugin/body-transformer.t b/t/plugin/body-transformer.t
index be07f9ac0..fd21621cb 100644
--- a/t/plugin/body-transformer.t
+++ b/t/plugin/body-transformer.t
@@ -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"}')
+        }
+    }