diff --git a/api/internal/core/entity/format.go b/api/internal/core/entity/format.go index 0c5d8d1061..a409644347 100644 --- a/api/internal/core/entity/format.go +++ b/api/internal/core/entity/format.go @@ -23,8 +23,8 @@ import ( ) func NodesFormat(obj interface{}) interface{} { + var nodes []*Node if value, ok := obj.(map[string]float64); ok { - var nodes []*Node var strArr []string for key, val := range value { node := &Node{} @@ -48,5 +48,22 @@ func NodesFormat(obj interface{}) interface{} { return nodes } + if nodes, ok := obj.([]*Node); ok { + return nodes + } + + if list, ok := obj.([]interface{}); ok { + for _, v := range list { + val := v.(map[string]interface{}) + node := &Node{} + node.Host = val["host"].(string) + node.Port = int(val["port"].(float64)) + node.Weight = int(val["weight"].(float64)) + nodes = append(nodes, node) + } + + return nodes + } + return obj } diff --git a/api/internal/handler/upstream/upstream_test.go b/api/internal/handler/upstream/upstream_test.go index 197d3a8fe3..f562c0de9f 100644 --- a/api/internal/handler/upstream/upstream_test.go +++ b/api/internal/handler/upstream/upstream_test.go @@ -30,6 +30,8 @@ import ( "github.com/apisix/manager-api/internal/core/store" ) +var upstreamHandler *Handler + func TestUpstream(t *testing.T) { // init err := storage.InitETCDClient([]string{"127.0.0.1:2379"}) @@ -37,10 +39,10 @@ func TestUpstream(t *testing.T) { err = store.InitStores() assert.Nil(t, err) - handler := &Handler{ + upstreamHandler = &Handler{ upstreamStore: store.GetStore(store.HubKeyUpstream), } - assert.NotNil(t, handler) + assert.NotNil(t, upstreamHandler) //create ctx := droplet.NewContext() @@ -94,7 +96,7 @@ func TestUpstream(t *testing.T) { err = json.Unmarshal([]byte(reqBody), upstream) assert.Nil(t, err) ctx.SetInput(upstream) - _, err = handler.Create(ctx) + _, err = upstreamHandler.Create(ctx) assert.Nil(t, err) //sleep @@ -104,7 +106,7 @@ func TestUpstream(t *testing.T) { input := &GetInput{} input.ID = "1" ctx.SetInput(input) - ret, err := handler.Get(ctx) + ret, err := upstreamHandler.Get(ctx) stored := ret.(*entity.Upstream) assert.Nil(t, err) assert.Equal(t, stored.ID, upstream.ID) @@ -161,7 +163,7 @@ func TestUpstream(t *testing.T) { err = json.Unmarshal([]byte(reqBody), upstream2) assert.Nil(t, err) ctx.SetInput(upstream2) - _, err = handler.Update(ctx) + _, err = upstreamHandler.Update(ctx) assert.Nil(t, err) //list @@ -170,7 +172,7 @@ func TestUpstream(t *testing.T) { err = json.Unmarshal([]byte(reqBody), listInput) assert.Nil(t, err) ctx.SetInput(listInput) - retPage, err := handler.List(ctx) + retPage, err := upstreamHandler.List(ctx) assert.Nil(t, err) dataPage := retPage.(*store.ListOutput) assert.Equal(t, len(dataPage.Rows), 1) @@ -181,7 +183,50 @@ func TestUpstream(t *testing.T) { err = json.Unmarshal([]byte(reqBody), inputDel) assert.Nil(t, err) ctx.SetInput(inputDel) - _, err = handler.BatchDelete(ctx) + _, err = upstreamHandler.BatchDelete(ctx) + assert.Nil(t, err) + +} + +func TestUpstream_Pass_Host(t *testing.T) { + //create + ctx := droplet.NewContext() + upstream := &entity.Upstream{} + reqBody := `{ + "id": "2", + "nodes": [{ + "host": "httpbin.org", + "port": 80, + "weight": 1 + }], + "type": "roundrobin", + "pass_host": "node" + }` + err := json.Unmarshal([]byte(reqBody), upstream) + assert.Nil(t, err) + ctx.SetInput(upstream) + _, err = upstreamHandler.Create(ctx) + assert.Nil(t, err) + + //sleep + time.Sleep(time.Duration(20) * time.Millisecond) + + //get + input := &GetInput{} + input.ID = "2" + ctx.SetInput(input) + ret, err := upstreamHandler.Get(ctx) + stored := ret.(*entity.Upstream) + assert.Nil(t, err) + assert.Equal(t, stored.ID, upstream.ID) + + //delete test data + inputDel := &BatchDelete{} + reqBody = `{"ids": "2"}` + err = json.Unmarshal([]byte(reqBody), inputDel) + assert.Nil(t, err) + ctx.SetInput(inputDel) + _, err = upstreamHandler.BatchDelete(ctx) assert.Nil(t, err) }