diff --git a/grpc/response_test.go b/grpc/response_test.go index d562505..3088039 100644 --- a/grpc/response_test.go +++ b/grpc/response_test.go @@ -1,9 +1,11 @@ -package grpc +package grpc_test import ( "log" "testing" + "github.com/gojek/fiber" + "github.com/gojek/fiber/grpc" testproto "github.com/gojek/fiber/internal/testdata/gen/testdata/proto" "github.com/stretchr/testify/assert" "google.golang.org/grpc/codes" @@ -15,16 +17,16 @@ import ( func TestResponse_Backend(t *testing.T) { tests := []struct { name string - res Response - want Response + res grpc.Response + want grpc.Response backendName string }{ { name: "ok", - res: Response{ + res: grpc.Response{ Metadata: map[string][]string{}, }, - want: Response{ + want: grpc.Response{ Metadata: metadata.New(map[string]string{"backend": "testing"}), }, backendName: "testing", @@ -42,13 +44,13 @@ func TestResponse_Backend(t *testing.T) { func TestResponse_Status(t *testing.T) { tests := []struct { name string - res Response + res grpc.Response expectedCode int expectedSuccess bool }{ { name: "ok", - res: Response{ + res: grpc.Response{ Status: *status.New(codes.OK, ""), }, expectedCode: 0, @@ -56,7 +58,7 @@ func TestResponse_Status(t *testing.T) { }, { name: "ok", - res: Response{ + res: grpc.Response{ Status: *status.New(codes.InvalidArgument, ""), }, expectedCode: 3, @@ -83,12 +85,12 @@ func TestResponse_Payload(t *testing.T) { responseByte, _ := proto.Marshal(response) tests := []struct { name string - req Response + req grpc.Response expected []byte }{ { name: "", - req: Response{ + req: grpc.Response{ Message: responseByte, }, expected: responseByte, @@ -100,3 +102,98 @@ func TestResponse_Payload(t *testing.T) { }) } } + +func TestResponse_Label(t *testing.T) { + tests := map[string]struct { + response fiber.Response + key string + expected []string + }{ + "empty labels": { + response: &grpc.Response{ + Metadata: map[string][]string{}, + }, + key: "dummy-key", + }, + "non-empty labels": { + response: &grpc.Response{ + Metadata: map[string][]string{"key": []string{"v1", "v2"}}, + }, + key: "key", + expected: []string{"v1", "v2"}, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + values := tt.response.Label(tt.key) + assert.Equal(t, tt.expected, values) + }) + } +} + +func TestResponse_WithLabel(t *testing.T) { + tests := map[string]struct { + response fiber.Response + key string + values []string + expected []string + }{ + "new labels": { + response: &grpc.Response{ + Metadata: map[string][]string{"key": []string{"v1", "v2"}}, + }, + key: "k1", + values: []string{"v1", "v2"}, + expected: []string{"v1", "v2"}, + }, + "append labels": { + response: &grpc.Response{ + Metadata: map[string][]string{"k1": []string{"v1", "v2"}}, + }, + key: "k1", + values: []string{"v3"}, + expected: []string{"v1", "v2", "v3"}, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + newLabels := tt.response.WithLabel(tt.key, tt.values...) + assert.Equal(t, tt.expected, newLabels.Label(tt.key)) + }) + } +} + +func TestHTTPResponse_WithLabels(t *testing.T) { + tests := map[string]struct { + response fiber.Response + labels fiber.Labels + key string + expected []string + }{ + "new labels": { + response: &grpc.Response{ + Metadata: map[string][]string{"key": []string{"v1", "v2"}}, + }, + labels: fiber.LabelsMap{"k1": []string{"v1", "v2"}}, + key: "k1", + expected: []string{"v1", "v2"}, + }, + "append labels": { + response: &grpc.Response{ + Metadata: map[string][]string{"k1": []string{"v1", "v2"}}, + }, + labels: fiber.LabelsMap{"k1": []string{"v3"}}, + key: "k1", + expected: []string{"v1", "v2", "v3"}, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + newLabels := tt.response.WithLabels(tt.labels) + assert.Equal(t, tt.expected, newLabels.Label(tt.key)) + }) + } +} diff --git a/http/response_test.go b/http/response_test.go index be9c21c..5e23b28 100644 --- a/http/response_test.go +++ b/http/response_test.go @@ -5,8 +5,10 @@ import ( "net/http" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/gojek/fiber" fiberHTTP "github.com/gojek/fiber/http" "github.com/gojek/fiber/internal/testutils" ) @@ -91,3 +93,107 @@ func TestNewHTTPResponse(t *testing.T) { }) } } + +func TestHTTPResponseLabel(t *testing.T) { + tests := map[string]struct { + response fiber.Response + key string + expected []string + }{ + "empty labels": { + response: fiberHTTP.NewHTTPResponse(&http.Response{ + Body: makeBody([]byte("{}")), + StatusCode: http.StatusOK, + }), + key: "dummy-key", + }, + "case insensitive key": { + response: fiberHTTP.NewHTTPResponse(&http.Response{ + Header: http.Header{"Key": []string{"v1", "v2"}}, + Body: makeBody([]byte("{}")), + StatusCode: http.StatusOK, + }), + key: "Key", + expected: []string{"v1", "v2"}, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + values := tt.response.Label(tt.key) + assert.Equal(t, tt.expected, values) + }) + } +} + +func TestHTTPResponseWithLabel(t *testing.T) { + tests := map[string]struct { + response fiber.Response + key string + values []string + expected []string + }{ + "new labels": { + response: fiberHTTP.NewHTTPResponse(&http.Response{ + Body: makeBody([]byte("{}")), + StatusCode: http.StatusOK, + }), + key: "k1", + values: []string{"v1", "v2"}, + expected: []string{"v1", "v2"}, + }, + "append labels": { + response: fiberHTTP.NewHTTPResponse(&http.Response{ + Header: http.Header{"K1": []string{"v1", "v2"}}, + Body: makeBody([]byte("{}")), + StatusCode: http.StatusOK, + }), + key: "k1", + values: []string{"v3"}, + expected: []string{"v1", "v2", "v3"}, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + newLabels := tt.response.WithLabel(tt.key, tt.values...) + assert.Equal(t, tt.expected, newLabels.Label(tt.key)) + }) + } +} + +func TestHTTPResponseWithLabels(t *testing.T) { + tests := map[string]struct { + response fiber.Response + labels fiber.Labels + key string + expected []string + }{ + "new labels": { + response: fiberHTTP.NewHTTPResponse(&http.Response{ + Body: makeBody([]byte("{}")), + StatusCode: http.StatusOK, + }), + labels: fiber.LabelsMap{"k1": []string{"v1", "v2"}}, + key: "K1", + expected: []string{"v1", "v2"}, + }, + "append labels": { + response: fiberHTTP.NewHTTPResponse(&http.Response{ + Header: http.Header{"K1": []string{"v1", "v2"}}, + Body: makeBody([]byte("{}")), + StatusCode: http.StatusOK, + }), + labels: fiber.LabelsMap{"k1": []string{"v3"}}, + key: "k1", + expected: []string{"v1", "v2", "v3"}, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + newLabels := tt.response.WithLabels(tt.labels) + assert.Equal(t, tt.expected, newLabels.Label(tt.key)) + }) + } +} diff --git a/labels_test.go b/labels_test.go new file mode 100644 index 0000000..ead1f21 --- /dev/null +++ b/labels_test.go @@ -0,0 +1,91 @@ +package fiber_test + +import ( + "sort" + "testing" + + "github.com/gojek/fiber" + "github.com/stretchr/testify/assert" +) + +func TestLabelsMapKeys(t *testing.T) { + tests := map[string]struct { + data fiber.LabelsMap + expected []string + }{ + "empty map": { + data: fiber.LabelsMap{}, + expected: []string{}, + }, + "non-empty map": { + data: fiber.LabelsMap{"k1": []string{"v1", "v2"}, "k2": []string{"v3"}}, + expected: []string{"k1", "k2"}, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + keys := tt.data.Keys() + // Sort and compare + sort.Slice(keys, func(p, q int) bool { + return keys[p] < keys[q] + }) + assert.Equal(t, tt.expected, keys) + }) + } +} + +func TestLabelsMapWithLabel(t *testing.T) { + tests := map[string]struct { + data fiber.LabelsMap + key string + values []string + expected fiber.LabelsMap + }{ + "set key": { + data: fiber.LabelsMap{}, + key: "k1", + values: []string{"v1", "v2"}, + expected: fiber.LabelsMap{"k1": []string{"v1", "v2"}}, + }, + "overwrite key": { + data: fiber.LabelsMap{"k1": []string{"v1", "v2"}, "k2": []string{"v3"}}, + key: "k2", + values: []string{"new-val"}, + expected: fiber.LabelsMap{"k1": []string{"v1", "v2"}, "k2": []string{"new-val"}}, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + newMap := tt.data.WithLabel(tt.key, tt.values...) + assert.Equal(t, tt.expected, newMap) + }) + } +} + +func TestLabelsMapLabel(t *testing.T) { + tests := map[string]struct { + data fiber.LabelsMap + key string + expected []string + }{ + "empty map": { + data: fiber.LabelsMap{}, + key: "k", + expected: []string{}, + }, + "non-empty map": { + data: fiber.LabelsMap{"k1": []string{"v1", "v2"}, "k2": []string{"v3"}}, + key: "k1", + expected: []string{"v1", "v2"}, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + values := tt.data.Label(tt.key) + assert.Equal(t, tt.expected, values) + }) + } +}