|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +use t::APISIX 'no_plan'; |
| 18 | + |
| 19 | +no_long_string(); |
| 20 | +no_shuffle(); |
| 21 | +no_root_location(); |
| 22 | + |
| 23 | +add_block_preprocessor(sub { |
| 24 | + my ($block) = @_; |
| 25 | + |
| 26 | + if (!$block->request) { |
| 27 | + $block->set_value("request", "GET /t"); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +run_tests; |
| 32 | + |
| 33 | +__DATA__ |
| 34 | +
|
| 35 | +=== TEST 1: body transformer with decoded body (keyword: context) |
| 36 | +--- config |
| 37 | + location /t { |
| 38 | + content_by_lua_block { |
| 39 | + local t = require("lib.test_admin") |
| 40 | + local core = require("apisix.core") |
| 41 | +
|
| 42 | + local req_template = ngx.encode_base64[[ |
| 43 | + {% |
| 44 | + local core = require 'apisix.core' |
| 45 | + local cjson = require 'cjson' |
| 46 | + context.name = "bar" |
| 47 | + context.address = nil |
| 48 | + context.age = context.age + 1 |
| 49 | + local body = core.json.encode(context) |
| 50 | + %}{* body *} |
| 51 | + ]] |
| 52 | +
|
| 53 | + local code, body = t.test('/apisix/admin/routes/1', |
| 54 | + ngx.HTTP_PUT, |
| 55 | + string.format([[{ |
| 56 | + "uri": "/echo", |
| 57 | + "plugins": { |
| 58 | + "body-transformer": { |
| 59 | + "request": { |
| 60 | + "template": "%s" |
| 61 | + } |
| 62 | + } |
| 63 | + }, |
| 64 | + "upstream": { |
| 65 | + "type": "roundrobin", |
| 66 | + "nodes": { |
| 67 | + "127.0.0.1:1980": 1 |
| 68 | + } |
| 69 | + } |
| 70 | + }]], req_template) |
| 71 | + ) |
| 72 | +
|
| 73 | + if code >= 300 then |
| 74 | + ngx.status = code |
| 75 | + end |
| 76 | + ngx.say(body) |
| 77 | + } |
| 78 | + } |
| 79 | +--- response_body |
| 80 | +passed |
| 81 | +
|
| 82 | +
|
| 83 | +
|
| 84 | +=== TEST 2: verify the transformed body |
| 85 | +--- request |
| 86 | +POST /echo |
| 87 | +{"name": "foo", "address":"LA", "age": 18} |
| 88 | +-- response_body |
| 89 | +{"name": "bar", "age": 19} |
0 commit comments