diff --git a/api/test/e2e/route_with_vars_test.go b/api/test/e2e/route_with_vars_test.go deleted file mode 100644 index faad3f2784..0000000000 --- a/api/test/e2e/route_with_vars_test.go +++ /dev/null @@ -1,317 +0,0 @@ -/* - * 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. - */ -package e2e - -import ( - "net/http" - "testing" -) - -func TestRoute_with_vars(t *testing.T) { - tests := []HttpTestCase{ - { - Desc: "add route with vars (args)", - Object: ManagerApiExpect(t), - Method: http.MethodPut, - Path: "/apisix/admin/routes/r1", - Body: `{ - "uri": "/hello", - "vars": [ - ["arg_name","==","aaa"] - ], - "upstream": { - "type": "roundrobin", - "nodes": [{ - "host": "172.16.238.20", - "port": 1980, - "weight": 1 - }] - } - }`, - Headers: map[string]string{"Authorization": token}, - ExpectStatus: http.StatusOK, - }, - - { - Desc: "hit the route with right args", - Object: APISIXExpect(t), - Method: http.MethodGet, - Path: `/hello`, - Query: "name=aaa", - ExpectStatus: http.StatusOK, - Sleep: sleepTime, - }, - - { - Desc: "hit the route with wrong args", - Object: APISIXExpect(t), - Method: http.MethodGet, - Path: `/hello`, - Query: "name=bbb", - ExpectStatus: http.StatusNotFound, - Sleep: sleepTime, - }, - - { - Desc: "hit the route with no args", - Object: APISIXExpect(t), - Method: http.MethodGet, - Path: `/hello`, - ExpectStatus: http.StatusNotFound, - Sleep: sleepTime, - }, - - { - Desc: "update route with vars (header)", - Object: ManagerApiExpect(t), - Method: http.MethodPut, - Path: "/apisix/admin/routes/r1", - Body: `{ - "uri": "/hello", - "vars": [ - ["http_k","==","header"] - ], - "upstream": { - "type": "roundrobin", - "nodes": [{ - "host": "172.16.238.20", - "port": 1980, - "weight": 1 - }] - } - }`, - Headers: map[string]string{"Authorization": token}, - ExpectStatus: http.StatusOK, - }, - - { - Desc: "hit the route with right header", - Object: APISIXExpect(t), - Method: http.MethodGet, - Headers: map[string]string{"k": "header"}, - Path: `/hello`, - ExpectStatus: http.StatusOK, - Sleep: sleepTime, - }, - - { - Desc: "hit the route with wrong header", - Object: APISIXExpect(t), - Method: http.MethodGet, - Headers: map[string]string{"k": "jack"}, - Path: `/hello`, - ExpectStatus: http.StatusNotFound, - Sleep: sleepTime, - }, - - { - Desc: "hit the route with no header", - Object: APISIXExpect(t), - Method: http.MethodGet, - Path: `/hello`, - ExpectStatus: http.StatusNotFound, - Sleep: sleepTime, - }, - - { - Desc: "update route with vars (cookie)", - Object: ManagerApiExpect(t), - Method: http.MethodPut, - Path: "/apisix/admin/routes/r1", - Body: `{ - "uri": "/hello", - "vars": [ - ["http_cookie","==","_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"] - ], - "upstream": { - "type": "roundrobin", - "nodes": [{ - "host": "172.16.238.20", - "port": 1980, - "weight": 1 - }] - } - }`, - Headers: map[string]string{"Authorization": token}, - ExpectStatus: http.StatusOK, - }, - - { - Desc: "hit the route with right Cookie", - Object: APISIXExpect(t), - Method: http.MethodGet, - Headers: map[string]string{"Cookie": "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, - Path: `/hello`, - ExpectStatus: http.StatusOK, - Sleep: sleepTime, - }, - - { - Desc: "hit the route with wrong Cookie", - Object: APISIXExpect(t), - Method: http.MethodGet, - Headers: map[string]string{"Cookie": "jack"}, - Path: `/hello`, - ExpectStatus: http.StatusNotFound, - Sleep: sleepTime, - }, - - { - Desc: "hit the route with no Cookie", - Object: APISIXExpect(t), - Method: http.MethodGet, - Path: `/hello`, - ExpectStatus: http.StatusNotFound, - Sleep: sleepTime, - }, - - { - Desc: "delete route", - Object: ManagerApiExpect(t), - Method: http.MethodDelete, - Path: "/apisix/admin/routes/r1", - Headers: map[string]string{"Authorization": token}, - ExpectStatus: http.StatusOK, - Sleep: sleepTime, - }, - - { - Desc: "add route with multiple vars (args, cookie and header)", - Object: ManagerApiExpect(t), - Method: http.MethodPut, - Path: "/apisix/admin/routes/r1", - Body: `{ - "uri": "/hello", - "vars": [ - ["http_cookie","==","_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"], - ["http_k","==","header"], - ["arg_name","==","aaa"] - ], - "upstream": { - "type": "roundrobin", - "nodes": [{ - "host": "172.16.238.20", - "port": 1980, - "weight": 1 - }] - } - }`, - Headers: map[string]string{"Authorization": token}, - ExpectStatus: http.StatusOK, - }, - - { - Desc: "hit the route with right parameters", - Object: APISIXExpect(t), - Method: http.MethodGet, - Headers: map[string]string{"k": "header", "Cookie": "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, - Path: `/hello`, - Query: "name=aaa", - ExpectStatus: http.StatusOK, - Sleep: sleepTime, - }, - - { - Desc: "hit the route with wrong arg", - Object: APISIXExpect(t), - Method: http.MethodGet, - Headers: map[string]string{"k": "header", "Cookie": "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, - Path: `/hello`, - ExpectStatus: http.StatusNotFound, - Sleep: sleepTime, - }, - - { - Desc: "hit the route with wrong header", - Object: APISIXExpect(t), - Method: http.MethodGet, - Headers: map[string]string{"k": "test", "Cookie": "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, - Path: `/hello`, - Query: "name=aaa", - ExpectStatus: http.StatusNotFound, - Sleep: sleepTime, - }, - - { - Desc: "hit the route with wrong cookie", - Object: APISIXExpect(t), - Method: http.MethodGet, - Headers: map[string]string{"k": "header", "Cookie": "_octo=GH1.1.572248189.1598928545; logged_in=yes;"}, - Path: `/hello`, - Query: "name=aaa", - ExpectStatus: http.StatusNotFound, - Sleep: sleepTime, - }, - - { - Desc: "delete route", - Object: ManagerApiExpect(t), - Method: http.MethodDelete, - Path: "/apisix/admin/routes/r1", - Headers: map[string]string{"Authorization": token}, - ExpectStatus: http.StatusOK, - Sleep: sleepTime, - }, - - { - Desc: "add route with vars (args is digital)", - Object: ManagerApiExpect(t), - Method: http.MethodPut, - Path: "/apisix/admin/routes/r1", - Body: `{ - "uri": "/hello", - "vars": [ - ["arg_name","==",111] - ], - "upstream": { - "type": "roundrobin", - "nodes": [{ - "host": "172.16.238.20", - "port": 1980, - "weight": 1 - }] - } - }`, - Headers: map[string]string{"Authorization": token}, - ExpectStatus: http.StatusOK, - }, - - { - Desc: "verify route", - Object: APISIXExpect(t), - Method: http.MethodGet, - Path: `/hello`, - Query: "name=111", - ExpectStatus: http.StatusOK, - Sleep: sleepTime, - }, - - { - Desc: "delete the route with vars (args is digital)", - Object: ManagerApiExpect(t), - Method: http.MethodDelete, - Path: "/apisix/admin/routes/r1", - Headers: map[string]string{"Authorization": token}, - ExpectStatus: http.StatusOK, - Sleep: sleepTime, - }, - } - - for _, tc := range tests { - testCaseCheck(tc, t) - } -} diff --git a/api/test/e2enew/route/route_with_vars_test.go b/api/test/e2enew/route/route_with_vars_test.go new file mode 100644 index 0000000000..9b6cc5d57e --- /dev/null +++ b/api/test/e2enew/route/route_with_vars_test.go @@ -0,0 +1,368 @@ +/* + * 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. + */ +package route + +import ( + "encoding/json" + "net/http" + + "github.com/onsi/ginkgo" + "github.com/stretchr/testify/assert" + + "e2enew/base" +) + +var upstream map[string]interface{} = map[string]interface{}{ + "type": "roundrobin", + "nodes": []map[string]interface{}{ + { + "host": base.UpstreamIp, + "port": 1980, + "weight": 1, + }, + }, +} + +var _ = ginkgo.Describe("test route with vars (args)", func() { + ginkgo.It("add route with vars (args)", func() { + t := ginkgo.GinkgoT() + var createRouteBody map[string]interface{} = map[string]interface{}{ + "uri": "/hello", + "vars": [][]string{ + {"arg_name", "==", "aaa"}, + }, + "upstream": upstream, + } + _createRouteBody, err := json.Marshal(createRouteBody) + assert.Nil(t, err) + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodPut, + Path: "/apisix/admin/routes/r1", + Body: string(_createRouteBody), + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + }) + }) + ginkgo.It("hit the route with right args", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: `/hello`, + Query: "name=aaa", + ExpectStatus: http.StatusOK, + ExpectBody: "hello world", + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit the route with wrong args", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: `/hello`, + Query: "name=bbb", + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit the route with no args", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: `/hello`, + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("update route with vars (header)", func() { + t := ginkgo.GinkgoT() + var createRouteBody map[string]interface{} = map[string]interface{}{ + "uri": "/hello", + "vars": [][]string{ + {"http_k", "==", "header"}, + }, + "upstream": upstream, + } + _createRouteBody, err := json.Marshal(createRouteBody) + assert.Nil(t, err) + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodPut, + Path: "/apisix/admin/routes/r1", + Body: string(_createRouteBody), + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + }) + }) + ginkgo.It("hit the route with right header", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Headers: map[string]string{"k": "header"}, + Path: `/hello`, + ExpectStatus: http.StatusOK, + ExpectBody: "hello world", + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit the route with wrong header", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Headers: map[string]string{"k": "jack"}, + Path: `/hello`, + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit the route with no header", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: `/hello`, + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("update route with vars (cookie)", func() { + t := ginkgo.GinkgoT() + var createRouteBody map[string]interface{} = map[string]interface{}{ + "uri": "/hello", + "vars": [][]string{ + {"http_cookie", "==", "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, + }, + "upstream": upstream, + } + _createRouteBody, err := json.Marshal(createRouteBody) + assert.Nil(t, err) + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodPut, + Path: "/apisix/admin/routes/r1", + Body: string(_createRouteBody), + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + }) + }) + ginkgo.It("hit the route with right Cookie", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Headers: map[string]string{"Cookie": "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, + Path: `/hello`, + ExpectStatus: http.StatusOK, + ExpectBody: "hello world", + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit the route with wrong Cookie", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Headers: map[string]string{"Cookie": "jack"}, + Path: `/hello`, + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit the route with no Cookie", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: `/hello`, + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("delete route", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodDelete, + Path: "/apisix/admin/routes/r1", + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit the route just delete", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Headers: map[string]string{"Cookie": "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, + Path: `/hello`, + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) +}) + +var _ = ginkgo.Describe("test route with multiple vars (args, cookie and header)", func() { + ginkgo.It("add route with multiple vars (args, cookie and header)", func() { + t := ginkgo.GinkgoT() + var createRouteBody map[string]interface{} = map[string]interface{}{ + "uri": "/hello", + "vars": [][]string{ + {"http_cookie", "==", "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, + {"http_k", "==", "header"}, + {"arg_name", "==", "aaa"}, + }, + "upstream": upstream, + } + _createRouteBody, err := json.Marshal(createRouteBody) + assert.Nil(t, err) + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodPut, + Path: "/apisix/admin/routes/r1", + Body: string(_createRouteBody), + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + }) + }) + ginkgo.It("hit the route with right parameters", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Headers: map[string]string{"k": "header", "Cookie": "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, + Path: `/hello`, + Query: "name=aaa", + ExpectStatus: http.StatusOK, + ExpectBody: "hello world", + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit the route with wrong arg", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Headers: map[string]string{"k": "header", "Cookie": "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, + Path: `/hello`, + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit the route with wrong arg", func() { + base.RunTestCase(base.HttpTestCase{ + Desc: "hit the route with wrong header", + Object: base.APISIXExpect(), + Method: http.MethodGet, + Headers: map[string]string{"k": "test", "Cookie": "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, + Path: `/hello`, + Query: "name=aaa", + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit the route with wrong cookie", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Headers: map[string]string{"k": "header", "Cookie": "_octo=GH1.1.572248189.1598928545; logged_in=yes;"}, + Path: `/hello`, + Query: "name=aaa", + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("delete route", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodDelete, + Path: "/apisix/admin/routes/r1", + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit the route just delete", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Headers: map[string]string{"k": "header", "Cookie": "_octo=GH1.1.572248189.1598928545; _device_id=2c1a1a52074e66a3a008e4b73c690500; logged_in=yes;"}, + Path: `/hello`, + Query: "name=aaa", + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) +}) + +var _ = ginkgo.Describe("test route with vars (args is digital)", func() { + ginkgo.It("add route with vars (args is digital)", func() { + t := ginkgo.GinkgoT() + var createRouteBody map[string]interface{} = map[string]interface{}{ + "uri": "/hello", + "vars": [][]string{ + {"arg_name", "==", "111"}, + }, + "upstream": upstream, + } + _createRouteBody, err := json.Marshal(createRouteBody) + assert.Nil(t, err) + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodPut, + Path: "/apisix/admin/routes/r1", + Body: string(_createRouteBody), + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + }) + }) + ginkgo.It("verify route", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: `/hello`, + Query: "name=111", + ExpectStatus: http.StatusOK, + ExpectBody: "hello world", + Sleep: base.SleepTime, + }) + }) + ginkgo.It("delete the route with vars (args is digital)", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.ManagerApiExpect(), + Method: http.MethodDelete, + Path: "/apisix/admin/routes/r1", + Headers: map[string]string{"Authorization": base.GetToken()}, + ExpectStatus: http.StatusOK, + Sleep: base.SleepTime, + }) + }) + ginkgo.It("hit route just delete", func() { + base.RunTestCase(base.HttpTestCase{ + Object: base.APISIXExpect(), + Method: http.MethodGet, + Path: `/hello`, + Query: "name=111", + ExpectStatus: http.StatusNotFound, + ExpectBody: `{"error_msg":"404 Route Not Found"}`, + Sleep: base.SleepTime, + }) + }) +})