Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check whether it is nil before using a variable as a object #589

Merged
merged 1 commit into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions api/internal/handler/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
60 changes: 60 additions & 0 deletions api/internal/handler/route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
8 changes: 6 additions & 2 deletions api/internal/handler/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand Down
46 changes: 46 additions & 0 deletions api/internal/handler/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}