Skip to content

Commit

Permalink
Removed unnecessary comments and whitespaces
Browse files Browse the repository at this point in the history
Signed-off-by: Prashant Dwivedi <[email protected]>
  • Loading branch information
Prashant-Dwivedi-08-01 committed Jun 23, 2024
1 parent 2687a13 commit 85cd1c4
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 69 deletions.
17 changes: 6 additions & 11 deletions pkg/api/grpc/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (

func TestGrpcEcho(t *testing.T) {

// Server initialization
// bufconn => uses in-memory connection instead of system network I/O
lis := bufconn.Listen(1024*1024)
lis := bufconn.Listen(1024 * 1024)
t.Cleanup(func() {
lis.Close()
})
Expand All @@ -30,19 +28,18 @@ func TestGrpcEcho(t *testing.T) {

echo.RegisterEchoServiceServer(srv, &echoServer{config: s.config, logger: s.logger})

go func(){
go func() {
if err := srv.Serve(lis); err != nil {
log.Fatalf("srv.Serve %v", err)
}
}()

// - Test
dialer := func(context.Context, string) (net.Conn, error){
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}

ctx := context.Background()

conn, err := grpc.DialContext(ctx, "", grpc.WithContextDialer(dialer), grpc.WithInsecure())
t.Cleanup(func() {
conn.Close()
Expand All @@ -53,18 +50,16 @@ func TestGrpcEcho(t *testing.T) {
}

client := echo.NewEchoServiceClient(conn)
res , err := client.Echo(context.Background(), &echo.Message{Body:"test123-test"})
res, err := client.Echo(context.Background(), &echo.Message{Body: "test123-test"})

// Check the status code is what we expect.
if _, ok := status.FromError(err); !ok {
t.Errorf("Echo returned type %T, want %T", err, status.Error)
}

// Check the response body is what we expect.
expected := ".*body.*test123-test.*"
r := regexp.MustCompile(expected)
if !r.MatchString(res.String()) {
t.Fatalf("Returned unexpected body:\ngot \n%v \nwant \n%s",
res, expected)
}
}
}
5 changes: 0 additions & 5 deletions pkg/api/grpc/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (

func TestGrpcEnv(t *testing.T) {

// Server initialization
// bufconn => uses in-memory connection instead of system network I/O
lis := bufconn.Listen(1024 * 1024)
t.Cleanup(func() {
lis.Close()
Expand All @@ -35,7 +33,6 @@ func TestGrpcEnv(t *testing.T) {
}
}()

// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
Expand All @@ -54,12 +51,10 @@ func TestGrpcEnv(t *testing.T) {
client := env.NewEnvServiceClient(conn)
res, err := client.Env(context.Background(), &env.EnvRequest{})

// Check the status code is what we expect.
if _, ok := status.FromError(err); !ok {
t.Errorf("Env returned type %T, want %T", err, status.Error)
}

// Check the response body is what we expect.
expected := ".*PATH.*"
r := regexp.MustCompile(expected)
if !r.MatchString(res.String()) {
Expand Down
11 changes: 0 additions & 11 deletions pkg/api/grpc/headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (

func TestGrpcHeader(t *testing.T) {

// Server initialization
// bufconn => uses in-memory connection instead of system network I/O
lis := bufconn.Listen(1024 * 1024)
t.Cleanup(func() {
lis.Close()
Expand All @@ -36,7 +34,6 @@ func TestGrpcHeader(t *testing.T) {
}
}()

// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
Expand All @@ -50,27 +47,19 @@ func TestGrpcHeader(t *testing.T) {
t.Fatalf("grpc.DialContext %v", err)
}

// Prepare your headers as key-value pairs.
headers := metadata.New(map[string]string{
"X-Test": "testing",
})

// Create a context with the headers attached.
ctx := metadata.NewOutgoingContext(context.Background(), headers)

client := header.NewHeaderServiceClient(conn)
res, err := client.Header(ctx, &header.HeaderRequest{})

// Check the status code is what we expect.
if _, ok := status.FromError(err); !ok {
t.Errorf("Header returned type %T, want %T", err, status.Error)
}
// if res != nil {
// fmt.Printf("res %v\n", res)
// // fmt.Printf(res.Color, " ", reflect.TypeOf(res.Color))
// }

// Check the response body is what we expect.
expected := ".*testing.*"
r := regexp.MustCompile(expected)
if !r.MatchString(res.String()) {
Expand Down
7 changes: 1 addition & 6 deletions pkg/api/grpc/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ import (

func TestGrpcInfo(t *testing.T) {

// Server initialization
// bufconn => uses in-memory connection instead of system network I/O
lis := bufconn.Listen(1024 * 1024)
t.Cleanup(func() {
lis.Close()
})

s := NewMockGrpcServer()
srv := grpc.NewServer() // replace this with Mock that return srv that has all the config, logger, etc
srv := grpc.NewServer()
t.Cleanup(func() {
srv.Stop()
})
Expand All @@ -36,7 +34,6 @@ func TestGrpcInfo(t *testing.T) {
}
}()

// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
Expand All @@ -55,12 +52,10 @@ func TestGrpcInfo(t *testing.T) {
client := info.NewInfoServiceClient(conn)
res, err := client.Info(context.Background(), &info.InfoRequest{})

// Check the status code is what we expect.
if _, ok := status.FromError(err); !ok {
t.Errorf("Info returned type %T, want %T", err, status.Error)
}

// Check the response body is what we expect.
expected := ".*color.*blue.*"
r := regexp.MustCompile(expected)
if !r.MatchString(res.String()) {
Expand Down
7 changes: 0 additions & 7 deletions pkg/api/grpc/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ type StatusServer struct {
logger *zap.Logger
}

// SayHello implements helloworld.GreeterServer

func (s *StatusServer) Status(ctx context.Context, req *pb.StatusRequest) (*pb.StatusResponse, error) {
reqCode := req.GetCode()
// return &pb.StatusResponse{Status: reqCode}, nil

// type Code Code.code
grpcCodes := map[string]codes.Code{
"Ok": codes.OK,
"Canceled": codes.Canceled,
Expand All @@ -42,10 +38,7 @@ func (s *StatusServer) Status(ctx context.Context, req *pb.StatusRequest) (*pb.S
"Unauthenticated": codes.Unauthenticated,
}

// try to access the map with the request code string as key. If the key is not found, return an error
// if the key is found, return the grpc status code
code, ok := grpcCodes[reqCode]
//s.logger.Info(string(code))
if !ok {
return nil, status.Error(codes.Unknown, "Unknown status code for more information check https://chromium.googlesource.com/external/github.com/grpc/grpc/+/refs/tags/v1.21.4-pre1/doc/statuscodes.md")
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/api/grpc/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (

func TestGrpcStatusError(t *testing.T) {

// Server initialization
// bufconn => uses in-memory connection instead of system network I/O
lis := bufconn.Listen(1024 * 1024)
t.Cleanup(func() {
lis.Close()
Expand All @@ -37,7 +35,6 @@ func TestGrpcStatusError(t *testing.T) {
}
}()

// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
Expand Down Expand Up @@ -72,8 +69,6 @@ func TestGrpcStatusError(t *testing.T) {

func TestGrpcStatusOk(t *testing.T) {

// Server initialization
// bufconn => uses in-memory connection instead of system network I/O
lis := bufconn.Listen(1024 * 1024)
t.Cleanup(func() {
lis.Close()
Expand All @@ -92,7 +87,6 @@ func TestGrpcStatusOk(t *testing.T) {
}
}()

// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/api/grpc/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ type jwtCustomClaims struct {
jwt.StandardClaims
}

// SayHello implements helloworld.GreeterServer

func (s *TokenServer) TokenGenerate(ctx context.Context, req *pb.TokenRequest) (*pb.TokenResponse, error) {

user := "anonymous"
Expand Down Expand Up @@ -57,21 +55,18 @@ func (s *TokenServer) TokenGenerate(ctx context.Context, req *pb.TokenRequest) (
return &result, nil
}

// code to get the authorization token from the header of grpc request and validate it if it is expired or not
func (s *TokenServer) TokenValidate(ctx context.Context, req *pb.TokenRequest) (*pb.TokenResponse, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Errorf(codes.DataLoss, "UnaryEcho: failed to get metadata")
}

// Retrieve the bearer token from the "authorization" key in metadata
authorization := md.Get("authorization")

if len(authorization) == 0 {
return nil, status.Errorf(codes.Unauthenticated, "Authorization token not found in metadata")
}

// Extract the token from the value
token := strings.TrimSpace(strings.TrimPrefix(authorization[0], "Bearer"))

claims := jwtCustomClaims{}
Expand Down
4 changes: 0 additions & 4 deletions pkg/api/grpc/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (

func TestGrpcToken(t *testing.T) {

// Server initialization
// bufconn => uses in-memory connection instead of system network I/O
lis := bufconn.Listen(1024 * 1024)
t.Cleanup(func() {
lis.Close()
Expand All @@ -35,7 +33,6 @@ func TestGrpcToken(t *testing.T) {
}
}()

// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
Expand All @@ -54,7 +51,6 @@ func TestGrpcToken(t *testing.T) {
client := token.NewTokenServiceClient(conn)
res, err := client.TokenGenerate(context.Background(), &token.TokenRequest{})

// Check the status code is what we expect.
if _, ok := status.FromError(err); !ok {
t.Errorf("Token Handler returned type %T, want %T", err, status.Error)
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/api/grpc/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ type VersionServer struct {
pb.UnimplementedVersionServiceServer
config *Config
logger *zap.Logger

}

func (s *VersionServer) Version(ctx context.Context, req *pb.VersionRequest) (*pb.VersionResponse, error) {
return &pb.VersionResponse{Version: version.VERSION, Commit: version.REVISION}, nil
}

18 changes: 6 additions & 12 deletions pkg/api/grpc/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,30 @@ import (

func TestGrpcVersion(t *testing.T) {

// Server initialization
// bufconn => uses in-memory connection instead of system network I/O
lis := bufconn.Listen(1024*1024)
lis := bufconn.Listen(1024 * 1024)
t.Cleanup(func() {
lis.Close()
})


srv := grpc.NewServer()
t.Cleanup(func() {
srv.Stop()
})

version.RegisterVersionServiceServer(srv, &VersionServer{})

go func(){
go func() {
if err := srv.Serve(lis); err != nil {
log.Fatalf("srv.Serve %v", err)
}
}()

// - Test
dialer := func(context.Context, string) (net.Conn, error){
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}

ctx := context.Background()

conn, err := grpc.DialContext(ctx, "", grpc.WithContextDialer(dialer), grpc.WithInsecure())
t.Cleanup(func() {
conn.Close()
Expand All @@ -55,18 +51,16 @@ func TestGrpcVersion(t *testing.T) {
}

client := version.NewVersionServiceClient(conn)
res , err := client.Version(context.Background(), &version.VersionRequest{})
res, err := client.Version(context.Background(), &version.VersionRequest{})

// Check the status code is what we expect.
if _, ok := status.FromError(err); !ok {
t.Errorf("Version returned type %T, want %T", err, status.Error)
}

// Check the response body is what we expect.
expected := fmt.Sprintf(".*%s.*", v.VERSION)
r := regexp.MustCompile(expected)
if !r.MatchString(res.String()) {
t.Fatalf("Returned unexpected body:\ngot \n%v \nwant \n%s",
res, expected)
}
}
}

0 comments on commit 85cd1c4

Please sign in to comment.