Skip to content

Commit

Permalink
fix search by status on the route list page is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaycean committed Jan 5, 2021
1 parent 45e90ec commit 6c49aa3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
23 changes: 14 additions & 9 deletions api/internal/handler/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ import (
"reflect"
"strings"

"github.com/gin-gonic/gin"
"github.com/shiningrush/droplet"
"github.com/shiningrush/droplet/data"
"github.com/shiningrush/droplet/wrapper"
"github.com/yuin/gopher-lua"
wgin "github.com/shiningrush/droplet/wrapper/gin"
"github.com/apisix/manager-api/internal/conf"
"github.com/apisix/manager-api/internal/core/entity"
"github.com/apisix/manager-api/internal/core/store"
"github.com/apisix/manager-api/internal/handler"
"github.com/apisix/manager-api/internal/log"
"github.com/apisix/manager-api/internal/utils"
"github.com/apisix/manager-api/internal/utils/consts"
"github.com/gin-gonic/gin"
"github.com/shiningrush/droplet"
"github.com/shiningrush/droplet/data"
"github.com/shiningrush/droplet/wrapper"
wgin "github.com/shiningrush/droplet/wrapper/gin"
lua "github.com/yuin/gopher-lua"
)

type Handler struct {
Expand Down Expand Up @@ -178,9 +178,10 @@ func (h *Handler) Get(c droplet.Context) (interface{}, error) {
}

type ListInput struct {
Name string `auto_read:"name,query"`
URI string `auto_read:"uri,query"`
Label string `auto_read:"label,query"`
Name string `auto_read:"name,query"`
URI string `auto_read:"uri,query"`
Label string `auto_read:"label,query"`
Status string `auto_read:"status,query"`
store.Pagination
}

Expand Down Expand Up @@ -219,6 +220,10 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
return false
}

if input.Status != "" && fmt.Sprintf("%d", obj.(*entity.Route).Status) != input.Status {
return false
}

return true
},
Format: func(obj interface{}) interface{} {
Expand Down
46 changes: 46 additions & 0 deletions api/internal/handler/route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestRoute(t *testing.T) {
"vars": [],
"remote_addrs": ["127.0.0.0/8"],
"methods": ["PUT", "GET"],
"status": 1,
"upstream": {
"type": "roundrobin",
"nodes": [{
Expand Down Expand Up @@ -415,6 +416,7 @@ func TestRoute(t *testing.T) {
"hosts": ["foo.com", "*.bar.com"],
"remote_addrs": ["127.0.0.0/8"],
"methods": ["PUT", "GET"],
"status": 1,
"labels": {
"l1": "v1",
"l2": "v2"
Expand Down Expand Up @@ -925,6 +927,39 @@ func TestRoute(t *testing.T) {
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 0)

// list search and status match
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "status": "1"}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)

// list search and status not match
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "status": "0"}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 0)

//list search with name and status
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "name": "a", "status": "1"}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)

//list search with name and label
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "name": "a", "label":"l1:v1"}`
Expand Down Expand Up @@ -958,6 +993,17 @@ func TestRoute(t *testing.T) {
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)

//list search with uri,name, status and label
listInput = &ListInput{}
reqBody = `{"page_size": 1, "page": 1, "name": "a", "status": "1", "uri": "index", "label":"l1:v1"}`
err = json.Unmarshal([]byte(reqBody), listInput)
assert.Nil(t, err)
ctx.SetInput(listInput)
retPage, err = handler.List(ctx)
assert.Nil(t, err)
dataPage = retPage.(*store.ListOutput)
assert.Equal(t, len(dataPage.Rows), 1)

//create route using uris
route3 := &entity.Route{}
reqBody = `{
Expand Down

0 comments on commit 6c49aa3

Please sign in to comment.