fix: reqpreprocessor
This commit is contained in:
@@ -1,107 +0,0 @@
|
|||||||
package reqpreprocessor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
checkKeys []string
|
|
||||||
Role string
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
|
|
||||||
var data map[string]any
|
|
||||||
body, err := io.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(body, &data); err != nil {
|
|
||||||
http.Error(w, "Failed to decode request body", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
contextRaw := data[contextKey]
|
|
||||||
if contextRaw == nil {
|
|
||||||
http.Error(w, fmt.Sprintf("%s field not found.", contextKey), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
contextData, ok := contextRaw.(map[string]any)
|
|
||||||
if !ok {
|
|
||||||
http.Error(w, fmt.Sprintf("%s field is not a map.", contextKey), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var subID any
|
|
||||||
switch cfg.Role {
|
|
||||||
case "bap":
|
|
||||||
subID = contextData["bap_id"]
|
|
||||||
case "bpp":
|
|
||||||
subID = contextData["bpp_id"]
|
|
||||||
}
|
|
||||||
ctx := context.WithValue(r.Context(), subscriberIDKey, subID)
|
|
||||||
for _, key := range cfg.checkKeys {
|
|
||||||
value := uuid.NewString()
|
|
||||||
updatedValue := update(contextData, key, value)
|
|
||||||
ctx = context.WithValue(ctx, contextKeyType(key), updatedValue)
|
|
||||||
}
|
|
||||||
data[contextKey] = contextData
|
|
||||||
updatedBody, err := json.Marshal(data)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Failed to marshal updated JSON", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
for _, key := range cfg.checkKeys {
|
|
||||||
if key == "" {
|
|
||||||
return errors.New("checkKeys cannot contain empty strings")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
package reqpreprocessor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNewUUIDSetter(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
config *Config
|
|
||||||
requestBody map[string]any
|
|
||||||
expectedCode int
|
|
||||||
expectedKeys []string
|
|
||||||
role string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Valid keys, update missing keys with bap role",
|
|
||||||
config: &Config{
|
|
||||||
checkKeys: []string{"transaction_id", "message_id"},
|
|
||||||
Role: "bap",
|
|
||||||
},
|
|
||||||
requestBody: map[string]any{
|
|
||||||
"context": map[string]any{
|
|
||||||
"transaction_id": "",
|
|
||||||
"message_id": nil,
|
|
||||||
"bap_id": "bap-123",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedCode: http.StatusOK,
|
|
||||||
expectedKeys: []string{"transaction_id", "message_id", "bap_id"},
|
|
||||||
role: "bap",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Valid keys, do not update existing keys with bpp role",
|
|
||||||
config: &Config{
|
|
||||||
checkKeys: []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",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedCode: http.StatusOK,
|
|
||||||
expectedKeys: []string{"transaction_id", "message_id", "bpp_id"},
|
|
||||||
role: "bpp",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Missing context key",
|
|
||||||
config: &Config{
|
|
||||||
checkKeys: []string{"transaction_id"},
|
|
||||||
},
|
|
||||||
requestBody: map[string]any{
|
|
||||||
"otherKey": "value",
|
|
||||||
},
|
|
||||||
expectedCode: http.StatusBadRequest,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Invalid context type",
|
|
||||||
config: &Config{
|
|
||||||
checkKeys: []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 || len(tt.config.checkKeys) == 0 {
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("Expected an error, 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) {
|
|
||||||
ctx := r.Context()
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
if subID, ok := ctx.Value(subscriberIDKey).(string); ok {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
http.Error(w, "Subscriber ID not found", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
middleware(dummyHandler).ServeHTTP(rec, req)
|
|
||||||
if rec.Code != tt.expectedCode {
|
|
||||||
t.Errorf("Expected status code %d, but got %d", tt.expectedCode, rec.Code)
|
|
||||||
}
|
|
||||||
if rec.Code == http.StatusOK {
|
|
||||||
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"
|
|
||||||
}
|
|
||||||
|
|
||||||
if subID, ok := responseBody["subscriber_id"].(string); ok {
|
|
||||||
expectedSubID := tt.requestBody["context"].(map[string]any)[expectedSubIDKey]
|
|
||||||
if subID != expectedSubID {
|
|
||||||
t.Errorf("Expected subscriber_id %v, but got %v", expectedSubID, subID)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
t.Error("subscriber_id not found in response")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user