resolved merge conflict
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/beckn/beckn-onix/pkg/model"
|
"github.com/beckn/beckn-onix/pkg/model"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ToDo Separate Middleware creation and execution.
|
// ToDo Separate Middleware creation and execution.
|
||||||
@@ -74,11 +73,11 @@ func TestNewPreProcessorSuccessCases(t *testing.T) {
|
|||||||
|
|
||||||
dummyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
dummyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
gotSubID = ctx.Value(subscriberIDKey)
|
gotSubID = ctx.Value(model.SubscriberIDKey)
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
// Verify subscriber ID
|
// Verify subscriber ID
|
||||||
subID := ctx.Value(subscriberIDKey)
|
subID := ctx.Value(model.SubscriberIDKey)
|
||||||
if subID == nil {
|
if subID == nil {
|
||||||
t.Errorf("Expected subscriber ID but got none %s", ctx)
|
t.Errorf("Expected subscriber ID but got none %s", ctx)
|
||||||
return
|
return
|
||||||
@@ -237,7 +236,6 @@ func TestNewPreProcessorErrorCases(t *testing.T) {
|
|||||||
// Mock configuration
|
// Mock configuration
|
||||||
var testConfig = &Config{
|
var testConfig = &Config{
|
||||||
Role: "bap",
|
Role: "bap",
|
||||||
ContextKeys: []string{"message_id"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mock request payload
|
// Mock request payload
|
||||||
@@ -249,16 +247,15 @@ var testPayload = `{
|
|||||||
}`
|
}`
|
||||||
|
|
||||||
// Mock handler to capture processed request context
|
// Mock handler to capture processed request context
|
||||||
func captureContextHandler(t *testing.T, expectedMsgID string, expectedSubID string) http.Handler {
|
func captureContextHandler(t *testing.T, expectedSubID string) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Retrieve values from context
|
// Retrieve subscriber_id 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)
|
subID, ok := r.Context().Value(model.SubscriberIDKey).(string)
|
||||||
require.True(t, ok, "subscriber_id should be set")
|
if !ok {
|
||||||
require.Equal(t, expectedSubID, subID, "subscriber_id should match")
|
t.Error("subscriber_id should be set in context")
|
||||||
|
} else if subID != expectedSubID {
|
||||||
|
t.Errorf("expected subscriber_id %s, got %s", expectedSubID, subID)
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
@@ -266,8 +263,20 @@ func captureContextHandler(t *testing.T, expectedMsgID string, expectedSubID str
|
|||||||
|
|
||||||
// Test NewPreProcessor middleware
|
// Test NewPreProcessor middleware
|
||||||
func TestNewPreProcessor(t *testing.T) {
|
func TestNewPreProcessor(t *testing.T) {
|
||||||
|
testConfig := &Config{
|
||||||
|
Role: "bap",
|
||||||
|
}
|
||||||
|
|
||||||
|
testPayload := `{
|
||||||
|
"context": {
|
||||||
|
"bap_id": "test-bap-id"
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
preProcessor, err := NewPreProcessor(testConfig)
|
preProcessor, err := NewPreProcessor(testConfig)
|
||||||
require.NoError(t, err)
|
if err != nil {
|
||||||
|
t.Fatalf("expected no error, got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Create test request
|
// Create test request
|
||||||
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewBufferString(testPayload))
|
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewBufferString(testPayload))
|
||||||
@@ -277,11 +286,13 @@ func TestNewPreProcessor(t *testing.T) {
|
|||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
|
|
||||||
// Wrap handler with middleware
|
// Wrap handler with middleware
|
||||||
handler := preProcessor(captureContextHandler(t, "test-msg-id", "test-bap-id"))
|
handler := preProcessor(captureContextHandler(t, "test-bap-id"))
|
||||||
|
|
||||||
// Serve request
|
// Serve request
|
||||||
handler.ServeHTTP(recorder, req)
|
handler.ServeHTTP(recorder, req)
|
||||||
|
|
||||||
// Check response status
|
// Check response status
|
||||||
require.Equal(t, http.StatusOK, recorder.Code, "Middleware should process correctly")
|
if recorder.Code != http.StatusOK {
|
||||||
|
t.Errorf("expected status %d, got %d", http.StatusOK, recorder.Code)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user