resolved reqpreprocessing review comments

1. added logs for contextKey parsing and unmarshaling yml file.
2. resolved router test case issue.
2. resolved logger test case issues.
This commit is contained in:
MohitKatare-protean
2025-04-06 17:09:59 +05:30
parent 278e217c64
commit ec62a3242b
7 changed files with 176 additions and 70 deletions

View File

@@ -15,7 +15,8 @@ import (
// Config represents the configuration for the request preprocessor middleware.
type Config struct {
Role string
Role string
ContextKeys []string
}
const contextKey = "context"
@@ -57,7 +58,12 @@ func NewPreProcessor(cfg *Config) (func(http.Handler) http.Handler, error) {
log.Debugf(ctx, "adding subscriberId to request:%s, %v", model.ContextKeySubscriberID, subID)
ctx = context.WithValue(ctx, model.ContextKeySubscriberID, subID)
}
for _, key := range cfg.ContextKeys {
ctxKey, _ := model.ParseContextKey(key)
if v, ok := reqContext[key]; ok {
ctx = context.WithValue(ctx, ctxKey, v)
}
}
r.Body = io.NopCloser(bytes.NewBuffer(body))
r.ContentLength = int64(len(body))
r = r.WithContext(ctx)
@@ -74,5 +80,11 @@ func validateConfig(cfg *Config) error {
if cfg.Role != "bap" && cfg.Role != "bpp" {
return errors.New("role must be either 'bap' or 'bpp'")
}
for _, key := range cfg.ContextKeys {
if _, err := model.ParseContextKey(key); err != nil {
return err
}
}
return nil
}

View File

@@ -296,7 +296,7 @@ func TestValidateRulesFailure(t *testing.T) {
Endpoints: []string{"search"},
},
},
wantErr: `invalid URI htp:// invalid-url.com in request body for url: invalid URL - htp:// invalid-url.com: parse "htp:// invalid-url.com": invalid character " " in host name`,
wantErr: `invalid URL - htp:// invalid-url.com: parse "htp:// invalid-url.com": invalid character " " in host name`,
},
{
name: "Missing topic_id for targetType: publisher",
@@ -321,12 +321,12 @@ func TestValidateRulesFailure(t *testing.T) {
Version: "1.0.0",
TargetType: "bpp",
Target: target{
URL: "htp://invalid-url.com", // Invalid URL
URL: "htp:// invalid-url.com", // Invalid URL
},
Endpoints: []string{"search"},
},
},
wantErr: "invalid URI htp://invalid-url.com in request body for bpp: URL 'htp://invalid-url.com' must use https scheme",
wantErr: `invalid URL - htp:// invalid-url.com defined in routing config for target type bpp: parse "htp:// invalid-url.com": invalid character " " in host name`,
},
{
name: "Invalid URL for BAP targetType",
@@ -336,12 +336,12 @@ func TestValidateRulesFailure(t *testing.T) {
Version: "1.0.0",
TargetType: "bap",
Target: target{
URL: "http://[invalid].com", // Invalid host
URL: "http:// [invalid].com", // Invalid host
},
Endpoints: []string{"search"},
},
},
wantErr: "invalid URI http://[invalid].com in request body for bap",
wantErr: `invalid URL - http:// [invalid].com defined in routing config for target type bap: parse "http:// [invalid].com": invalid character " " in host name`,
},
}
@@ -464,8 +464,8 @@ func TestRouteFailure(t *testing.T) {
name: "Invalid bpp_uri format in request",
configFile: "bap_caller.yaml",
url: "https://example.com/v1/ondc/select",
body: `{"context": {"domain": "ONDC:TRV10", "version": "2.0.0", "bpp_uri": "htp://invalid-url"}}`, // Invalid scheme (htp instead of http)
wantErr: "invalid BPP URI - htp://invalid-url in request body for select: URL 'htp://invalid-url' must use https scheme",
body: `{"context": {"domain": "ONDC:TRV10", "version": "2.0.0", "bpp_uri": "htp:// invalid-url"}}`, // Invalid scheme (htp instead of http)
wantErr: `invalid BPP URI - htp:// invalid-url in request body for select: parse "htp:// invalid-url": invalid character " " in host name`,
},
}