Skip to content

Commit

Permalink
feat: delete useless data in the response (#2993)
Browse files Browse the repository at this point in the history
Fix #2759

Signed-off-by: spacewander <spacewanderlzx@gmail.com>
  • Loading branch information
spacewander authored Dec 9, 2020
1 parent 4719977 commit 1e882e3
Show file tree
Hide file tree
Showing 10 changed files with 1,469 additions and 141 deletions.
26 changes: 26 additions & 0 deletions apisix/admin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ local str_lower = string.lower
local reload_event = "/apisix/admin/plugins/reload"
local ipairs = ipairs
local error = error
local type = type


local events
local MAX_REQ_BODY = 1024 * 1024 * 1.5 -- 1.5 MiB

Expand Down Expand Up @@ -90,6 +93,27 @@ local function check_token(ctx)
end


local function strip_etcd_resp(data)
if type(data) == "table"
and data.header ~= nil
and data.header.revision ~= nil
and data.header.raft_term ~= nil
then
-- strip etcd data
data.header = nil
data.responses = nil
data.succeeded = nil

if data.node then
data.node.createdIndex = nil
data.node.modifiedIndex = nil
end
end

return data
end


local function run()
local api_ctx = {}
core.ctx.set_vars_meta(api_ctx)
Expand Down Expand Up @@ -150,6 +174,7 @@ local function run()
local code, data = resource[method](seg_id, req_body, seg_sub_path,
uri_args)
if code then
data = strip_etcd_resp(data)
core.response.exit(code, data)
end
end
Expand Down Expand Up @@ -219,6 +244,7 @@ local function run_stream()
local code, data = resource[method](seg_id, req_body, seg_sub_path,
uri_args)
if code then
data = strip_etcd_resp(data)
core.response.exit(code, data)
end
end
Expand Down
124 changes: 124 additions & 0 deletions t/admin/consumers2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#
# 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';

repeat_each(1);
no_long_string();
no_root_location();
no_shuffle();
log_level("info");

add_block_preprocessor(sub {
my ($block) = @_;

if (!$block->request) {
$block->set_value("request", "GET /t");
}

if (!$block->no_error_log) {
$block->set_value("no_error_log", "[error]\n[alert]");
}
});

run_tests;

__DATA__
=== TEST 1: not unwanted data, PUT
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin").test
local code, message, res = t('/apisix/admin/consumers',
ngx.HTTP_PUT,
[[{
"username":"jack"
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(message)
return
end
res = json.decode(res)
res.node.value.create_time = nil
res.node.value.update_time = nil
ngx.say(json.encode(res))
}
}
--- response_body
{"action":"set","node":{"key":"/apisix/consumers/jack","value":{"username":"jack"}}}
=== TEST 2: not unwanted data, GET
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin").test
local code, message, res = t('/apisix/admin/consumers/jack',
ngx.HTTP_GET
)
if code >= 300 then
ngx.status = code
ngx.say(message)
return
end
res = json.decode(res)
res.node.value.create_time = nil
res.node.value.update_time = nil
ngx.say(json.encode(res))
}
}
--- response_body
{"action":"get","count":"1","node":{"key":"/apisix/consumers/jack","value":{"username":"jack"}}}
=== TEST 3: not unwanted data, DELETE
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin").test
local code, message, res = t('/apisix/admin/consumers/jack',
ngx.HTTP_DELETE
)
if code >= 300 then
ngx.status = code
ngx.say(message)
return
end
res = json.decode(res)
ngx.say(json.encode(res))
}
}
--- response_body
{"action":"delete","deleted":"1","key":"/apisix/consumers/jack","node":{}}
136 changes: 136 additions & 0 deletions t/admin/global-rules.t
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,139 @@ GET /t
passed
--- no_error_log
[error]



=== TEST 12: not unwanted data, PUT
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin").test
local code, message, res = t('/apisix/admin/global_rules/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/"
}
}
}]]
)

if code >= 300 then
ngx.status = code
ngx.say(message)
return
end

res = json.decode(res)
res.node.value.create_time = nil
res.node.value.update_time = nil
ngx.say(json.encode(res))
}
}
--- response_body
{"action":"set","node":{"key":"/apisix/global_rules/1","value":{"id":"1","plugins":{"proxy-rewrite":{"uri":"/"}}}}}
--- request
GET /t
--- no_error_log
[error]



=== TEST 13: not unwanted data, PATCH
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin").test
local code, message, res = t('/apisix/admin/global_rules/1',
ngx.HTTP_PATCH,
[[{
"plugins": {
"proxy-rewrite": {
"uri": "/"
}
}
}]]
)

if code >= 300 then
ngx.status = code
ngx.say(message)
return
end

res = json.decode(res)
res.node.value.create_time = nil
res.node.value.update_time = nil
ngx.say(json.encode(res))
}
}
--- response_body
{"action":"compareAndSwap","node":{"key":"/apisix/global_rules/1","value":{"id":"1","plugins":{"proxy-rewrite":{"uri":"/"}}}}}
--- request
GET /t
--- no_error_log
[error]



=== TEST 14: not unwanted data, GET
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin").test
local code, message, res = t('/apisix/admin/global_rules/1',
ngx.HTTP_GET
)

if code >= 300 then
ngx.status = code
ngx.say(message)
return
end

res = json.decode(res)
res.node.value.create_time = nil
res.node.value.update_time = nil
ngx.say(json.encode(res))
}
}
--- response_body
{"action":"get","count":"1","node":{"key":"/apisix/global_rules/1","value":{"id":"1","plugins":{"proxy-rewrite":{"uri":"/"}}}}}
--- request
GET /t
--- no_error_log
[error]



=== TEST 15: not unwanted data, DELETE
--- config
location /t {
content_by_lua_block {
local json = require("toolkit.json")
local t = require("lib.test_admin").test
local code, message, res = t('/apisix/admin/global_rules/1',
ngx.HTTP_DELETE
)

if code >= 300 then
ngx.status = code
ngx.say(message)
return
end

res = json.decode(res)
ngx.say(json.encode(res))
}
}
--- response_body
{"action":"delete","deleted":"1","key":"/apisix/global_rules/1","node":{}}
--- request
GET /t
--- no_error_log
[error]
Loading

0 comments on commit 1e882e3

Please sign in to comment.