From e133ca73a80f5a8de1e8316c6632081681f573f6 Mon Sep 17 00:00:00 2001 From: nic-chen <33000667+nic-chen@users.noreply.github.com> Date: Fri, 23 Oct 2020 00:02:44 +0800 Subject: [PATCH] fix: check whether it is nil before using the variable (#589) --- api/internal/handler/route/route.go | 8 ++- api/internal/handler/route/route_test.go | 60 ++++++++++++++++++++ api/internal/handler/service/service.go | 8 ++- api/internal/handler/service/service_test.go | 46 +++++++++++++++ 4 files changed, 118 insertions(+), 4 deletions(-) diff --git a/api/internal/handler/route/route.go b/api/internal/handler/route/route.go index c0ba6a9775..5794951610 100644 --- a/api/internal/handler/route/route.go +++ b/api/internal/handler/route/route.go @@ -93,7 +93,9 @@ func (h *Handler) Get(c droplet.Context) (interface{}, error) { } //format - route.Upstream.Nodes = entity.NodesFormat(route.Upstream.Nodes) + if route.Upstream != nil && route.Upstream.Nodes != nil { + route.Upstream.Nodes = entity.NodesFormat(route.Upstream.Nodes) + } return route, nil } @@ -138,7 +140,9 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) { }, Format: func(obj interface{}) interface{} { route := obj.(*entity.Route) - route.Upstream.Nodes = entity.NodesFormat(route.Upstream.Nodes) + if route.Upstream != nil && route.Upstream.Nodes != nil { + route.Upstream.Nodes = entity.NodesFormat(route.Upstream.Nodes) + } return route }, PageSize: input.PageSize, diff --git a/api/internal/handler/route/route_test.go b/api/internal/handler/route/route_test.go index 0b23a496ce..3f79cd378f 100644 --- a/api/internal/handler/route/route_test.go +++ b/api/internal/handler/route/route_test.go @@ -907,4 +907,64 @@ func TestRoute(t *testing.T) { assert.NotNil(t, err) assert.Equal(t, http.StatusBadRequest, ret.(*data.SpecCodeResponse).StatusCode) + //create route with out upstream + route11 := &entity.Route{} + reqBody = `{ + "id": "11", + "name": "bbbbb", + "uri": "/r11", + "hosts": ["foo.com", "*.bar.com"], + "remote_addrs": ["127.0.0.0/8"], + "methods": ["PUT", "GET"], + "plugins": { + "limit-count": { + "count": 2, + "time_window": 60, + "rejected_code": 503, + "key": "remote_addr" + } + } + }` + json.Unmarshal([]byte(reqBody), route11) + ctx.SetInput(route11) + _, err = handler.Create(ctx) + assert.Nil(t, err) + + //sleep + time.Sleep(time.Duration(100) * time.Millisecond) + + //get + input11 := &GetInput{} + input11.ID = "11" + ctx.SetInput(input11) + ret, err = handler.Get(ctx) + assert.Nil(t, err) + stored = ret.(*entity.Route) + assert.Equal(t, "11", stored.ID) + + //list + listInput11 := &ListInput{} + reqBody = `{"page_size": 10, "page": 1}` + json.Unmarshal([]byte(reqBody), listInput11) + ctx.SetInput(listInput11) + retPage, err = handler.List(ctx) + assert.Nil(t, err) + + //list search match + listInput12 := &ListInput{} + reqBody = `{"page_size": 1, "page": 1, "uri": "r11"}` + json.Unmarshal([]byte(reqBody), listInput12) + ctx.SetInput(listInput12) + retPage, err = handler.List(ctx) + assert.Nil(t, err) + dataPage = retPage.(*store.ListOutput) + assert.Equal(t, len(dataPage.Rows), 1) + + //delete test data + reqBody = `{"ids": "11"}` + json.Unmarshal([]byte(reqBody), inputDel) + ctx.SetInput(inputDel) + _, err = handler.BatchDelete(ctx) + assert.Nil(t, err) + } diff --git a/api/internal/handler/service/service.go b/api/internal/handler/service/service.go index 3c15b8e5b9..64d46537d9 100644 --- a/api/internal/handler/service/service.go +++ b/api/internal/handler/service/service.go @@ -76,7 +76,9 @@ func (h *Handler) Get(c droplet.Context) (interface{}, error) { } service := r.(*entity.Service) - service.Upstream.Nodes = entity.NodesFormat(service.Upstream.Nodes) + if service.Upstream != nil && service.Upstream.Nodes != nil { + service.Upstream.Nodes = entity.NodesFormat(service.Upstream.Nodes) + } return r, nil } @@ -98,7 +100,9 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) { }, Format: func(obj interface{}) interface{} { service := obj.(*entity.Service) - service.Upstream.Nodes = entity.NodesFormat(service.Upstream.Nodes) + if service.Upstream != nil && service.Upstream.Nodes != nil { + service.Upstream.Nodes = entity.NodesFormat(service.Upstream.Nodes) + } return service }, PageSize: input.PageSize, diff --git a/api/internal/handler/service/service_test.go b/api/internal/handler/service/service_test.go index 0bf78eb1cf..42efc4d56d 100644 --- a/api/internal/handler/service/service_test.go +++ b/api/internal/handler/service/service_test.go @@ -149,4 +149,50 @@ func TestService(t *testing.T) { _, err = handler.BatchDelete(ctx) assert.Nil(t, err) + //create without upstream + service11 := &entity.Service{} + reqBody = `{ + "id": "11", + "plugins": { + "limit-count": { + "count": 2, + "time_window": 60, + "rejected_code": 503, + "key": "remote_addr" + } + } + }` + json.Unmarshal([]byte(reqBody), service11) + ctx.SetInput(service11) + _, err = handler.Create(ctx) + assert.Nil(t, err) + + //sleep + time.Sleep(time.Duration(100) * time.Millisecond) + + //get + input11 := &GetInput{} + input11.ID = "11" + ctx.SetInput(input11) + ret, err = handler.Get(ctx) + stored = ret.(*entity.Service) + assert.Nil(t, err) + assert.Equal(t, "11", stored.ID) + + //list + listInput11 := &ListInput{} + reqBody = `{"page_size": 10, "page": 1}` + json.Unmarshal([]byte(reqBody), listInput11) + ctx.SetInput(listInput11) + retPage, err = handler.List(ctx) + assert.Nil(t, err) + + //delete test data + inputDel11 := &BatchDelete{} + reqBody = `{"ids": "11"}` + json.Unmarshal([]byte(reqBody), inputDel11) + ctx.SetInput(inputDel11) + _, err = handler.BatchDelete(ctx) + assert.Nil(t, err) + }