Added: Bug Fixes

This commit is contained in:
shreyvishal
2025-06-25 16:30:47 +05:30
parent 02c69b98b6
commit 7a0a78d43d
12 changed files with 405 additions and 104 deletions

View File

@@ -115,6 +115,7 @@ func (r *Router) loadRules(configPath string) error {
if err != nil {
return fmt.Errorf("invalid URL in rule: %w", err)
}
parsedURL.Path = joinPath(parsedURL, endpoint)
route = &model.Route{
TargetType: rule.TargetType,
URL: parsedURL,
@@ -126,6 +127,7 @@ func (r *Router) loadRules(configPath string) error {
if err != nil {
return fmt.Errorf("invalid URL in rule: %w", err)
}
parsedURL.Path = joinPath(parsedURL, endpoint)
}
route = &model.Route{
TargetType: rule.TargetType,
@@ -227,24 +229,23 @@ func handleProtocolMapping(route *model.Route, npURI, endpoint string) (*model.R
}
return &model.Route{
TargetType: targetTypeURL,
URL: &url.URL{
Scheme: route.URL.Scheme,
Host: route.URL.Host,
Path: path.Join(route.URL.Path, endpoint),
},
URL: route.URL,
}, nil
}
targetURL, err := url.Parse(target)
if err != nil {
return nil, fmt.Errorf("invalid %s URI - %s in request body for %s: %w", strings.ToUpper(route.TargetType), target, endpoint, err)
}
targetURL.Path = joinPath(targetURL, endpoint)
return &model.Route{
TargetType: targetTypeURL,
URL: &url.URL{
Scheme: targetURL.Scheme,
Host: targetURL.Host,
Path: path.Join(targetURL.Path, endpoint),
},
URL: targetURL,
}, nil
}
func joinPath(u *url.URL, endpoint string) string {
if u.Path == "" {
u.Path = "/"
}
return path.Join(u.Path, endpoint)
}

View File

@@ -6,8 +6,11 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/beckn/beckn-onix/pkg/model"
)
//go:embed testData/*
@@ -47,76 +50,141 @@ func setupRouter(t *testing.T, configFile string) (*Router, func() error, string
func TestNew(t *testing.T) {
ctx := context.Background()
// List of YAML files in the testData directory
yamlFiles := []string{
"bap_caller.yaml",
"bap_receiver.yaml",
"bpp_caller.yaml",
"bpp_receiver.yaml",
validConfigFile := "bap_caller.yaml"
rulesFilePath := setupTestConfig(t, validConfigFile)
defer os.RemoveAll(filepath.Dir(rulesFilePath))
config := &Config{
RoutingConfig: rulesFilePath,
}
for _, yamlFile := range yamlFiles {
t.Run(yamlFile, func(t *testing.T) {
rulesFilePath := setupTestConfig(t, yamlFile)
defer os.RemoveAll(filepath.Dir(rulesFilePath))
router, _, err := New(ctx, config)
if err != nil {
t.Errorf("New(%v) = %v, want nil error", config, err)
return
}
if router == nil {
t.Errorf("New(%v) = nil router, want non-nil", config)
}
if len(router.rules) == 0 {
t.Error("Expected router to have loaded rules, but rules map is empty")
}
}
// Define test cases
tests := []struct {
name string
config *Config
wantErr string
}{
{
name: "Valid configuration",
config: &Config{
RoutingConfig: rulesFilePath,
},
wantErr: "",
},
{
name: "Empty config",
config: nil,
wantErr: "config cannot be nil",
},
{
name: "Empty routing config path",
config: &Config{
RoutingConfig: "",
},
wantErr: "routingConfig path is empty",
},
{
name: "Routing config file does not exist",
config: &Config{
RoutingConfig: "/nonexistent/path/to/rules.yaml",
},
wantErr: "error reading config file",
},
// TestNewErrors tests the New function for failure cases.
func TestNewErrors(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
config *Config
wantErr string
}{
{
name: "Empty config",
config: nil,
wantErr: "config cannot be nil",
},
{
name: "Empty routing config path",
config: &Config{
RoutingConfig: "",
},
wantErr: "routingConfig path is empty",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
router, _, err := New(ctx, tt.config)
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("New(%v) = %v, want error containing %q", tt.config, err, tt.wantErr)
}
if router != nil {
t.Errorf("New(%v) = %v, want nil router on error", tt.config, router)
}
})
}
}
// TestLoadRules tests the loadRules function for successful loading and map construction.
func TestLoadRules(t *testing.T) {
router := &Router{
rules: make(map[string]map[string]map[string]*model.Route),
}
rulesFilePath := setupTestConfig(t, "valid_all_routes.yaml")
defer os.RemoveAll(filepath.Dir(rulesFilePath))
err := router.loadRules(rulesFilePath)
if err != nil {
t.Fatalf("loadRules() err = %v, want nil", err)
}
// Expected router.rules map structure based on the yaml.
expectedRules := map[string]map[string]map[string]*model.Route{
"ONDC:TRV10": {
"2.0.0": {
"search": {TargetType: targetTypeURL, URL: parseURL(t, "https://mock_gateway.com/v2/ondc/search")},
"init": {TargetType: targetTypeBAP, URL: parseURL(t, "https://mock_bpp.com/v2/ondc/init")},
"select": {TargetType: targetTypeBAP, URL: parseURL(t, "https://mock_bpp.com/v2/ondc/select")},
"on_search": {TargetType: targetTypeBAP, URL: parseURL(t, "https://mock_bap_gateway.com/v2/ondc/on_search")},
"confirm": {TargetType: targetTypePublisher, PublisherID: "beckn_onix_topic", URL: nil},
},
},
}
if !reflect.DeepEqual(router.rules, expectedRules) {
t.Errorf("Loaded rules mismatch.\nGot:\n%#v\nWant:\n%#v", router.rules, expectedRules)
}
}
// mustParseURL is a helper for TestLoadRules to parse URLs.
func parseURL(t *testing.T, rawURL string) *url.URL {
u, err := url.Parse(rawURL)
if err != nil {
t.Fatalf("Failed to parse URL %s: %v", rawURL, err)
}
return u
}
// TestLoadRulesErrors tests the loadRules function for various error cases.
func TestLoadRulesErrors(t *testing.T) {
router := &Router{
rules: make(map[string]map[string]map[string]*model.Route),
}
tests := []struct {
name string
configPath string
wantErr string
}{
{
name: "Empty routing config path",
configPath: "",
wantErr: "routingConfig path is empty",
},
{
name: "Routing config file does not exist",
configPath: "/nonexistent/path/to/rules.yaml",
wantErr: "error reading config file",
},
{
name: "Invalid YAML (Unmarshal error)",
configPath: setupTestConfig(t, "invalid_yaml.yaml"),
wantErr: "error parsing YAML",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !strings.Contains(tt.configPath, "/nonexistent/") && tt.configPath != "" {
defer os.RemoveAll(filepath.Dir(tt.configPath))
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
router, _, err := New(ctx, tt.config)
// Check for expected error
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("New(%v) = %v, want error containing %q", tt.config, err, tt.wantErr)
}
return
}
// Ensure no error occurred
if err != nil {
t.Errorf("New(%v) = %v, want nil error", tt.config, err)
return
}
// Ensure the router and close function are not nil
if router == nil {
t.Errorf("New(%v, %v) = nil router, want non-nil", ctx, tt.config)
}
})
err := router.loadRules(tt.configPath)
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("loadRules(%q) = %v, want error containing %q", tt.configPath, err, tt.wantErr)
}
})
}
@@ -483,4 +551,4 @@ func TestRouteFailure(t *testing.T) {
}
})
}
}
}

View File

@@ -0,0 +1 @@
key: value: invalid

View File

@@ -0,0 +1,31 @@
# testData/all_route_types.yaml
routingRules:
- domain: ONDC:TRV10
version: 2.0.0
targetType: url
target:
url: https://mock_gateway.com/v2/ondc
endpoints:
- search
- domain: ONDC:TRV10
version: 2.0.0
targetType: bap
target:
url: https://mock_bpp.com/v2/ondc
endpoints:
- init
- select
- domain: ONDC:TRV10
version: 2.0.0
targetType: publisher
target:
publisherId: beckn_onix_topic
endpoints:
- confirm
- domain: ONDC:TRV10
version: 2.0.0
targetType: bap
target:
url: https://mock_bap_gateway.com/v2/ondc
endpoints:
- on_search