You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ch...@apache.org on 2020/10/15 04:34:55 UTC

[apisix-dashboard] 02/02: fix route search by uri

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

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

commit 061d9a2acc5b6a074f0dc2a5eca3c7f0c7b60caf
Author: nic-chen <jo...@163.com>
AuthorDate: Thu Oct 15 12:34:07 2020 +0800

    fix route search by uri
---
 api/internal/handler/route/route.go      | 22 +++++++++++--
 api/internal/handler/route/route_test.go | 53 +++++++++++++++++++++++++++++++-
 2 files changed, 71 insertions(+), 4 deletions(-)

diff --git a/api/internal/handler/route/route.go b/api/internal/handler/route/route.go
index 870eb06..0895a6a 100644
--- a/api/internal/handler/route/route.go
+++ b/api/internal/handler/route/route.go
@@ -97,26 +97,42 @@ type ListInput struct {
 	store.Pagination
 }
 
+func uriContains(obj *entity.Route, uri string) bool {
+	if strings.Contains(obj.URI, uri) {
+		return true
+	}
+	for _, str := range obj.Uris {
+		result := strings.Contains(str, uri)
+		if result {
+			return true
+		}
+	}
+	return false
+}
+
 func (h *Handler) List(c droplet.Context) (interface{}, error) {
 	input := c.Input().(*ListInput)
 
 	ret, err := h.routeStore.List(store.ListInput{
 		Predicate: func(obj interface{}) bool {
 			if input.Name != "" && input.URI != "" {
-				return strings.Contains(obj.(*entity.Route).Name, input.Name) &&
-					strings.Contains(obj.(*entity.Route).URI, input.URI)
+				if strings.Contains(obj.(*entity.Route).Name, input.Name) {
+					return uriContains(obj.(*entity.Route), input.URI)
+				}
+				return false
 			}
 			if input.Name != "" {
 				return strings.Contains(obj.(*entity.Route).Name, input.Name)
 			}
 			if input.URI != "" {
-				return strings.Contains(obj.(*entity.Route).URI, input.URI)
+				return uriContains(obj.(*entity.Route), input.URI)
 			}
 			return true
 		},
 		PageSize:   input.PageSize,
 		PageNumber: input.PageNumber,
 	})
+
 	if err != nil {
 		return nil, err
 	}
diff --git a/api/internal/handler/route/route_test.go b/api/internal/handler/route/route_test.go
index abed058..611dccc 100644
--- a/api/internal/handler/route/route_test.go
+++ b/api/internal/handler/route/route_test.go
@@ -745,6 +745,9 @@ func TestRoute(t *testing.T) {
 	_, err = handler.Update(ctx)
 	assert.Nil(t, err)
 
+	//sleep
+	time.Sleep(time.Duration(100) * time.Millisecond)
+
 	//list
 	listInput := &ListInput{}
 	reqBody = `{"page_size": 1, "page": 1}`
@@ -785,12 +788,60 @@ func TestRoute(t *testing.T) {
 	dataPage = retPage.(*store.ListOutput)
 	assert.Equal(t, len(dataPage.Rows), 0)
 
+	//create route using uris
+	route3 := &entity.Route{}
+	reqBody = `{
+      "id": "2",
+      "name": "bbbbb",
+      "uris": ["/aa", "/bb"],
+      "hosts": ["foo.com", "*.bar.com"],
+      "remote_addrs": ["127.0.0.0/8"],
+      "methods": ["PUT", "GET"],
+      "upstream": {
+          "type": "roundrobin",
+          "nodes": [{
+              "host": "www.a.com",
+              "port": 80,
+              "weight": 1
+          }]
+      }
+  }`
+	json.Unmarshal([]byte(reqBody), route3)
+	ctx.SetInput(route3)
+	_, err = handler.Create(ctx)
+	assert.Nil(t, err)
+
+	//sleep
+	time.Sleep(time.Duration(100) * time.Millisecond)
+
+	//list search match uris
+	listInput5 := &ListInput{}
+	reqBody = `{"page_size": 1, "page": 1, "name": "bbb", "uri": "bb"}`
+	json.Unmarshal([]byte(reqBody), listInput5)
+	ctx.SetInput(listInput5)
+	retPage, err = handler.List(ctx)
+	assert.Nil(t, err)
+	dataPage = retPage.(*store.ListOutput)
+	assert.Equal(t, len(dataPage.Rows), 1)
+
 	//delete test data
 	inputDel := &BatchDelete{}
-	reqBody = `{"ids": "1"}`
+	reqBody = `{"ids": "1,2"}`
 	json.Unmarshal([]byte(reqBody), inputDel)
 	ctx.SetInput(inputDel)
 	_, err = handler.BatchDelete(ctx)
 	assert.Nil(t, err)
 
+	//sleep
+	time.Sleep(time.Duration(100) * time.Millisecond)
+
+	//get route -- deleted, not found
+	getInput := &GetInput{}
+	reqBody = `{"id": "1"}`
+	json.Unmarshal([]byte(reqBody), getInput)
+	ctx.SetInput(getInput)
+	ret, err = handler.Get(ctx)
+	assert.Nil(t, ret)
+	assert.EqualError(t, err, "data not found")
+
 }