From d2b577298d4b86ea120fda718e21478cd94e82eb Mon Sep 17 00:00:00 2001 From: nic-chen <33000667+nic-chen@users.noreply.github.com> Date: Thu, 24 Dec 2020 22:14:01 +0800 Subject: [PATCH] feat: add test cases for rejecting request data if it contains an empty array (#1112) * feat: add test cases for empty array --- api/test/e2e/route_test.go | 113 +++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/api/test/e2e/route_test.go b/api/test/e2e/route_test.go index 8e97157f96..15f680ec48 100644 --- a/api/test/e2e/route_test.go +++ b/api/test/e2e/route_test.go @@ -393,3 +393,116 @@ func TestRoute_Patch(t *testing.T) { testCaseCheck(tc, t) } } + +//uris methods remote_addrs +func TestRoute_With_Empty_Array(t *testing.T) { + tests := []HttpTestCase{ + { + Desc: "create route with empty hosts and host", + Object: ManagerApiExpect(t), + Path: "/apisix/admin/routes/r1", + Method: http.MethodPut, + Body: `{ + "uri": "/hello", + "hosts": [], + "host": "test.com", + "upstream": { + "nodes": { + "172.16.238.20:1980": 1 + }, + "type": "roundrobin" + } + }`, + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusBadRequest, + ExpectBody: `{"code":10000,"message":"schema validate failed: (root): Must validate one and only one schema (oneOf)\n(root): Must validate all the schemas (allOf)\nhosts: Array must have at least 1 items"}`, + }, + { + Desc: "make sure the route not created", + Object: APISIXExpect(t), + Method: http.MethodGet, + Path: "/hello", + Headers: map[string]string{"Host": "test.com"}, + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + }, + { + Desc: "create route with empty hosts", + Object: ManagerApiExpect(t), + Path: "/apisix/admin/routes/r1", + Method: http.MethodPut, + Body: `{ + "uri": "/hello", + "hosts": [], + "upstream": { + "nodes": { + "172.16.238.20:1980": 1 + }, + "type": "roundrobin" + } + }`, + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusBadRequest, + ExpectBody: `{"code":10000,"message":"schema validate failed: hosts: Array must have at least 1 items"}`, + }, + { + Desc: "make sure the route not created", + Object: APISIXExpect(t), + Method: http.MethodGet, + Path: "/hello", + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + }, + { + Desc: "create route with empty uris and uri", + Object: ManagerApiExpect(t), + Path: "/apisix/admin/routes/r1", + Method: http.MethodPut, + Body: `{ + "uri": "/hello", + "uris": [], + "upstream": { + "nodes": { + "172.16.238.20:1980": 1 + }, + "type": "roundrobin" + } + }`, + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusBadRequest, + ExpectBody: `{"code":10000,"message":"schema validate failed: (root): Must validate one and only one schema (oneOf)\n(root): Must validate all the schemas (allOf)\nuris: Array must have at least 1 items"}`, + }, + { + Desc: "create route with empty remote_addrs and remote_addr", + Object: ManagerApiExpect(t), + Path: "/apisix/admin/routes/r1", + Method: http.MethodPut, + Body: `{ + "uri": "/hello", + "remote_addrs": [], + "remote_addr": "0.0.0.0", + "upstream": { + "nodes": { + "172.16.238.20:1980": 1 + }, + "type": "roundrobin" + } + }`, + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusBadRequest, + ExpectBody: `{"code":10000,"message":"schema validate failed: (root): Must validate one and only one schema (oneOf)\n(root): Must validate all the schemas (allOf)\nremote_addrs: Array must have at least 1 items"}`, + }, + { + Desc: "make sure the route not created", + Object: APISIXExpect(t), + Method: http.MethodGet, + Path: "/hello", + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + }, + } + + for _, tc := range tests { + testCaseCheck(tc, t) + } +}