Skip to content
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
12 changes: 7 additions & 5 deletions apisix/plugins/body-transformer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ local type = type
local pcall = pcall
local pairs = pairs
local next = next

local setmetatable = setmetatable

local transform_schema = {
type = "object",
Expand Down Expand Up @@ -155,10 +155,12 @@ local function transform(conf, body, typ, ctx, request_method)
return nil, 503, err
end

out._ctx = ctx
out._body = body
out._escape_xml = escape_xml
out._escape_json = escape_json
setmetatable(out, {__index = {
_ctx = ctx,
_body = body,
_escape_xml = escape_xml,
_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)
Expand Down
89 changes: 89 additions & 0 deletions t/plugin/body-transformer2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#
# 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: body transformer with decoded body (keyword: context)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin")
local core = require("apisix.core")

local req_template = ngx.encode_base64[[
{%
local core = require 'apisix.core'
local cjson = require 'cjson'
context.name = "bar"
context.address = nil
context.age = context.age + 1
local body = core.json.encode(context)
%}{* body *}
]]

local code, body = t.test('/apisix/admin/routes/1',
ngx.HTTP_PUT,
string.format([[{
"uri": "/echo",
"plugins": {
"body-transformer": {
"request": {
"template": "%s"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}]], req_template)
)

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



=== TEST 2: verify the transformed body
--- request
POST /echo
{"name": "foo", "address":"LA", "age": 18}
-- response_body
{"name": "bar", "age": 19}