@@ -985,3 +985,172 @@ qr/GET \/hello\?token=\*\*\*\*\* HTTP\/\d+\.\d+/
985985 }
986986--- response_body
987987success
988+
989+
990+
991+ === TEST 23: create route for removing all JSON array elements
992+ --- config
993+ location /t {
994+ content_by_lua_block {
995+ local t = require("lib.test_admin").test
996+ local code, body = t('/apisix/admin/routes/1',
997+ ngx.HTTP_PUT,
998+ [[{
999+ "plugins": {
1000+ "data-mask": {
1001+ "request": [
1002+ {
1003+ "action": "remove",
1004+ "body_format": "json",
1005+ "name": "$.items[*]",
1006+ "type": "body"
1007+ }
1008+ ]
1009+ },
1010+ "file-logger": {
1011+ "include_req_body": true,
1012+ "path": "mask-json-array-star.log"
1013+ }
1014+ },
1015+ "upstream": {
1016+ "nodes": {
1017+ "127.0.0.1:1982": 1
1018+ },
1019+ "type": "roundrobin"
1020+ },
1021+ "uri": "/hello"
1022+ }]]
1023+ )
1024+
1025+ if code >= 300 then
1026+ ngx.status = code
1027+ end
1028+ ngx.say(body)
1029+ }
1030+ }
1031+
1032+
1033+
1034+ === TEST 24: verify removing all array elements yields an empty array
1035+ --- config
1036+ location /t {
1037+ content_by_lua_block {
1038+ local core = require("apisix.core")
1039+ local t = require("lib.test_admin").test
1040+
1041+ os.remove("mask-json-array-star.log")
1042+ local code = t("/hello", ngx.HTTP_POST, [[{"items":["a","b","c"]}]])
1043+
1044+ local fd, err = io.open("mask-json-array-star.log", "r")
1045+ if not fd then
1046+ core.log.error("failed to open file: ", err)
1047+ return
1048+ end
1049+ local line = fd:read()
1050+ local log = core.json.decode(line)
1051+ os.remove("mask-json-array-star.log")
1052+
1053+ if not log or not log.request or not log.request.body then
1054+ ngx.say("missing logged request body")
1055+ return
1056+ end
1057+
1058+ local body = core.json.decode(log.request.body)
1059+ if not body or type(body.items) ~= "table" then
1060+ ngx.say("items missing: " .. tostring(log.request.body))
1061+ return
1062+ end
1063+ if #body.items ~= 0 then
1064+ ngx.say("expected empty array, got: " .. core.json.encode(body.items))
1065+ return
1066+ end
1067+ ngx.say("success")
1068+ }
1069+ }
1070+ --- response_body
1071+ success
1072+
1073+
1074+ === TEST 25: create route for removing non-contiguous JSON array elements
1075+ --- config
1076+ location /t {
1077+ content_by_lua_block {
1078+ local t = require("lib.test_admin").test
1079+ local code, body = t('/apisix/admin/routes/1',
1080+ ngx.HTTP_PUT,
1081+ [[{
1082+ "plugins": {
1083+ "data-mask": {
1084+ "request": [
1085+ {
1086+ "action": "remove",
1087+ "body_format": "json",
1088+ "name": "$.items[0,2]",
1089+ "type": "body"
1090+ }
1091+ ]
1092+ },
1093+ "file-logger": {
1094+ "include_req_body": true,
1095+ "path": "mask-json-array-multi.log"
1096+ }
1097+ },
1098+ "upstream": {
1099+ "nodes": {
1100+ "127.0.0.1:1982": 1
1101+ },
1102+ "type": "roundrobin"
1103+ },
1104+ "uri": "/hello"
1105+ }]]
1106+ )
1107+
1108+ if code >= 300 then
1109+ ngx.status = code
1110+ end
1111+ ngx.say(body)
1112+ }
1113+ }
1114+
1115+ === TEST 26: verify removing non-contiguous array elements keeps the unmatched item
1116+ --- config
1117+ location /t {
1118+ content_by_lua_block {
1119+ local core = require("apisix.core")
1120+ local t = require("lib.test_admin").test
1121+
1122+ os.remove("mask-json-array-multi.log")
1123+ local code = t("/hello", ngx.HTTP_POST, [[{"items":["a","b","c"]}]])
1124+
1125+ local fd, err = io.open("mask-json-array-multi.log", "r")
1126+ if not fd then
1127+ core.log.error("failed to open file: ", err)
1128+ return
1129+ end
1130+ local line = fd:read()
1131+ local log = core.json.decode(line)
1132+ os.remove("mask-json-array-multi.log")
1133+
1134+ if not log or not log.request or not log.request.body then
1135+ ngx.say("missing logged request body")
1136+ return
1137+ end
1138+
1139+ local body = core.json.decode(log.request.body)
1140+ if not body or type(body.items) ~= "table" then
1141+ ngx.say("items missing: " .. tostring(log.request.body))
1142+ return
1143+ end
1144+ if #body.items ~= 1 then
1145+ ngx.say("expected compacted array of 1, got: " .. core.json.encode(body.items))
1146+ return
1147+ end
1148+ if body.items[1] ~= "b" then
1149+ ngx.say("wrong remaining element: " .. core.json.encode(body.items))
1150+ return
1151+ end
1152+ ngx.say("success")
1153+ }
1154+ }
1155+ --- response_body
1156+ success
0 commit comments