From d1e8716e15d8a6bbd7c238d461067db97a509b5d Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Fri, 4 Apr 2025 15:01:15 +0530 Subject: [PATCH 1/2] ft: module_name wrapper middleware --- config/onix-bap/adapter.yaml | 1 + config/onix-bpp/adapter.yaml | 1 + config/onix/adapter.yaml | 1 + core/module/module.go | 8 ++++++++ core/module/module_test.go | 21 +++++++++++++++++++++ 5 files changed, 32 insertions(+) diff --git a/config/onix-bap/adapter.yaml b/config/onix-bap/adapter.yaml index a95c53f..878b8d9 100644 --- a/config/onix-bap/adapter.yaml +++ b/config/onix-bap/adapter.yaml @@ -7,6 +7,7 @@ log: - transaction_id - message_id - subscriber_id + - module_name http: port: 8080 timeout: diff --git a/config/onix-bpp/adapter.yaml b/config/onix-bpp/adapter.yaml index 8994980..ddee6a5 100644 --- a/config/onix-bpp/adapter.yaml +++ b/config/onix-bpp/adapter.yaml @@ -7,6 +7,7 @@ log: - transaction_id - message_id - subscriber_id + - module_name http: port: 8080 timeout: diff --git a/config/onix/adapter.yaml b/config/onix/adapter.yaml index a626c4e..1aee391 100644 --- a/config/onix/adapter.yaml +++ b/config/onix/adapter.yaml @@ -7,6 +7,7 @@ log: - transaction_id - message_id - subscriber_id + - module_name http: port: 8080 timeout: diff --git a/core/module/module.go b/core/module/module.go index 3b0fcef..2926995 100644 --- a/core/module/module.go +++ b/core/module/module.go @@ -44,6 +44,7 @@ func Register(ctx context.Context, mCfgs []Config, mux *http.ServeMux, mgr handl return fmt.Errorf("failed to add middleware: %w", err) } + h = moduleCtxMiddleware(c.Name, h) log.Debugf(ctx, "Registering handler %s, of type %s @ %s", c.Name, c.Handler.Type, c.Path) mux.Handle(c.Path, h) } @@ -71,3 +72,10 @@ func addMiddleware(ctx context.Context, mgr handler.PluginManager, handler http. log.Debugf(ctx, "Middleware chain setup completed") return handler, nil } + +func moduleCtxMiddleware(moduleName string, next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := context.WithValue(r.Context(), "module_name", moduleName) + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} diff --git a/core/module/module_test.go b/core/module/module_test.go index 8f9016c..fc191af 100644 --- a/core/module/module_test.go +++ b/core/module/module_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "net/http" + "net/http/httptest" "testing" "github.com/beckn/beckn-onix/core/module/handler" @@ -97,6 +98,26 @@ func TestRegisterSuccess(t *testing.T) { if err != nil { t.Errorf("unexpected error: %v", err) } + + // Create a request and a response recorder + req := httptest.NewRequest(http.MethodGet, "/test", nil) + rec := httptest.NewRecorder() + + // Create a handler that extracts context + var capturedModuleName any + testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + capturedModuleName = r.Context().Value("module_name") + w.WriteHeader(http.StatusOK) + }) + + wrappedHandler := moduleCtxMiddleware("test-module", testHandler) + wrappedHandler.ServeHTTP(rec, req) + + // Now verify if module name exists in context + if capturedModuleName != "test-module" { + t.Errorf("expected module_name in context to be 'test-module', got %v", capturedModuleName) + } + } // TestRegisterFailure tests scenarios where the handler registration should fail. From 2329efd5628ceaeb81f2171aa8c5cda338a76d17 Mon Sep 17 00:00:00 2001 From: "mayur.popli" Date: Fri, 4 Apr 2025 15:39:05 +0530 Subject: [PATCH 2/2] fix: module id and linting issues --- config/onix-bap/adapter.yaml | 2 +- config/onix-bpp/adapter.yaml | 2 +- config/onix/adapter.yaml | 2 +- core/module/module.go | 3 ++- core/module/module_test.go | 5 +++-- pkg/model/error_test.go | 2 +- pkg/model/model.go | 4 ++++ 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/config/onix-bap/adapter.yaml b/config/onix-bap/adapter.yaml index 878b8d9..dda19fc 100644 --- a/config/onix-bap/adapter.yaml +++ b/config/onix-bap/adapter.yaml @@ -7,7 +7,7 @@ log: - transaction_id - message_id - subscriber_id - - module_name + - module_id http: port: 8080 timeout: diff --git a/config/onix-bpp/adapter.yaml b/config/onix-bpp/adapter.yaml index ddee6a5..aa3d242 100644 --- a/config/onix-bpp/adapter.yaml +++ b/config/onix-bpp/adapter.yaml @@ -7,7 +7,7 @@ log: - transaction_id - message_id - subscriber_id - - module_name + - module_id http: port: 8080 timeout: diff --git a/config/onix/adapter.yaml b/config/onix/adapter.yaml index 1aee391..e3a785b 100644 --- a/config/onix/adapter.yaml +++ b/config/onix/adapter.yaml @@ -7,7 +7,7 @@ log: - transaction_id - message_id - subscriber_id - - module_name + - module_id http: port: 8080 timeout: diff --git a/core/module/module.go b/core/module/module.go index 2926995..4641fcb 100644 --- a/core/module/module.go +++ b/core/module/module.go @@ -7,6 +7,7 @@ import ( "github.com/beckn/beckn-onix/core/module/handler" "github.com/beckn/beckn-onix/pkg/log" + "github.com/beckn/beckn-onix/pkg/model" ) // Config represents the configuration for a module. @@ -75,7 +76,7 @@ func addMiddleware(ctx context.Context, mgr handler.PluginManager, handler http. func moduleCtxMiddleware(moduleName string, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := context.WithValue(r.Context(), "module_name", moduleName) + ctx := context.WithValue(r.Context(), model.ContextKeyModuleId, moduleName) next.ServeHTTP(w, r.WithContext(ctx)) }) } diff --git a/core/module/module_test.go b/core/module/module_test.go index fc191af..6091fc1 100644 --- a/core/module/module_test.go +++ b/core/module/module_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/beckn/beckn-onix/core/module/handler" + "github.com/beckn/beckn-onix/pkg/model" "github.com/beckn/beckn-onix/pkg/plugin" "github.com/beckn/beckn-onix/pkg/plugin/definition" ) @@ -106,7 +107,7 @@ func TestRegisterSuccess(t *testing.T) { // Create a handler that extracts context var capturedModuleName any testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - capturedModuleName = r.Context().Value("module_name") + capturedModuleName = r.Context().Value(model.ContextKeyModuleId) w.WriteHeader(http.StatusOK) }) @@ -115,7 +116,7 @@ func TestRegisterSuccess(t *testing.T) { // Now verify if module name exists in context if capturedModuleName != "test-module" { - t.Errorf("expected module_name in context to be 'test-module', got %v", capturedModuleName) + t.Errorf("expected module_id in context to be 'test-module', got %v", capturedModuleName) } } diff --git a/pkg/model/error_test.go b/pkg/model/error_test.go index ee295e6..7b9fd95 100644 --- a/pkg/model/error_test.go +++ b/pkg/model/error_test.go @@ -3,8 +3,8 @@ package model import ( "errors" "fmt" - "testing" "net/http" + "testing" "github.com/stretchr/testify/assert" "gopkg.in/yaml.v2" diff --git a/pkg/model/model.go b/pkg/model/model.go index 621bdeb..536a9ad 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -16,6 +16,10 @@ type Subscriber struct { Domain string `json:"domain"` } +type ContextKey string + +const ContextKeyModuleId ContextKey = "module_id" + // Subscription represents subscription details of a network participant. type Subscription struct { Subscriber `json:",inline"`