-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
493 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.