From 3ca946c720e43d5e3817669ff730980dbc4aca36 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Wed, 28 Aug 2024 12:34:37 +0200 Subject: [PATCH] OZG-6514 Refactoring --- internal/server/grpc_router.go | 4 ++-- internal/server/handler_test.go | 7 +++---- internal/server/middleware.go | 20 ++------------------ internal/server/middleware_test.go | 3 +-- 4 files changed, 8 insertions(+), 26 deletions(-) diff --git a/internal/server/grpc_router.go b/internal/server/grpc_router.go index a0437cd..2d8609a 100644 --- a/internal/server/grpc_router.go +++ b/internal/server/grpc_router.go @@ -63,9 +63,9 @@ func (s *collaborationRouter) FindVorgang(ctx context.Context, in *pb.GrpcFindVo } defer connCleanUp() - updatedRequest := &pb.GrpcFindVorgangRequest{VorgangId: collaborationInfo.Attribute, SamlToken: in.SamlToken} + in.VorgangId = collaborationInfo.Attribute - return client.FindVorgang(context.Background(), updatedRequest) + return client.FindVorgang(context.Background(), in) } type CollaborationInfo struct { diff --git a/internal/server/handler_test.go b/internal/server/handler_test.go index 47b2250..88ae727 100644 --- a/internal/server/handler_test.go +++ b/internal/server/handler_test.go @@ -28,7 +28,6 @@ package server import ( "bytes" "fachstellen-proxy/internal/mock" - "fmt" "github.com/stretchr/testify/assert" "net/http" "testing" @@ -48,7 +47,7 @@ func TestRegisterFachstelleRegistrationEndpoints(t *testing.T) { SetUpHttpGateway() jsonData := []byte(`{"fachstelle": {"mukId": "testMukId"}}`) - resp, err := http.Post(fmt.Sprintf("http://localhost:8080%v", FachstelleRegistrationUrlPath), "application/json", bytes.NewBuffer(jsonData)) + resp, err := http.Post("http://localhost:8080/api/fachstellen", "application/json", bytes.NewBuffer(jsonData)) assert.NoError(t, err) assert.Equal(t, http.StatusOK, resp.StatusCode) @@ -59,7 +58,7 @@ func TestRegisterCollaborationEndpoints(t *testing.T) { SetUpHttpGateway() SetUpCollaborationRouter() - resp, err := http.Get(fmt.Sprintf("http://localhost:8080%v/bG9jYWxob3N0L3Rlc3RWb3JnYW5nSWQ=/testSamlToken", VorgangUrlBasePath)) + resp, err := http.Get("http://localhost:8080/api/vorgang/bG9jYWxob3N0L3Rlc3RWb3JnYW5nSWQ=/testSamlToken") assert.NoError(t, err) assert.Equal(t, http.StatusOK, resp.StatusCode) @@ -70,7 +69,7 @@ func TestErrorHandler(t *testing.T) { mock.SetUpGrpcServer() SetUpHttpGateway() - resp, err := http.Post(fmt.Sprintf("http://localhost:8080%v", FachstelleRegistrationUrlPath), "application/json", bytes.NewBuffer(jsonData)) + resp, err := http.Post("http://localhost:8080/api/fachstellen", "application/json", bytes.NewBuffer(jsonData)) assert.NoError(t, err) assert.Equal(t, expectedStatusCode, resp.StatusCode) diff --git a/internal/server/middleware.go b/internal/server/middleware.go index fc33db1..4171075 100644 --- a/internal/server/middleware.go +++ b/internal/server/middleware.go @@ -30,13 +30,9 @@ import ( "fmt" "io" "net/http" - "strings" ) -const ( - FachstelleRegistrationUrlPath = "/api/fachstellen" - VorgangUrlBasePath = "/api/vorgang" -) +const HomeUrlPath = "/" type logResponseWriter struct { http.ResponseWriter @@ -50,7 +46,7 @@ func (rsp *logResponseWriter) WriteHeader(statusCode int) { func RequestLoggingMiddleware(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if !isRequestToBeLogged(r.URL.Path) { + if r.URL.Path == HomeUrlPath { h.ServeHTTP(w, r) return } @@ -75,15 +71,3 @@ func RequestLoggingMiddleware(h http.Handler) http.Handler { } }) } - -func isRequestToBeLogged(path string) bool { - requestPathsToBeLogged := []string{FachstelleRegistrationUrlPath, VorgangUrlBasePath} - - for _, rp := range requestPathsToBeLogged { - if strings.Contains(path, rp) { - return true - } - } - - return false -} diff --git a/internal/server/middleware_test.go b/internal/server/middleware_test.go index 798a7b3..90806e9 100644 --- a/internal/server/middleware_test.go +++ b/internal/server/middleware_test.go @@ -28,7 +28,6 @@ package server import ( "bytes" "fachstellen-proxy/internal/mock" - "fmt" "github.com/stretchr/testify/assert" "net/http" "testing" @@ -49,7 +48,7 @@ func TestRequestLoggingMiddleware(t *testing.T) { }() jsonData := []byte(`{"fachstelle": {"mukId": "testMukId"}}`) - http.Post(fmt.Sprintf("http://localhost:8080%v", FachstelleRegistrationUrlPath), "application/json", bytes.NewBuffer(jsonData)) + http.Post("http://localhost:8080/api/fachstellen", "application/json", bytes.NewBuffer(jsonData)) logOutput := buf.String() assert.Contains(t, logOutput, "received POST request for /api/fachstellen with body: {\"fachstelle\": {\"mukId\": \"testMukId\"}}") -- GitLab