test: change the string to lua table (#6632)
diff --git a/t/plugin/traffic-split.t b/t/plugin/traffic-split.t
index 423816f..ea11f85 100644
--- a/t/plugin/traffic-split.t
+++ b/t/plugin/traffic-split.t
@@ -397,35 +397,64 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_name", "==", "jack" }, { "arg_age", "~~", "^[1-9]{1,2}"}
+ }
+ },
+ {
+ vars = {
+ {"arg_name2", "in", {"jack", "rose"} }, { "arg_age", "!", "<", 18}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1981"] = 20
+ }
+ },
+ weight = 2
+ },
+ {
+ upstream = {
+ name = "upstream_B",
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1982"] = 10
+ }
+ },
+ weight = 2
+ },
+ {
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
+
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {"vars": [["arg_name", "==", "jack"], ["arg_age", "~~", "^[1-9]{1,2}"]]},
- {"vars": [["arg_name2", "in", ["jack", "rose"]], ["arg_age2", "!", "<", 18]]}
- ],
- "weighted_upstreams": [
- {"upstream": {"name": "upstream_A", "type": "roundrobin", "nodes": {"127.0.0.1:1981":20}}, "weight": 2},
- {"upstream": {"name": "upstream_B", "type": "roundrobin", "nodes": {"127.0.0.1:1982":10}}, "weight": 2},
- {"weight": 1}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -479,34 +508,60 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_name", "==", "jack" }, { "arg_age", "~~", "^[1-9]{1,2}"}
+ }
+ },
+ {
+ vars = {
+ {"arg_name2", "in", {"jack", "rose"} }, { "arg_age", "!", "<", 18}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1981"] = 20
+ }
+ },
+ weight = 2
+ },
+ {
+ upstream = {
+ name = "upstream_B",
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1982"] = 10
+ }
+ },
+ weight = 2
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {"vars": [["arg_name", "==", "jack"], ["arg_age", "~~", "^[1-9]{1,2}"]]},
- {"vars": [["arg_name2", "in", ["jack", "rose"]], ["arg_age2", "!", "<", 18]]}
- ],
- "weighted_upstreams": [
- {"upstream": {"name": "upstream_A", "type": "roundrobin", "nodes": {"127.0.0.1:1981":20}}, "weight": 2},
- {"upstream": {"name": "upstream_B", "type": "roundrobin", "nodes": {"127.0.0.1:1982":10}}, "weight": 2}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -550,28 +605,52 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_name", "==", "jack" }, { "arg_age", "~~", "^[1-9]{1,2}"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1,
+ ["127.0.0.1:1981"] = 2,
+ ["127.0.0.1:1982"] = 2
+ },
+ timeout = {
+ connect = 15,
+ send = 15,
+ read = 15
+ }
+ },
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_name", "==", "jack"], ["arg_age", "~~", "^[1-9]{1,2}"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream": {"name": "upstream_A", "type": "roundrobin", "nodes": {"127.0.0.1:1980":1, "127.0.0.1:1981":2, "127.0.0.1:1982":2}, "timeout": {"connect": 15, "send": 15, "read": 15}}, "weight": 1}
- ]
- }
- ]
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -676,40 +755,38 @@
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
+ local json = require("toolkit.json")
+
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = {{
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {["127.0.0.1:1981"] = 1}
+ },
+ weight = 2
+ },
+ {
+ weight = 1
+ }
+ }
+ }}
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {["127.0.0.1:1980"] = 1}
+ }
+ }
+
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "weighted_upstreams": [
- {
- "upstream": {
- "name": "upstream_A",
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1981":1
- }
- },
- "weight": 2
- },
- {
- "weight": 1
- }
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
diff --git a/t/plugin/traffic-split2.t b/t/plugin/traffic-split2.t
index 853b98e..41bee39 100644
--- a/t/plugin/traffic-split2.t
+++ b/t/plugin/traffic-split2.t
@@ -111,42 +111,39 @@
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
+ local json = require("toolkit.json")
+
+ local data = {
+ uri = "/uri",
+ plugins = {
+ ["traffic-split"] = {
+ rules = {{
+ match = { {
+ vars = { { "arg_name", "==", "jack" } }
+ } },
+ weighted_upstreams = {
+ {
+ upstream = {
+ type = "roundrobin",
+ pass_host = "pass",
+ nodes = {["127.0.0.1:1981"] = 1}
+ }
+ }
+ }
+ }}
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {["127.0.0.1:1980"] = 1}
+ }
+ }
+
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/uri",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_name", "==", "jack"]]
- }
- ],
- "weighted_upstreams": [
- {
- "upstream": {
- "type": "roundrobin",
- "pass_host": "pass",
- "nodes": {
- "127.0.0.1:1981":1
- }
- }
- }
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
+
if code >= 300 then
ngx.status = code
end
@@ -234,42 +231,39 @@
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
- local code, body = t('/apisix/admin/routes/1',
- ngx.HTTP_PATCH,
- [=[{
- "uri": "/uri",
- "plugins": {
- "traffic-split": {
- "rules": [
+ local json = require("toolkit.json")
+
+ local data = {
+ uri = "/uri",
+ plugins = {
+ ["traffic-split"] = {
+ rules = {{
+ match = { {
+ vars = { { "arg_name", "==", "jack" } }
+ } },
+ weighted_upstreams = {
{
- "match": [
- {
- "vars": [["arg_name", "==", "jack"]]
- }
- ],
- "weighted_upstreams": [
- {
- "upstream": {
- "type": "roundrobin",
- "pass_host": "node",
- "nodes": {
- "localhost:1981":1
- }
- }
- }
- ]
+ upstream = {
+ type = "roundrobin",
+ pass_host = "node",
+ nodes = {["localhost:1981"] = 1}
+ }
}
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
}
+ }}
}
- }]=]
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {["127.0.0.1:1980"] = 1}
+ }
+ }
+
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ json.encode(data)
)
+
if code >= 300 then
ngx.status = code
end
@@ -297,41 +291,41 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "chash_test",
+ type = "chash",
+ hash_on = "header",
+ key = "custom_header",
+ nodes = {
+ ["127.0.0.1:1981"] = 1,
+ ["127.0.0.1:1982"] = 1
+ }
+ },
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PATCH,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "weighted_upstreams": [
- {
- "upstream": {
- "name": "chash_test",
- "type": "chash",
- "hash_on": "header",
- "key": "custom_header",
- "nodes": {
- "127.0.0.1:1981":1,
- "127.0.0.1:1982":1
- }
- },
- "weight": 1
- }
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -567,6 +561,7 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/upstreams/1',
ngx.HTTP_PUT,
@@ -600,49 +595,55 @@
ngx.say(body)
end
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = {
+ {
+ match = {
+ {
+ vars = {
+ {"arg_id", "==", "1" }
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream_id = 1,
+ weight = 1
+ }
+ }
+ },
+ {
+ match = {
+ {
+ vars = {
+ {"arg_id", "==", "2" }
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream_id = 2,
+ weight = 1
+ }
+ }
+ }
+ }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
+
code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PATCH,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_id","==","1"]]
- }
- ],
- "weighted_upstreams": [
- {
- "upstream_id": 1,
- "weight": 1
- }
- ]
- },
- {
- "match": [
- {
- "vars": [["arg_id","==","2"]]
- }
- ],
- "weighted_upstreams": [
- {
- "upstream_id": 2,
- "weight": 1
- }
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -682,6 +683,7 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/upstreams/1',
ngx.HTTP_PUT,
@@ -701,36 +703,40 @@
return
end
+ local data = {
+ uri = "/uri",
+ plugins = {
+ ["traffic-split"] = {
+ rules = {
+ {
+ match = {
+ {
+ vars = {
+ {"arg_id", "==", "1" }
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream_id = 1,
+ weight = 1
+ }
+ }
+ }
+ }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1978"] = 1
+ }
+ }
+ }
+
code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PATCH,
- [=[{
- "uri": "/uri",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_id","==","1"]]
- }
- ],
- "weighted_upstreams": [
- {
- "upstream_id": 1,
- "weight": 1
- }
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1978": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
diff --git a/t/plugin/traffic-split3.t b/t/plugin/traffic-split3.t
index 4851ceb..dc5f155 100644
--- a/t/plugin/traffic-split3.t
+++ b/t/plugin/traffic-split3.t
@@ -130,36 +130,60 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_name", "==", "jack"},
+ {"arg_age", ">", "23"},
+ {"http_appkey", "~~", "[a-z]{1,5}"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1981"] = 20
+ }
+ },
+ weight = 2
+ },
+ {
+ upstream = {
+ name = "upstream_B",
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1982"] = 10
+ }
+ },
+ weight = 2
+ },
+ {
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_name", "==", "jack"], ["arg_age", ">", "23"],["http_appkey", "~~", "[a-z]{1,5}"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream": {"name": "upstream_A", "type": "roundrobin", "nodes": {"127.0.0.1:1981":20}}, "weight": 2},
- {"upstream": {"name": "upstream_B", "type": "roundrobin", "nodes": {"127.0.0.1:1982":10}}, "weight": 2},
- {"weight": 1}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -218,29 +242,38 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {
+ {host = "foo.com", port = 80, weight = 0}
+ }
+ },
+ weight = 2
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "weighted_upstreams": [
- {"upstream": {"name": "upstream_A", "type": "roundrobin", "nodes": [{"host":"foo.com", "port": 80, "weight": 0}]}, "weight": 2}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -266,35 +299,51 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_name", "==", "jack"},
+ {"arg_age", ">", "23"},
+ {"http_appkey", "~~", "[a-z]{1,5}"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {
+ {host = "127.0.0.1", port = 1981, weight = 2},
+ {host = "127.0.0.1", port = 1982, weight = 2}
+ }
+ },
+ weight = 4
+ },
+ {
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_name", "==", "jack"], ["arg_age", ">", "23"],["http_appkey", "~~", "[a-z]{1,5}"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream": {"name": "upstream_A", "type": "roundrobin", "nodes": [{"host":"127.0.0.1", "port":1981, "weight": 2}, {"host":"127.0.0.1", "port":1982, "weight": 2}]}, "weight": 4},
- {"weight": 1}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -332,36 +381,60 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_name", "==", "jack"},
+ {"arg_age", ">", "23"},
+ {"http_appkey", "~~", "[a-z]{1,5}"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {
+ {host = "127.0.0.1", port = 1981, weight = 2}
+ }
+ },
+ weight = 2
+ },
+ {
+ upstream = {
+ name = "upstream_B",
+ type = "roundrobin",
+ nodes = {
+ {host = "127.0.0.1", port = 1982, weight = 2}
+ }
+ },
+ weight = 2
+ },
+ {
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_name", "==", "jack"], ["arg_age", ">", "23"],["http_appkey", "~~", "[a-z]{1,5}"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream": {"name": "upstream_A", "type": "roundrobin", "nodes": [{"host":"127.0.0.1", "port":1981, "weight": 2}]}, "weight": 2},
- {"upstream": {"name": "upstream_B", "type": "roundrobin", "nodes": [{"host":"127.0.0.1", "port":1982, "weight": 2}]}, "weight": 2},
- {"weight": 1}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -399,30 +472,48 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {
+ {host = "127.0.0.1", port = 1981, weight = 2}
+ }
+ },
+ weight = 2
+ },
+ {
+ upstream = {
+ name = "upstream_B",
+ type = "roundrobin",
+ nodes = {
+ {host = "127.0.0.1", port = 1982, weight = 2}
+ }
+ },
+ weight = 2
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "weighted_upstreams": [
- {"upstream": {"name": "upstream_A", "type": "roundrobin", "nodes": [{"host":"127.0.0.1", "port":1981, "weight": 2}]}, "weight": 2},
- {"upstream": {"name": "upstream_B", "type": "roundrobin", "nodes": [{"host":"127.0.0.1", "port":1982, "weight": 2}]}, "weight": 2}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -466,30 +557,41 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {
+ {host = "127.0.0.1", port = 1981, weight = 2}
+ }
+ },
+ weight = 1
+ },
+ {
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "weighted_upstreams": [
- {"upstream": {"name": "upstream_A", "type": "roundrobin", "nodes": [{"host":"127.0.0.1", "port":1981, "weight": 2}]}, "weight": 1},
- {"weight": 1}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -529,35 +631,49 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"http_x-api-appkey", "==", "api-key"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {
+ {host = "127.0.0.1", port = 1981, weight = 2},
+ {host = "127.0.0.1", port = 1982, weight = 2}
+ }
+ },
+ weight = 4
+ },
+ {
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["http_x-api-appkey", "==", "api-key"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream": {"name": "upstream_A", "type": "roundrobin", "nodes": [{"host":"127.0.0.1", "port":1981, "weight": 2}, {"host":"127.0.0.1", "port":1982, "weight": 2}]}, "weight": 4},
- {"weight": 1}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -595,35 +711,51 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_x-api-name", "==", "jack"},
+ {"arg_x-api-age", ">", "23"},
+ {"http_x-api-appkey", "~~", "[a-z]{1,5}"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream = {
+ name = "upstream_A",
+ type = "roundrobin",
+ nodes = {
+ {host = "127.0.0.1", port = 1981, weight = 2},
+ {host = "127.0.0.1", port = 1982, weight = 2}
+ }
+ },
+ weight = 4
+ },
+ {
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_x-api-name", "==", "jack"], ["arg_x-api-age", ">", "23"],["http_x-api-appkey", "~~", "[a-z]{1,5}"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream": {"name": "upstream_A", "type": "roundrobin", "nodes": [{"host":"127.0.0.1", "port":1981, "weight": 2}, {"host":"127.0.0.1", "port":1982, "weight": 2}]}, "weight": 4},
- {"weight": 1}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
diff --git a/t/plugin/traffic-split4.t b/t/plugin/traffic-split4.t
index dc819ac..9da4f0f 100644
--- a/t/plugin/traffic-split4.t
+++ b/t/plugin/traffic-split4.t
@@ -96,29 +96,33 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_name", "==", "James"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream_id = 2
+ }
+ }
+ } }
+ }
+ },
+ upstream_id = 1
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_name", "==", "James"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream_id": 2}
- ]
- }
- ]
- }
- },
- "upstream_id":"1"
- }]=]
+ json.encode(data)
)
if code >= 300 then
@@ -162,35 +166,39 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_name", "==", "James"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream_id = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_name", "==", "James"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream_id": 1}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
- )
+ json.encode(data)
+ )
if code >= 300 then
ngx.status = code
@@ -233,30 +241,37 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"uri", "==", "/server_port"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream_id = 2,
+ weight = 1
+ },
+ {
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream_id = 1
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["uri", "==", "/server_port"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream_id": 2, "weight": 1},
- {"weight": 1}
- ]
- }
- ]
- }
- },
- "upstream_id":"1"
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -292,36 +307,46 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_x-api-name", "==", "jack"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream_id = 1,
+ weight = 2
+ },
+ {
+ upstream_id = 2,
+ weight = 1
+ },
+ {
+ weight = 2
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_x-api-name", "==", "jack"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream_id": 1, "weight": 2},
- {"upstream_id": 2, "weight": 1},
- {"weight": 2}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -358,35 +383,43 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_x-api-name", "==", "jack"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream_id = 1,
+ weight = 1
+ },
+ {
+ upstream_id = 2,
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_x-api-name", "==", "jack"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream_id": 1, "weight": 1},
- {"upstream_id": 2, "weight": 1}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -422,36 +455,51 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ match = {
+ {
+ vars = {
+ {"arg_x-api-name", "==", "jack"}
+ }
+ }
+ },
+ weighted_upstreams = {
+ {
+ upstream_id = 1,
+ weight = 2
+ },
+ {
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ {host = "127.0.0.1", port = 1982, weight = 1}
+ }
+ },
+ weight = 1
+ },
+ {
+ weight = 2
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "match": [
- {
- "vars": [["arg_x-api-name", "==", "jack"]]
- }
- ],
- "weighted_upstreams": [
- {"upstream_id": 1, "weight": 2},
- {"upstream": {"type": "roundrobin", "nodes": [{"host":"127.0.0.1", "port":1982, "weight": 1}]}, "weight": 1},
- {"weight": 2}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
ngx.status = code
@@ -489,30 +537,32 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
- local code, body = t('/apisix/admin/upstreams/1',
- ngx.HTTP_PUT,
- [[{
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1981": 1,
- "127.0.0.1:1970": 1
- },
- "checks": {
- "active": {
- "http_path": "/status",
- "host": "foo.com",
- "healthy": {
- "interval": 1,
- "successes": 1
- },
- "unhealthy": {
- "interval": 1,
- "http_failures": 2
- }
+ local up_data = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1981"] = 1,
+ ["127.0.0.1:1970"] = 1
+ },
+ checks = {
+ active = {
+ http_path = "/status",
+ host = "foo.com",
+ healthy = {
+ interval = 1,
+ successes = 1
+ },
+ unhealthy = {
+ interval = 1,
+ http_failures = 2
}
}
- }]]
+ }
+ }
+ local code, body = t('/apisix/admin/upstreams/1',
+ ngx.HTTP_PUT,
+ json.encode(up_data)
)
if code >= 300 then
@@ -521,28 +571,31 @@
return
end
+ local data = {
+ uri = "/server_port",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ weighted_upstreams = {
+ {
+ upstream_id = 1,
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
+
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/server_port",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "weighted_upstreams": [
- {"upstream_id": 1, "weight": 1}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then
@@ -640,29 +693,32 @@
--- config
location /t {
content_by_lua_block {
+ local json = require("toolkit.json")
local t = require("lib.test_admin").test
+ local data = {
+ uri = "/hello",
+ plugins = {
+ ["traffic-split"] = {
+ rules = { {
+ weighted_upstreams = {
+ {
+ upstream_id = 1,
+ weight = 1
+ }
+ }
+ } }
+ }
+ },
+ upstream = {
+ type = "roundrobin",
+ nodes = {
+ ["127.0.0.1:1980"] = 1
+ }
+ }
+ }
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
- [=[{
- "uri": "/hello",
- "plugins": {
- "traffic-split": {
- "rules": [
- {
- "weighted_upstreams": [
- {"upstream_id": 1, "weight": 1}
- ]
- }
- ]
- }
- },
- "upstream": {
- "type": "roundrobin",
- "nodes": {
- "127.0.0.1:1980": 1
- }
- }
- }]=]
+ json.encode(data)
)
if code >= 300 then