You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by me...@apache.org on 2020/11/26 14:16:06 UTC

[apisix-dashboard] branch master updated: test: add e2e test cases for route with limit plugin (#882)

This is an automated email from the ASF dual-hosted git repository.

membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 0a826e6  test: add e2e test cases for route with limit plugin (#882)
0a826e6 is described below

commit 0a826e6ce9cbd253da1e39a5e3e960c7746d62ce
Author: nic-chen <33...@users.noreply.github.com>
AuthorDate: Thu Nov 26 22:14:42 2020 +0800

    test: add e2e test cases for route with limit plugin (#882)
    
    Related issues #636
---
 api/test/e2e/route_with_limit_plugin_test.go | 304 +++++++++++++++++++++++++++
 1 file changed, 304 insertions(+)

diff --git a/api/test/e2e/route_with_limit_plugin_test.go b/api/test/e2e/route_with_limit_plugin_test.go
new file mode 100644
index 0000000..ea2a5ed
--- /dev/null
+++ b/api/test/e2e/route_with_limit_plugin_test.go
@@ -0,0 +1,304 @@
+/*
+ * 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"
+	"time"
+)
+
+func TestRoute_With_Limit_Plugin(t *testing.T) {
+	tests := []HttpTestCase{
+		{
+			caseDesc:     "make sure the route is not created ",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusNotFound,
+			ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
+		},
+		{
+			caseDesc: "create route",
+			Object:   MangerApiExpect(t),
+			Method:   http.MethodPut,
+			Path:     "/apisix/admin/routes/r1",
+			Body: `{
+				"uri": "/hello",
+				"plugins": {
+					"limit-count": {
+						"count": 2,
+						"time_window": 2,
+						"rejected_code": 503,
+						"key": "remote_addr"
+					}
+				},
+				"upstream": {
+					"type": "roundrobin",
+					"nodes": [{
+						"host": "172.16.238.20",
+						"port": 1981,
+						"weight": 1
+					}]
+				}
+			}`,
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   `"code":0`,
+		},
+		{
+			caseDesc:     "verify route that should not be limited",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   "hello world",
+			Sleep:        sleepTime,
+		},
+		{
+			caseDesc:     "verify route that should not be limited 2",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   "hello world",
+		},
+		{
+			caseDesc:     "verify route that should be limited",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusServiceUnavailable,
+			ExpectBody:   "503 Service Temporarily Unavailable",
+		},
+		{
+			caseDesc:     "verify route that should not be limited since time window pass",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   "hello world",
+			Sleep:        2 * time.Second,
+		},
+		{
+			caseDesc:     "delete route",
+			Object:       MangerApiExpect(t),
+			Method:       http.MethodDelete,
+			Path:         "/apisix/admin/routes/r1",
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+		},
+		{
+			caseDesc:     "make sure the route has been deleted",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusNotFound,
+			ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
+			Sleep:        sleepTime,
+		},
+	}
+
+	for _, tc := range tests {
+		testCaseCheck(tc)
+	}
+}
+
+func TestRoute_With_Limit_Plugin_By_Consumer(t *testing.T) {
+	tests := []HttpTestCase{
+		{
+			caseDesc:     "make sure the route is not created ",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusNotFound,
+			ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
+		},
+		{
+			caseDesc: "create route",
+			Object:   MangerApiExpect(t),
+			Method:   http.MethodPut,
+			Path:     "/apisix/admin/routes/r1",
+			Body: `{
+				"uri": "/hello",
+				"plugins": {
+					"key-auth": {},
+					"limit-count": {
+						"count": 2,
+						"time_window": 2,
+						"rejected_code": 503,
+						"key": "consumer_name"
+					}
+				},
+				"upstream": {
+					"type": "roundrobin",
+					"nodes": [{
+						"host": "172.16.238.20",
+						"port": 1981,
+						"weight": 1
+					}]
+				}
+			}`,
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   `"code":0`,
+		},
+		{
+			caseDesc:     "make sure the consumer is not created",
+			Object:       MangerApiExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/apisix/admin/consumers/jack",
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusNotFound,
+		},
+		{
+			caseDesc: "create consumer",
+			Object:   MangerApiExpect(t),
+			Path:     "/apisix/admin/consumers",
+			Method:   http.MethodPut,
+			Body: `{
+				"username": "jack",
+				"plugins": {
+					"key-auth": {
+						"key": "auth-jack"
+					}
+				}
+			}`,
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+		},
+		{
+			caseDesc: "create consumer 2",
+			Object:   MangerApiExpect(t),
+			Path:     "/apisix/admin/consumers",
+			Method:   http.MethodPut,
+			Body: `{
+				"username": "pony",
+				"plugins": {
+					"key-auth": {
+						"key": "auth-pony"
+					}
+				}
+			}`,
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+		},
+		{
+			caseDesc:     "verify route that should not be limited",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			Headers:      map[string]string{"apikey": "auth-jack"},
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   "hello world",
+			Sleep:        sleepTime,
+		},
+		{
+			caseDesc:     "verify route that should not be limited 2",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			Headers:      map[string]string{"apikey": "auth-jack"},
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   "hello world",
+		},
+		{
+			caseDesc:     "verify route that should be limited",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			Headers:      map[string]string{"apikey": "auth-jack"},
+			ExpectStatus: http.StatusServiceUnavailable,
+			ExpectBody:   "503 Service Temporarily Unavailable",
+		},
+		{
+			caseDesc:     "verify route that should not be limited (other consumer)",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			Headers:      map[string]string{"apikey": "auth-pony"},
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   "hello world",
+		},
+		{
+			caseDesc:     "verify route that should not be limited since time window pass",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			Headers:      map[string]string{"apikey": "auth-jack"},
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   "hello world",
+			Sleep:        2 * time.Second,
+		},
+		{
+			caseDesc:     "delete consumer pony",
+			Object:       MangerApiExpect(t),
+			Method:       http.MethodDelete,
+			Path:         "/apisix/admin/consumers/pony",
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+		},
+		{
+			caseDesc:     "delete consumer jack",
+			Object:       MangerApiExpect(t),
+			Method:       http.MethodDelete,
+			Path:         "/apisix/admin/consumers/jack",
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+		},
+		{
+			caseDesc:     "make sure pony has been deleted",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			Headers:      map[string]string{"apikey": "auth-pony"},
+			ExpectStatus: http.StatusUnauthorized,
+			ExpectBody:   `{"message":"Missing related consumer"}`,
+			Sleep:        sleepTime,
+		},
+		{
+			caseDesc:     "make sure jack has been deleted",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			Headers:      map[string]string{"apikey": "auth-pony"},
+			ExpectStatus: http.StatusUnauthorized,
+			ExpectBody:   `{"message":"Missing related consumer"}`,
+		},
+		{
+			caseDesc:     "delete route",
+			Object:       MangerApiExpect(t),
+			Method:       http.MethodDelete,
+			Path:         "/apisix/admin/routes/r1",
+			Headers:      map[string]string{"Authorization": token},
+			ExpectStatus: http.StatusOK,
+		},
+		{
+			caseDesc:     "make sure the route has been deleted",
+			Object:       APISIXExpect(t),
+			Method:       http.MethodGet,
+			Path:         "/hello",
+			ExpectStatus: http.StatusNotFound,
+			ExpectBody:   `{"error_msg":"404 Route Not Found"}`,
+			Sleep:        sleepTime,
+		},
+	}
+
+	for _, tc := range tests {
+		testCaseCheck(tc)
+	}
+}