Removed unnecessary comments and whitespaces

Signed-off-by: Prashant Dwivedi <prashantdwivedi194@gmail.com>
This commit is contained in:
Prashant Dwivedi
2024-06-23 21:59:50 +05:30
parent 2687a13c75
commit 85cd1c46d4
10 changed files with 13 additions and 69 deletions

View File

@@ -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()
})
@@ -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()
@@ -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)
}
}
}

View File

@@ -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()
@@ -35,7 +33,6 @@ func TestGrpcEnv(t *testing.T) {
}
}()
// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
@@ -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()) {

View File

@@ -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()
@@ -36,7 +34,6 @@ func TestGrpcHeader(t *testing.T) {
}
}()
// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
@@ -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()) {

View File

@@ -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()
})
@@ -36,7 +34,6 @@ func TestGrpcInfo(t *testing.T) {
}
}()
// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
@@ -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()) {

View File

@@ -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,
@@ -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")
}

View File

@@ -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()
@@ -37,7 +35,6 @@ func TestGrpcStatusError(t *testing.T) {
}
}()
// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
@@ -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()
@@ -92,7 +87,6 @@ func TestGrpcStatusOk(t *testing.T) {
}
}()
// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}

View File

@@ -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"
@@ -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{}

View File

@@ -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()
@@ -35,7 +33,6 @@ func TestGrpcToken(t *testing.T) {
}
}()
// - Test
dialer := func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}
@@ -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)
}

View File

@@ -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
}

View File

@@ -17,14 +17,11 @@ 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()
@@ -32,19 +29,18 @@ func TestGrpcVersion(t *testing.T) {
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()
@@ -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)
}
}
}