Skip to content

Commit

Permalink
feat: increased coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Semior001 committed Mar 10, 2024
1 parent 7d2d799 commit a12d6b9
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 7 deletions.
5 changes: 3 additions & 2 deletions pkg/proxy/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"google.golang.org/protobuf/proto"
)

// RawBytesCodec sets the received bytes as is to the target,
// which must always be a pointer to a byte slice.
// RawBytesCodec sets the received bytes as-is to the target,
// whether it is a byte slice or a proto.Message.
// For proto.Message, it uses proto.Marshal and proto.Unmarshal.
type RawBytesCodec struct{}

// Marshal returns the received byte slice as is.
Expand Down
43 changes: 43 additions & 0 deletions pkg/proxy/codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package proxy

import (
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/protobuf/proto"
)

func TestRawBytesCodec_Marshal(t *testing.T) {
t.Run("proto message", func(t *testing.T) {
bts, err := RawBytesCodec{}.Marshal(&errdetails.RequestInfo{RequestId: "1"})
require.NoError(t, err)
expected, err := proto.Marshal(&errdetails.RequestInfo{RequestId: "1"})
require.NoError(t, err)
require.Equal(t, expected, bts)
})

t.Run("byte slice", func(t *testing.T) {
bts, err := RawBytesCodec{}.Marshal(&[]byte{1, 2, 3})
require.NoError(t, err)
require.Equal(t, []byte{1, 2, 3}, bts)
})
}

func TestRawBytesCodec_Unmarshal(t *testing.T) {
t.Run("proto message", func(t *testing.T) {
bts, err := proto.Marshal(&errdetails.RequestInfo{RequestId: "1"})
require.NoError(t, err)

msg := &errdetails.RequestInfo{}
require.NoError(t, RawBytesCodec{}.Unmarshal(bts, msg))
require.Truef(t, proto.Equal(&errdetails.RequestInfo{RequestId: "1"}, msg), "got: %v", msg)
})

t.Run("byte slice", func(t *testing.T) {
var bts []byte
err := RawBytesCodec{}.Unmarshal([]byte{1, 2, 3}, &bts)
require.NoError(t, err)
require.Equal(t, []byte{1, 2, 3}, bts)
})
}
4 changes: 1 addition & 3 deletions pkg/proxy/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ func Chain(base grpc.StreamHandler, mws ...Middleware) grpc.StreamHandler {
func AppInfo(app, author, version string) Middleware {
return func(next grpc.StreamHandler) grpc.StreamHandler {
return func(srv any, stream grpc.ServerStream) error {
ctx := stream.Context()

md := metadata.Pairs(
"app", app,
"author", author,
"version", version,
)

if err := stream.SetHeader(md); err != nil {
slog.WarnContext(ctx, "failed to send app info", slogx.Error(err))
slog.WarnContext(stream.Context(), "failed to send app info", slogx.Error(err))
}

return next(srv, stream)
Expand Down
77 changes: 77 additions & 0 deletions pkg/proxy/middleware/middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package middleware

import (
"bytes"
"context"
"log/slog"
"testing"

"github.com/Semior001/groxy/pkg/proxy/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)

func TestAppInfo(t *testing.T) {
mw := AppInfo("app", "author", "version")
var header metadata.MD
ss := &mocks.ServerStreamMock{
SetHeaderFunc: func(md metadata.MD) error {
header = md
return nil
},
}

err := mw(func(_ any, _ grpc.ServerStream) error { return nil })(nil, ss)
require.NoError(t, err)

assert.Equal(t, metadata.Pairs(
"app", "app",
"author", "author",
"version", "version",
), header)
}

func TestRecoverer(t *testing.T) {
bts := bytes.NewBuffer(nil)
slog.SetDefault(slog.New(slog.NewTextHandler(bts, &slog.HandlerOptions{})))
mw := Recoverer(func(_ any, _ grpc.ServerStream) error { panic("test") })
var err error
require.NotPanics(t, func() {
err = mw(nil, &mocks.ServerStreamMock{
ContextFunc: func() context.Context { return context.Background() },
})
})
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.ResourceExhausted, st.Code())
assert.Equal(t, "{groxy} panic", st.Message())
}

func TestChain(t *testing.T) {
var calls []string
mw1 := func(next grpc.StreamHandler) grpc.StreamHandler {
return func(srv any, stream grpc.ServerStream) error {
calls = append(calls, "mw1")
return next(srv, stream)
}
}
mw2 := func(next grpc.StreamHandler) grpc.StreamHandler {
return func(srv any, stream grpc.ServerStream) error {
calls = append(calls, "mw2")
return next(srv, stream)
}
}
mw3 := func(next grpc.StreamHandler) grpc.StreamHandler {
return func(srv any, stream grpc.ServerStream) error {
calls = append(calls, "mw3")
return next(srv, stream)
}
}
h := Chain(func(_ any, _ grpc.ServerStream) error { return nil }, mw1, mw2, mw3)
require.NoError(t, h(nil, nil))
assert.Equal(t, []string{"mw1", "mw2", "mw3"}, calls)
}
Loading

0 comments on commit a12d6b9

Please sign in to comment.