From 01588fc866686b9c252a377583b59b8931328571 Mon Sep 17 00:00:00 2001 From: MohitKatare-protean Date: Thu, 3 Apr 2025 15:17:02 +0530 Subject: [PATCH] Added test case for the contextKeys --- pkg/model/model.go | 10 ++-- .../reqpreprocessor/reqpreprocessor_test.go | 53 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/pkg/model/model.go b/pkg/model/model.go index 53ff27e..8555046 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -40,11 +40,13 @@ const ( type ContextKey string -// MsgIDKey is the context key used to store and retrieve the message ID in a request context. -const MsgIDKey = ContextKey("message_id") +const ( + // MsgIDKey is the context key used to store and retrieve the message ID in a request context. + MsgIDKey = ContextKey("message_id") -// SubscriberIDKey is the context key used to store and retrieve the subscriber ID in a request context. -const SubscriberIDKey = ContextKey("subscriber_id") + // SubscriberIDKey is the context key used to store and retrieve the subscriber ID in a request context. + SubscriberIDKey = ContextKey("subscriber_id") +) // Role defines the type of participant in the network. type Role string diff --git a/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor_test.go b/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor_test.go index f35ee68..87d464f 100644 --- a/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor_test.go +++ b/pkg/plugin/implementation/reqpreprocessor/reqpreprocessor_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/beckn/beckn-onix/pkg/model" + "github.com/stretchr/testify/require" ) func TestNewUUIDSetterSuccessCases(t *testing.T) { @@ -178,3 +179,55 @@ func TestNewUUIDSetterErrorCases(t *testing.T) { }) } } + +// Mock configuration +var testConfig = &Config{ + Role: "bap", + ContextKeys: []string{"message_id"}, +} + +// Mock request payload +var testPayload = `{ + "context": { + "message_id": "test-msg-id", + "bap_id": "test-bap-id" + } +}` + +// Mock handler to capture processed request context +func captureContextHandler(t *testing.T, expectedMsgID string, expectedSubID string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Retrieve values from context + msgID, ok := r.Context().Value(model.MsgIDKey).(string) + require.True(t, ok, "message_id should be set") + require.Equal(t, expectedMsgID, msgID, "message_id should match") + + subID, ok := r.Context().Value(model.SubscriberIDKey).(string) + require.True(t, ok, "subscriber_id should be set") + require.Equal(t, expectedSubID, subID, "subscriber_id should match") + + w.WriteHeader(http.StatusOK) + }) +} + +// Test NewPreProcessor middleware +func TestNewPreProcessor(t *testing.T) { + preProcessor, err := NewPreProcessor(testConfig) + require.NoError(t, err) + + // Create test request + req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewBufferString(testPayload)) + req.Header.Set("Content-Type", "application/json") + + // Create response recorder + recorder := httptest.NewRecorder() + + // Wrap handler with middleware + handler := preProcessor(captureContextHandler(t, "test-msg-id", "test-bap-id")) + + // Serve request + handler.ServeHTTP(recorder, req) + + // Check response status + require.Equal(t, http.StatusOK, recorder.Code, "Middleware should process correctly") +}