fix: resolved comments
This commit is contained in:
21
pkg/plugin/implementation/requestPreProcessor/cmd/plugin.go
Normal file
21
pkg/plugin/implementation/requestPreProcessor/cmd/plugin.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
requestpreprocessor "github.com/beckn/beckn-onix/pkg/plugin/implementation/requestPreProcessor"
|
||||
)
|
||||
|
||||
type provider struct{}
|
||||
|
||||
func (p provider) New(ctx context.Context, c map[string]string) (func(http.Handler) http.Handler, error) {
|
||||
config := &requestpreprocessor.Config{}
|
||||
if contextKeysStr, ok := c["ContextKeys"]; ok {
|
||||
config.ContextKeys = strings.Split(contextKeysStr, ",")
|
||||
}
|
||||
return requestpreprocessor.NewUUIDSetter(config)
|
||||
}
|
||||
|
||||
var Provider = provider{}
|
||||
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TODO: Will Split this into success and fail (two test cases)
|
||||
func TestProviderNew(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
config map[string]string
|
||||
expectedError bool
|
||||
expectedStatus int
|
||||
prepareRequest func(req *http.Request)
|
||||
}{
|
||||
{
|
||||
name: "No Config",
|
||||
config: map[string]string{},
|
||||
expectedError: true,
|
||||
expectedStatus: http.StatusOK,
|
||||
prepareRequest: func(req *http.Request) {
|
||||
// Add minimal required headers.
|
||||
req.Header.Set("context", "test-context")
|
||||
req.Header.Set("transaction_id", "test-transaction")
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "With Check Keys",
|
||||
config: map[string]string{
|
||||
"ContextKeys": "message_id,transaction_id",
|
||||
},
|
||||
expectedError: false,
|
||||
expectedStatus: http.StatusOK,
|
||||
prepareRequest: func(req *http.Request) {
|
||||
// Add headers matching the check keys.
|
||||
req.Header.Set("context", "test-context")
|
||||
req.Header.Set("transaction_id", "test-transaction")
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
requestBody := `{
|
||||
"context": {
|
||||
"transaction_id": "abc"
|
||||
}
|
||||
}`
|
||||
|
||||
p := provider{}
|
||||
middleware, err := p.New(context.Background(), tc.config)
|
||||
if tc.expectedError {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, middleware)
|
||||
|
||||
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("POST", "/", strings.NewReader(requestBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if tc.prepareRequest != nil {
|
||||
tc.prepareRequest(req)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
middlewaredHandler := middleware(testHandler)
|
||||
middlewaredHandler.ServeHTTP(w, req)
|
||||
assert.Equal(t, tc.expectedStatus, w.Code, "Unexpected response status")
|
||||
responseBody := w.Body.String()
|
||||
t.Logf("Response Body: %s", responseBody)
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
105
pkg/plugin/implementation/requestPreProcessor/reqpreprocessor.go
Normal file
105
pkg/plugin/implementation/requestPreProcessor/reqpreprocessor.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package requestpreprocessor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
ContextKeys []string
|
||||
Role string
|
||||
}
|
||||
|
||||
type becknRequest struct {
|
||||
Context map[string]any `json:"context"`
|
||||
}
|
||||
|
||||
type contextKeyType string
|
||||
|
||||
const contextKey = "context"
|
||||
const subscriberIDKey contextKeyType = "subscriber_id"
|
||||
|
||||
func NewUUIDSetter(cfg *Config) (func(http.Handler) http.Handler, error) {
|
||||
if err := validateConfig(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var req becknRequest
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
http.Error(w, "Failed to decode request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if req.Context == nil {
|
||||
http.Error(w, fmt.Sprintf("%s field not found.", contextKey), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var subID any
|
||||
switch cfg.Role {
|
||||
case "bap":
|
||||
subID = req.Context["bap_id"]
|
||||
case "bpp":
|
||||
subID = req.Context["bpp_id"]
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), subscriberIDKey, subID)
|
||||
for _, key := range cfg.ContextKeys {
|
||||
value := uuid.NewString()
|
||||
updatedValue := update(req.Context, key, value)
|
||||
ctx = context.WithValue(ctx, contextKeyType(key), updatedValue)
|
||||
}
|
||||
reqData := map[string]any{"context": req.Context}
|
||||
updatedBody, _ := json.Marshal(reqData)
|
||||
r.Body = io.NopCloser(bytes.NewBuffer(updatedBody))
|
||||
r.ContentLength = int64(len(updatedBody))
|
||||
r = r.WithContext(ctx)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}, nil
|
||||
}
|
||||
|
||||
func update(wrapper map[string]any, key string, value any) any {
|
||||
field, exists := wrapper[key]
|
||||
if !exists || isEmpty(field) {
|
||||
wrapper[key] = value
|
||||
return value
|
||||
}
|
||||
|
||||
return field
|
||||
}
|
||||
func isEmpty(v any) bool {
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
return v == ""
|
||||
case nil:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func validateConfig(cfg *Config) error {
|
||||
if cfg == nil {
|
||||
return errors.New("config cannot be nil")
|
||||
}
|
||||
|
||||
// Check if ContextKeys is empty.
|
||||
if len(cfg.ContextKeys) == 0 {
|
||||
return errors.New("ContextKeys cannot be empty")
|
||||
}
|
||||
|
||||
// Validate that ContextKeys does not contain empty strings.
|
||||
for _, key := range cfg.ContextKeys {
|
||||
if key == "" {
|
||||
return errors.New("ContextKeys cannot contain empty strings")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
package requestpreprocessor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewUUIDSetterSuccessCases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
config *Config
|
||||
requestBody map[string]any
|
||||
expectedKeys []string
|
||||
role string
|
||||
}{
|
||||
{
|
||||
name: "Valid keys, update missing keys with bap role",
|
||||
config: &Config{
|
||||
ContextKeys: []string{"transaction_id", "message_id"},
|
||||
Role: "bap",
|
||||
},
|
||||
requestBody: map[string]any{
|
||||
"context": map[string]any{
|
||||
"transaction_id": "",
|
||||
"message_id": nil,
|
||||
"bap_id": "bap-123",
|
||||
},
|
||||
},
|
||||
expectedKeys: []string{"transaction_id", "message_id", "bap_id"},
|
||||
role: "bap",
|
||||
},
|
||||
{
|
||||
name: "Valid keys, do not update existing keys with bpp role",
|
||||
config: &Config{
|
||||
ContextKeys: []string{"transaction_id", "message_id"},
|
||||
Role: "bpp",
|
||||
},
|
||||
requestBody: map[string]any{
|
||||
"context": map[string]any{
|
||||
"transaction_id": "existing-transaction",
|
||||
"message_id": "existing-message",
|
||||
"bpp_id": "bpp-456",
|
||||
},
|
||||
},
|
||||
expectedKeys: []string{"transaction_id", "message_id", "bpp_id"},
|
||||
role: "bpp",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
middleware, err := NewUUIDSetter(tt.config)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error while creating middleware: %v", err)
|
||||
}
|
||||
|
||||
bodyBytes, _ := json.Marshal(tt.requestBody)
|
||||
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewReader(bodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
dummyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
subID, ok := ctx.Value(subscriberIDKey).(string)
|
||||
if !ok {
|
||||
http.Error(w, "Subscriber ID not found", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response := map[string]any{"subscriber_id": subID}
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
|
||||
middleware(dummyHandler).ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("Expected status code 200, but got %d", rec.Code)
|
||||
return
|
||||
}
|
||||
|
||||
var responseBody map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &responseBody); err != nil {
|
||||
t.Fatal("Failed to unmarshal response body:", err)
|
||||
}
|
||||
|
||||
expectedSubIDKey := "bap_id"
|
||||
if tt.role == "bpp" {
|
||||
expectedSubIDKey = "bpp_id"
|
||||
}
|
||||
|
||||
subID, ok := responseBody["subscriber_id"].(string)
|
||||
if !ok {
|
||||
t.Error("subscriber_id not found in response")
|
||||
return
|
||||
}
|
||||
|
||||
expectedSubID := tt.requestBody["context"].(map[string]any)[expectedSubIDKey]
|
||||
if subID != expectedSubID {
|
||||
t.Errorf("Expected subscriber_id %v, but got %v", expectedSubID, subID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewUUIDSetterErrorCases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
config *Config
|
||||
requestBody map[string]any
|
||||
expectedCode int
|
||||
}{
|
||||
{
|
||||
name: "Missing context key",
|
||||
config: &Config{
|
||||
ContextKeys: []string{"transaction_id"},
|
||||
},
|
||||
requestBody: map[string]any{
|
||||
"otherKey": "value",
|
||||
},
|
||||
expectedCode: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "Invalid context type",
|
||||
config: &Config{
|
||||
ContextKeys: []string{"transaction_id"},
|
||||
},
|
||||
requestBody: map[string]any{
|
||||
"context": "not-a-map",
|
||||
},
|
||||
expectedCode: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "Nil config",
|
||||
config: nil,
|
||||
requestBody: map[string]any{},
|
||||
expectedCode: http.StatusInternalServerError,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
middleware, err := NewUUIDSetter(tt.config)
|
||||
if tt.config == nil {
|
||||
if err == nil {
|
||||
t.Error("Expected an error for nil config, but got none")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error while creating middleware: %v", err)
|
||||
}
|
||||
|
||||
bodyBytes, _ := json.Marshal(tt.requestBody)
|
||||
req := httptest.NewRequest(http.MethodPost, "/test", bytes.NewReader(bodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
dummyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
middleware(dummyHandler).ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != tt.expectedCode {
|
||||
t.Errorf("Expected status code %d, but got %d", tt.expectedCode, rec.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user