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

[apisix] branch master updated: feat: Body transformer plugin (#8766)

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

spacewander 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 cfeadfbed feat: Body transformer plugin (#8766)
cfeadfbed is described below

commit cfeadfbedae1e23f0beef2a6e417446e9689c57e
Author: jinhua luo <ho...@163.com>
AuthorDate: Fri Feb 10 10:18:21 2023 +0800

    feat: Body transformer plugin (#8766)
---
 apisix/plugins/body-transformer.lua        | 211 ++++++++++
 conf/config-default.yaml                   |   1 +
 docs/en/latest/config.json                 |   3 +-
 docs/en/latest/plugins/body-transformer.md | 277 ++++++++++++
 t/admin/plugins.t                          |   1 +
 t/plugin/body-transformer.t                | 654 +++++++++++++++++++++++++++++
 6 files changed, 1146 insertions(+), 1 deletion(-)

diff --git a/apisix/plugins/body-transformer.lua b/apisix/plugins/body-transformer.lua
new file mode 100644
index 000000000..9ca6b86ba
--- /dev/null
+++ b/apisix/plugins/body-transformer.lua
@@ -0,0 +1,211 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local core              = require("apisix.core")
+local xml2lua           = require("xml2lua")
+local xmlhandler        = require("xmlhandler.tree")
+local template          = require("resty.template")
+local ngx               = ngx
+local decode_base64     = ngx.decode_base64
+local req_set_body_data = ngx.req.set_body_data
+local str_format        = string.format
+local type              = type
+local pcall             = pcall
+local pairs             = pairs
+
+
+local transform_schema = {
+    type = "object",
+    properties = {
+        input_format = { type = "string", enum = {"xml", "json"} },
+        template = { type = "string" },
+    },
+    required = {"template"},
+}
+
+local schema = {
+    type = "object",
+    properties = {
+        request = transform_schema,
+        response = transform_schema,
+    },
+    anyOf = {
+        {required = {"request"}},
+        {required = {"response"}},
+        {required = {"request", "response"}},
+    },
+}
+
+
+local _M = {
+    version  = 0.1,
+    priority = 1080,
+    name     = "body-transformer",
+    schema   = schema,
+}
+
+
+local function escape_xml(s)
+    return s:gsub("&", "&amp;")
+        :gsub("<", "&lt;")
+        :gsub(">", "&gt;")
+        :gsub("'", "&apos;")
+        :gsub('"', "&quot;")
+end
+
+
+local function escape_json(s)
+    return core.json.encode(s)
+end
+
+
+local function remove_namespace(tbl)
+    for k, v in pairs(tbl) do
+        if type(k) == "string" then
+            local newk = k:match(".*:(.*)")
+            if newk then
+                tbl[newk] = v
+                tbl[k] = nil
+            end
+            if type(v) == "table" then
+                remove_namespace(v)
+            end
+        end
+    end
+    return tbl
+end
+
+
+local decoders = {
+    xml = function(data)
+        local handler = xmlhandler:new()
+        local parser = xml2lua.parser(handler)
+        local ok, err = pcall(parser.parse, parser, data)
+        if ok then
+            return remove_namespace(handler.root)
+        else
+            return nil, err
+        end
+    end,
+    json = function(data)
+        return core.json.decode(data)
+    end,
+}
+
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+
+local function transform(conf, body, typ, ctx)
+    local out = {_body = body}
+    if body then
+        local err
+        local format = conf[typ].input_format
+        if format then
+            out, err = decoders[format](body)
+            if not out then
+                err = str_format("%s body decode: %s", typ, err)
+                core.log.error(err, ", body=", body)
+                return nil, 400, err
+            end
+        end
+    end
+
+    local text = conf[typ].template
+    text = decode_base64(text) or text
+    local ok, render = pcall(template.compile, text)
+    if not ok then
+        local err = render
+        err = str_format("%s template compile: %s", typ, err)
+        core.log.error(err)
+        return nil, 503, err
+    end
+
+    out._ctx = ctx
+    out._escape_xml = escape_xml
+    out._escape_json = escape_json
+    local ok, render_out = pcall(render, out)
+    if not ok then
+        local err = str_format("%s template rendering: %s", typ, render_out)
+        core.log.error(err)
+        return nil, 503, err
+    end
+
+    core.log.info(typ, " body transform output=", render_out)
+    return render_out
+end
+
+
+local function set_input_format(conf, typ, ct)
+    if conf[typ].input_format == nil and ct then
+        if ct:find("text/xml") then
+            conf[typ].input_format = "xml"
+        elseif ct:find("application/json") then
+            conf[typ].input_format = "json"
+        end
+    end
+end
+
+
+function _M.rewrite(conf, ctx)
+    if conf.request then
+        conf = core.table.deepcopy(conf)
+        ctx.body_transformer_conf = conf
+        local body = core.request.get_body()
+        set_input_format(conf, "request", ctx.var.http_content_type)
+        local out, status, err = transform(conf, body, "request", ctx)
+        if not out then
+            return status, { message = err }
+        end
+        req_set_body_data(out)
+    end
+end
+
+
+function _M.header_filter(conf, ctx)
+    if conf.response then
+        if not ctx.body_transformer_conf then
+            conf = core.table.deepcopy(conf)
+            ctx.body_transformer_conf = conf
+        end
+        set_input_format(conf, "response", ngx.header.content_type)
+        core.response.clear_header_as_body_modified()
+    end
+end
+
+
+function _M.body_filter(_, ctx)
+    local conf = ctx.body_transformer_conf
+    if conf.response then
+        local body = core.response.hold_body_chunk(ctx)
+        if ngx.arg[2] == false and not body then
+            return
+        end
+
+        local out = transform(conf, body, "response", ctx)
+        if not out then
+            core.log.error("failed to transform response body: ", body)
+            return
+        end
+
+        ngx.arg[1] = out
+    end
+end
+
+
+return _M
diff --git a/conf/config-default.yaml b/conf/config-default.yaml
index 3fcd7505b..db5cb2211 100755
--- a/conf/config-default.yaml
+++ b/conf/config-default.yaml
@@ -443,6 +443,7 @@ plugins:                          # plugin list (sorted by priority)
   - opa                            # priority: 2001
   - authz-keycloak                 # priority: 2000
   #- error-log-logger              # priority: 1091
+  - body-transformer               # priority: 1080
   - proxy-mirror                   # priority: 1010
   - proxy-cache                    # priority: 1009
   - proxy-rewrite                  # priority: 1008
diff --git a/docs/en/latest/config.json b/docs/en/latest/config.json
index 31fa6dd1d..27dbbddca 100644
--- a/docs/en/latest/config.json
+++ b/docs/en/latest/config.json
@@ -74,7 +74,8 @@
             "plugins/grpc-transcode",
             "plugins/grpc-web",
             "plugins/fault-injection",
-            "plugins/mocking"
+            "plugins/mocking",
+            "plugins/body-transformer"
           ]
         },
         {
diff --git a/docs/en/latest/plugins/body-transformer.md b/docs/en/latest/plugins/body-transformer.md
new file mode 100644
index 000000000..8e5bb0064
--- /dev/null
+++ b/docs/en/latest/plugins/body-transformer.md
@@ -0,0 +1,277 @@
+---
+title: body-transformer
+keywords:
+  - APISIX
+  - Plugin
+  - BODY TRANSFORMER
+  - body-transformer
+description: This document contains information about the Apache APISIX body-transformer Plugin.
+---
+
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+## Description
+
+This plugin is used to transform the request and/or response body from one
+format to another format, e.g. JSON to XML.
+
+Use cases:
+
+- simple SOAP proxy
+- generic template-based transform, e.g. JSON to JSON, JSON to HTML, XML to YAML
+
+## Attributes
+
+| Name      | Type | Required      | Description |
+| ----------- | ----------- | ----------- | ----------- |
+| `request`      | object       | False      | request body transformation configuration      |
+| `request.input_format`      | string       | False      | request body original format, if not specified, it would be determined from `Content-Type` header.      |
+| `request.template`      | string       | True      | request body transformation template       |
+| `response`      | object       | False      | response body transformation configuration      |
+| `response.input_format`      | string       | False      | response body original format, if not specified, it would be determined from `Content-Type` header.       |
+| `response.template`      | string       | True      | response body transformation template       |
+
+## Enabling the Plugin
+
+You can enable the Plugin on a specific Route as shown below:
+
+```shell
+curl http://127.0.0.1:9180/apisix/admin/routes/test_ws \
+    -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "methods": ["POST"],
+    "uri": "/ws",
+    "plugins": {
+        "body-transformer": {
+            "request": {
+                "template": "..."
+            },
+            "response": {
+                "template": "..."
+            }
+        }
+    },
+    "upstream": {
+        "type": "roundrobin",
+        "nodes": {
+            "localhost:8080": 1
+        }
+    }
+}'
+```
+
+## Configuration description
+
+The `request` and `response` correspond to configurations of request body and response body transformation perspectively.
+
+Specify one of them, or both of them, to fit your need.
+
+`request`/`response`:
+
+* `input_format` specifies the body original format:
+  * `xml` (`text/xml`)
+  * `json` (`application/json`)
+* `template` specifies the [template](https://github.com/bungle/lua-resty-template) text used by transformation.
+
+**Notes:**
+
+`{{ ... }}` in lua-resty-template will do html-escape, e.g. space character, so if it's not what you wish, use `{* ... *}` instead.
+
+If you do not specify `input_format` and no `Content-Type` header, or body is `nil`, then this plugin will not parse the body before template rendering.
+In any case, you could access body string via `{{ _body }}`.
+
+This is useful for below use cases:
+
+* you wish to generate body from scratch based on Nginx/APISIX variables, even if the original body is `nil`.
+* you wish to parse the body string yourself in the template via other lua modules, e.g. parse protobuf.
+
+For example, parse YAML to JSON yourself:
+
+```
+{%
+    local yaml = require("tinyyaml")
+    local body = yaml.parse(_body)
+%}
+{"foobar":"{{body.foobar.foo .. " " .. body.foobar.bar}}"}
+```
+
+You must ensure `template` is a valid JSON string, i.e. you need to take care of special characters escape, e.g. double quote.
+If it's cumbersome to escape big text file or complex file, you could use encode your template text file in base64 format instead.
+
+For example, you could use `base64` command to encode your template text file:
+
+```bash
+curl http://127.0.0.1:9180/apisix/admin/routes/test_ws \
+    -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "methods": ["POST"],
+    "uri": "/ws",
+    "plugins": {
+        "body-transformer": {
+            "request": {
+                "template": "'"$(base64 -w0 /path/to/my_template_file)"'"
+            }
+        }
+    },
+    "upstream": {
+        "type": "roundrobin",
+        "nodes": {
+            "localhost:8080": 1
+        }
+    }
+}'
+```
+
+In `template`, you can use below auxiliary functions to escape string to fit specific format:
+
+* `_escape_json()`
+* `_escape_xml()`
+
+Note that `_escape_json()` would double quote the value of string type, so don't repeat double-quote in the template, e.g. `{"foobar":{*_escape_json(name)*}}`.
+
+And, you can refer to `_ctx` to access nginx request context, e.g. `{{ _ctx.var.status }}`.
+
+## Example
+
+Let's take a simple SOAP proxy as example.
+
+* from downstream to upstream, it transforms the request body from JSON to XML
+* from upstream to downstream, it transforms the response body from XML to JSON
+  * the response `template` distinguishes the normal response from the fault response
+
+### Run a test web service server
+
+```bash
+cd /tmp
+git clone https://github.com/spring-guides/gs-soap-service.git
+cd gs-soap-service
+./mvnw spring-boot:run
+```
+
+### Test
+
+```bash
+req_template=$(cat <<EOF | awk '{gsub(/"/,"\\\"");};1' | awk '{$1=$1};1' | tr -d '\r\n'
+<?xml version="1.0"?>
+<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
+ <soap-env:Body>
+  <ns0:getCountryRequest xmlns:ns0="http://spring.io/guides/gs-producing-web-service">
+   <ns0:name>{{_escape_xml(name)}}</ns0:name>
+  </ns0:getCountryRequest>
+ </soap-env:Body>
+</soap-env:Envelope>
+EOF
+)
+
+rsp_template=$(cat <<EOF | awk '{gsub(/"/,"\\\"");};1' | awk '{$1=$1};1' | tr -d '\r\n'
+{% if Envelope.Body.Fault == nil then %}
+{
+   "status":"{{_ctx.var.status}}",
+   "currency":"{{Envelope.Body.getCountryResponse.country.currency}}",
+   "population":{{Envelope.Body.getCountryResponse.country.population}},
+   "capital":"{{Envelope.Body.getCountryResponse.country.capital}}",
+   "name":"{{Envelope.Body.getCountryResponse.country.name}}"
+}
+{% else %}
+{
+   "message":{*_escape_json(Envelope.Body.Fault.faultstring[1])*},
+   "code":"{{Envelope.Body.Fault.faultcode}}"
+   {% if Envelope.Body.Fault.faultactor ~= nil then %}
+   , "actor":"{{Envelope.Body.Fault.faultactor}}"
+   {% end %}
+}
+{% end %}
+EOF
+)
+
+curl http://127.0.0.1:9180/apisix/admin/routes/test_ws \
+    -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "methods": ["POST"],
+    "uri": "/ws",
+    "plugins": {
+        "proxy-rewrite": {
+            "headers": {
+                "set": {
+                    "Accept-Encoding": "identity",
+                    "Content-Type": "text/xml"
+                }
+            }
+        },
+        "response-rewrite": {
+            "headers": {
+                "set": {
+                    "Content-Type": "application/json"
+                }
+            }
+        },
+        "body-transformer": {
+            "request": {
+                "template": "'"$req_template"'"
+            },
+            "response": {
+                "template": "'"$rsp_template"'"
+            }
+        }
+    },
+    "upstream": {
+        "type": "roundrobin",
+        "nodes": {
+            "localhost:8080": 1
+        }
+    }
+}'
+
+curl -s http://127.0.0.1:9080/ws -H 'content-type: application/json' -X POST -d '{"name": "Spain"}' | jq
+{
+  "status": "200",
+  "currency": "EUR",
+  "population": 46704314,
+  "capital": "Madrid",
+  "name": "Spain"
+}
+
+# Fault response
+curl -s http://127.0.0.1:9080/ws -H 'content-type: application/json' -X POST -d '{"name": "Spain"}' | jq
+{
+  "message": "Your name is required.",
+  "code": "SOAP-ENV:Server"
+}
+```
+
+## Disable Plugin
+
+To disable the `body-transformer` Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.
+
+```shell
+curl http://127.0.0.1:9180/apisix/admin/routes/test_ws \
+    -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
+{
+    "methods": ["POST"],
+    "uri": "/ws",
+    "plugins": {},
+    "upstream": {
+        "type": "roundrobin",
+        "nodes": {
+            "localhost:8080": 1
+        }
+    }
+}'
+```
diff --git a/t/admin/plugins.t b/t/admin/plugins.t
index 9791278e2..361b8e84c 100644
--- a/t/admin/plugins.t
+++ b/t/admin/plugins.t
@@ -88,6 +88,7 @@ consumer-restriction
 forward-auth
 opa
 authz-keycloak
+body-transformer
 proxy-mirror
 proxy-cache
 proxy-rewrite
diff --git a/t/plugin/body-transformer.t b/t/plugin/body-transformer.t
new file mode 100644
index 000000000..ae2211de4
--- /dev/null
+++ b/t/plugin/body-transformer.t
@@ -0,0 +1,654 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+use t::APISIX 'no_plan';
+
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: simulate simple SOAP proxy
+--- config
+location /demo {
+    content_by_lua_block {
+        local core = require("apisix.core")
+        local body = core.request.get_body()
+        local xml2lua = require("xml2lua")
+        local xmlhandler = require("xmlhandler.tree")
+        local handler = xmlhandler:new()
+        local parser = xml2lua.parser(handler)
+        parser:parse(body)
+
+        ngx.print(string.format([[
+<SOAP-ENV:Envelope
+    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
+    <SOAP-ENV:Header/>
+    <SOAP-ENV:Body>
+        <ns2:getCountryResponse
+            xmlns:ns2="http://spring.io/guides/gs-producing-web-service">
+            <ns2:country>
+                <ns2:name>%s</ns2:name>
+                <ns2:population>46704314</ns2:population>
+                <ns2:capital>Madrid</ns2:capital>
+                <ns2:currency>EUR</ns2:currency>
+            </ns2:country>
+        </ns2:getCountryResponse>
+    </SOAP-ENV:Body>
+</SOAP-ENV:Envelope>
+        ]], handler.root["soap-env:Envelope"]["soap-env:Body"]["ns0:getCountryRequest"]["ns0:name"]))
+    }
+}
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+
+            local req_template = ngx.encode_base64[[
+<?xml version="1.0"?>
+<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
+ <soap-env:Body>
+  <ns0:getCountryRequest xmlns:ns0="http://spring.io/guides/gs-producing-web-service">
+   <ns0:name>{{_escape_xml(name)}}</ns0:name>
+  </ns0:getCountryRequest>
+ </soap-env:Body>
+</soap-env:Envelope>
+            ]]
+
+            local rsp_template = ngx.encode_base64[[
+{% if Envelope.Body.Fault == nil then %}
+{
+   "status":"{{_ctx.var.status}}",
+   "currency":"{{Envelope.Body.getCountryResponse.country.currency}}",
+   "population":{{Envelope.Body.getCountryResponse.country.population}},
+   "capital":"{{Envelope.Body.getCountryResponse.country.capital}}",
+   "name":"{{Envelope.Body.getCountryResponse.country.name}}"
+}
+{% else %}
+{
+   "message":{*_escape_json(Envelope.Body.Fault.faultstring[1])*},
+   "code":"{{Envelope.Body.Fault.faultcode}}"
+   {% if Envelope.Body.Fault.faultactor ~= nil then %}
+   , "actor":"{{Envelope.Body.Fault.faultactor}}"
+   {% end %}
+}
+{% end %}
+            ]]
+
+            local code, body = t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                string.format([[{
+                    "uri": "/ws",
+                    "plugins": {
+                        "proxy-rewrite": {
+                            "uri": "/demo"
+                        },
+                        "body-transformer": {
+                            "request": {
+                                "template": "%s"
+                            },
+                            "response": {
+                                "input_format": "xml",
+                                "template": "%s"
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:%d": 1
+                        }
+                    }
+                }]], req_template, rsp_template, ngx.var.server_port)
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.sleep(0.5)
+
+            local core = require("apisix.core")
+            local http = require("resty.http")
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/ws"
+            local body = [[{"name": "Spain"}]]
+            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 == 200)
+            local data1 = core.json.decode(res.body)
+            local data2 = core.json.decode[[{"status":"200","currency":"EUR","population":46704314,"capital":"Madrid","name":"Spain"}]]
+            assert(core.json.stably_encode(data1), core.json.stably_encode(data2))
+        }
+    }
+
+
+
+=== TEST 2: test JSON-to-JSON
+--- config
+    location /demo {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local body = core.request.get_body()
+            local data = core.json.decode(body)
+            assert(data.foo == "hello world" and data.bar == 30)
+        }
+    }
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+            local core = require("apisix.core")
+            local req_template = [[{"foo":"{{name .. " world"}}","bar":{{age+10}}}]]
+
+            local code, body = t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                string.format([[{
+                    "uri": "/foobar",
+                    "plugins": {
+                        "proxy-rewrite": {
+                            "uri": "/demo"
+                        },
+                        "body-transformer": {
+                            "request": {
+                                "template": "%s"
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:%d": 1
+                        }
+                    }
+                }]], req_template:gsub('"', '\\"'), ngx.var.server_port)
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.sleep(0.5)
+
+            local core = require("apisix.core")
+            local http = require("resty.http")
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/foobar"
+            local body = [[{"name":"hello","age":20}]]
+            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 == 200)
+        }
+    }
+
+
+
+=== TEST 3: specify wrong input_format
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+            local core = require("apisix.core")
+            local req_template = [[{"foo":"{{name .. " world"}}","bar":{{age+10}}}]]
+
+            local code, body = t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                string.format([[{
+                    "uri": "/foobar",
+                    "plugins": {
+                        "proxy-rewrite": {
+                            "uri": "/demo"
+                        },
+                        "body-transformer": {
+                            "request": {
+                                "input_format": "xml",
+                                "template": "%s"
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:%d": 1
+                        }
+                    }
+                }]], req_template:gsub('"', '\\"'), ngx.var.server_port)
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.sleep(0.5)
+
+            local core = require("apisix.core")
+            local http = require("resty.http")
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/foobar"
+            local body = [[{"name":"hello","age":20}]]
+            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 == 400)
+        }
+    }
+--- error_log
+Error Parsing XML
+
+
+
+=== TEST 4: invalid reference in template
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+            local core = require("apisix.core")
+            local req_template = [[{"foo":"{{name() .. " world"}}","bar":{{age+10}}}]]
+
+            local code, body = t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                string.format([[{
+                    "uri": "/foobar",
+                    "plugins": {
+                        "proxy-rewrite": {
+                            "uri": "/demo"
+                        },
+                        "body-transformer": {
+                            "request": {
+                                "template": "%s"
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:%d": 1
+                        }
+                    }
+                }]], req_template:gsub('"', '\\"'), ngx.var.server_port)
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.sleep(0.5)
+
+            local core = require("apisix.core")
+            local http = require("resty.http")
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/foobar"
+            local body = [[{"name":"hello","age":20}]]
+            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 == 503)
+        }
+    }
+--- grep_error_log eval
+qr/transform\(\): request template rendering:.*/
+--- grep_error_log_out eval
+qr/attempt to call global 'name' \(a string value\)/
+
+
+
+=== TEST 5: generate request body from scratch
+--- config
+    location /demo {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local body = core.request.get_body()
+            local data = core.json.decode(body)
+            assert(data.foo == "hello world")
+        }
+    }
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+            local core = require("apisix.core")
+            local req_template = [[{
+                "foo":"{{_ctx.var.arg_name .. " world"}}"
+            }]]
+
+            local code, body = t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                string.format([[{
+                    "uri": "/foobar",
+                    "plugins": {
+                        "proxy-rewrite": {
+                            "uri": "/demo",
+                            "method": "POST"
+                        },
+                        "body-transformer": {
+                            "request": {
+                                "template": "%s"
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "type": "roundrobin",
+                        "nodes": {
+                            "127.0.0.1:%d": 1
+                        }
+                    }
+                }]], req_template:gsub('"', '\\"'), ngx.var.server_port)
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.sleep(0.5)
+
+            local core = require("apisix.core")
+            local http = require("resty.http")
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/foobar?name=hello"
+            local opt = {method = "GET"}
+            local httpc = http.new()
+            local res = httpc:request_uri(uri, opt)
+            assert(res.status == 200)
+        }
+    }
+
+
+
+=== TEST 6: html escape in template
+--- config
+    location /demo {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local body = core.request.get_body()
+            local data = core.json.decode(body)
+            if (data == nil) or (data.agent:find("ngx_lua/", 0, true) == nil) then
+                return ngx.exit(400)
+            end
+        }
+    }
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+            local core = require("apisix.core")
+            -- html escape would escape '/' to '&#47' in string, which may be unexpected.
+            -- 'lua-resty-http/0.16.1 (Lua) ngx_lua/10021'
+            -- would be escaped into
+            -- 'lua-resty-http&#47;0.16.1 (Lua) ngx_lua&#47;10021'
+            local req_template = [[{
+                "agent":"{{_ctx.var.http_user_agent}}"
+            }]]
+            local admin_body = [[{
+                "uri": "/foobar",
+                "plugins": {
+                    "proxy-rewrite": {
+                        "uri": "/demo",
+                        "method": "POST"
+                    },
+                    "body-transformer": {
+                        "request": {
+                            "template": "%s"
+                        }
+                    }
+                },
+                "upstream": {
+                    "type": "roundrobin",
+                    "nodes": {
+                        "127.0.0.1:%d": 1
+                    }
+                }
+            }]]
+
+            local code, body = t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                string.format(admin_body, req_template:gsub('"', '\\"'), ngx.var.server_port)
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.sleep(0.5)
+
+            local core = require("apisix.core")
+            local http = require("resty.http")
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/foobar?name=hello"
+            local opt = {method = "GET"}
+            local httpc = http.new()
+            local res = httpc:request_uri(uri, opt)
+            assert(res.status == 400)
+
+            -- disable html escape, now it's ok
+            local req_template = [[{"agent":"{*_ctx.var.http_user_agent*}"}]]
+            local code, body = t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                string.format(admin_body, req_template:gsub('"', '\\"'), ngx.var.server_port)
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.sleep(0.5)
+
+            local res = httpc:request_uri(uri, opt)
+            assert(res.status == 200)
+        }
+    }
+
+
+
+=== TEST 7: parse body in yaml format
+--- config
+    location /demo {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local body = core.request.get_body()
+            local data = core.json.decode(body)
+            if data == nil or data.foobar ~= "hello world" then
+                return ngx.exit(400)
+            end
+        }
+    }
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+            local core = require("apisix.core")
+            local req_template = [[
+            {%
+                local yaml = require("tinyyaml")
+                local body = yaml.parse(_body)
+            %}
+            {"foobar":"{{body.foobar.foo .. " " .. body.foobar.bar}}"}
+            ]]
+            local admin_body = [[{
+                "uri": "/foobar",
+                "plugins": {
+                    "proxy-rewrite": {
+                        "uri": "/demo",
+                        "method": "POST"
+                    },
+                    "body-transformer": {
+                        "request": {
+                            "template": "%s"
+                        }
+                    }
+                },
+                "upstream": {
+                    "type": "roundrobin",
+                    "nodes": {
+                        "127.0.0.1:%d": 1
+                    }
+                }
+            }]]
+
+            local code, body = t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                string.format(admin_body, req_template:gsub('"', '\\"'), ngx.var.server_port)
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.sleep(0.5)
+
+            local core = require("apisix.core")
+            local http = require("resty.http")
+            local body = [[
+foobar:
+  foo: hello
+  bar: world
+            ]]
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/foobar"
+            local opt = {method = "POST", body = body}
+            local httpc = http.new()
+            local res = httpc:request_uri(uri, opt)
+            assert(res.status == 200)
+        }
+    }
+
+
+
+=== TEST 8: test _escape_json
+--- config
+    location /demo {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local body = core.request.get_body()
+            local data = core.json.decode(body)
+            if data == nil or data.foobar ~= [[hello "world"]] then
+                return ngx.exit(400)
+            end
+        }
+    }
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+            local core = require("apisix.core")
+            local req_template = [[{"foobar":{*_escape_json(name)*}}]]
+            local admin_body = [[{
+                "uri": "/foobar",
+                "plugins": {
+                    "proxy-rewrite": {
+                        "uri": "/demo",
+                        "method": "POST"
+                    },
+                    "body-transformer": {
+                        "request": {
+                            "input_format": "json",
+                            "template": "%s"
+                        }
+                    }
+                },
+                "upstream": {
+                    "type": "roundrobin",
+                    "nodes": {
+                        "127.0.0.1:%d": 1
+                    }
+                }
+            }]]
+
+            local code, body = t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                string.format(admin_body, req_template:gsub('"', '\\"'), ngx.var.server_port)
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.sleep(0.5)
+
+            local core = require("apisix.core")
+            local http = require("resty.http")
+            local body = [[{"name":"hello \"world\""}]]
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/foobar"
+            local opt = {method = "POST", body = body}
+            local httpc = http.new()
+            local res = httpc:request_uri(uri, opt)
+            assert(res.status == 200)
+        }
+    }
+
+
+
+=== TEST 9: test _escape_xml
+--- config
+    location /demo {
+        content_by_lua_block {
+            local core = require("apisix.core")
+            local body = core.request.get_body()
+            local xml2lua = require("xml2lua")
+            local xmlhandler = require("xmlhandler.tree")
+            local handler = xmlhandler:new()
+            local parser = xml2lua.parser(handler)
+            parser:parse(body)
+            assert(handler.root.foobar == "<nil>")
+        }
+    }
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin")
+            local core = require("apisix.core")
+            local req_template = [[<foobar>{*_escape_xml(name)*}</foobar>]]
+            local admin_body = [[{
+                "uri": "/foobar",
+                "plugins": {
+                    "proxy-rewrite": {
+                        "uri": "/demo",
+                        "method": "POST"
+                    },
+                    "body-transformer": {
+                        "request": {
+                            "input_format": "json",
+                            "template": "%s"
+                        }
+                    }
+                },
+                "upstream": {
+                    "type": "roundrobin",
+                    "nodes": {
+                        "127.0.0.1:%d": 1
+                    }
+                }
+            }]]
+
+            local code, body = t.test('/apisix/admin/routes/1',
+                ngx.HTTP_PUT,
+                string.format(admin_body, req_template:gsub('"', '\\"'), ngx.var.server_port)
+            )
+
+            if code >= 300 then
+                ngx.status = code
+                return
+            end
+            ngx.sleep(0.5)
+
+            local core = require("apisix.core")
+            local http = require("resty.http")
+            local body = [[{"name":"<nil>"}]]
+            local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/foobar"
+            local opt = {method = "POST", body = body}
+            local httpc = http.new()
+            local res = httpc:request_uri(uri, opt)
+            assert(res.status == 200)
+        }
+    }