Added test case for the contextKeys

This commit is contained in:
MohitKatare-protean
2025-04-03 15:17:02 +05:30
parent 0ee2010338
commit 01588fc866
2 changed files with 59 additions and 4 deletions

View File

@@ -40,11 +40,13 @@ const (
type ContextKey string type ContextKey string
// MsgIDKey is the context key used to store and retrieve the message ID in a request context. const (
const MsgIDKey = ContextKey("message_id") // 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. // SubscriberIDKey is the context key used to store and retrieve the subscriber ID in a request context.
const SubscriberIDKey = ContextKey("subscriber_id") SubscriberIDKey = ContextKey("subscriber_id")
)
// Role defines the type of participant in the network. // Role defines the type of participant in the network.
type Role string type Role string

View File

@@ -8,6 +8,7 @@ import (
"testing" "testing"
"github.com/beckn/beckn-onix/pkg/model" "github.com/beckn/beckn-onix/pkg/model"
"github.com/stretchr/testify/require"
) )
func TestNewUUIDSetterSuccessCases(t *testing.T) { 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")
}