From f7e8a8148e85373a33c2000fa254bd9f5899e3a7 Mon Sep 17 00:00:00 2001 From: tydhot <583125614@qq.com> Date: Wed, 30 Jun 2021 17:45:02 +0800 Subject: [PATCH] Enrich response.go's test case --- pkg/filter/response/response_test.go | 62 ++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/pkg/filter/response/response_test.go b/pkg/filter/response/response_test.go index e86561b44..b5b83aa68 100644 --- a/pkg/filter/response/response_test.go +++ b/pkg/filter/response/response_test.go @@ -25,17 +25,71 @@ import ( "github.com/stretchr/testify/assert" ) -func TestNewDubboResponse(t *testing.T) { +func TestNewDubboMapResponse(t *testing.T) { + slice := append(make([]string, 0), "read") resp := map[string]interface{}{ + "age": 18, + "iD": 0o001, + "name": "tc", + "time": nil, + "hobby": slice, + "father": map[string]interface{}{ + "name": "bob", + }, + } + result, err := dealResp(resp, false) + if err != nil { + t.Error("failed to deal resp") + } + r := result.(map[string]interface{}) + assert.Equal(t, 18, r["age"]) + assert.Equal(t, 1, r["iD"]) + assert.Equal(t, "tc", r["name"]) + assert.Equal(t, "read", r["hobby"].([]string)[0]) + assert.Equal(t, "bob", r["father"].(map[string]interface{})["name"]) +} + +func TestNewDubboMapHumpResponse(t *testing.T) { + slice := append(make([]string, 0), "readBook") + resp := map[string]interface{}{ + "age": 18, + "iD": 0o001, + "name": "tc", + "time": nil, + "simpleHobby": slice, + "father": map[string]interface{}{ + "name": "bob", + }, + } + result, err := dealResp(resp, true) + if err != nil { + t.Error("failed to deal resp") + } + r := result.(map[string]interface{}) + assert.Equal(t, 18, r["age"]) + assert.Equal(t, 1, r["i_d"]) + assert.Equal(t, "tc", r["name"]) + assert.Equal(t, "readBook", r["simple_hobby"].([]interface{})[0]) + assert.Equal(t, "bob", r["father"].(map[string]interface{})["name"]) +} + +func TestNewDubboSliceResponse(t *testing.T) { + resp := append(make([]map[string]interface{}, 0), map[string]interface{}{ "age": 18, "iD": 0o001, "name": "tc", "time": nil, + "father": map[string]interface{}{ + "name": "bob", + }, + }) + result, err := dealResp(resp, false) + if err != nil { + t.Error("failed to deal resp") } - f := responseFilter{} - result := f.newResponse(resp) - r := result.Data.(map[string]interface{}) + r := result.([]interface{})[0].(map[string]interface{}) assert.Equal(t, 18, r["age"]) assert.Equal(t, 1, r["iD"]) assert.Equal(t, "tc", r["name"]) + assert.Equal(t, "bob", r["father"].(map[string]interface{})["name"]) }