Resolved review comments

This commit is contained in:
MohitKatare-protean
2025-05-14 16:11:28 +05:30
parent ed32b57a80
commit 20883902fb
2 changed files with 37 additions and 25 deletions

View File

@@ -68,8 +68,7 @@ func GetConnURL(cfg *Config) (string, error) {
if user == "" || pass == "" { if user == "" || pass == "" {
return "", model.NewBadReqErr(fmt.Errorf("missing RabbitMQ credentials in environment")) return "", model.NewBadReqErr(fmt.Errorf("missing RabbitMQ credentials in environment"))
} }
parts := strings.SplitN(strings.TrimSpace(cfg.Addr), "/", 2)
parts := strings.SplitN(cfg.Addr, "/", 2)
hostPort := parts[0] hostPort := parts[0]
vhost := "/" vhost := "/"
if len(parts) > 1 { if len(parts) > 1 {
@@ -93,7 +92,8 @@ func GetConnURL(cfg *Config) (string, error) {
} }
connURL := fmt.Sprintf("%s://%s:%s@%s/%s", protocol, encodedUser, encodedPass, hostPort, encodedVHost) connURL := fmt.Sprintf("%s://%s:%s@%s/%s", protocol, encodedUser, encodedPass, hostPort, encodedVHost)
log.Debugf(context.Background(), "Generated RabbitMQ connection URL: %s", connURL) log.Debugf(context.Background(), "Generated RabbitMQ connection details: protocol=%s, hostPort=%s, vhost=%s", protocol, hostPort, vhost)
return connURL, nil return connURL, nil
} }

View File

@@ -3,7 +3,6 @@ package publisher
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"strings" "strings"
"testing" "testing"
@@ -16,12 +15,13 @@ func TestGetConnURLSuccess(t *testing.T) {
config *Config config *Config
}{ }{
{ {
name: "Valid config with credentials", name: "Valid config with connection address",
config: &Config{ config: &Config{
Addr: "localhost:5672", Addr: "localhost:5672",
UseTLS: false, UseTLS: false,
}, },
}, },
{ {
name: "Valid config with vhost", name: "Valid config with vhost",
config: &Config{ config: &Config{
@@ -29,11 +29,18 @@ func TestGetConnURLSuccess(t *testing.T) {
UseTLS: false, UseTLS: false,
}, },
}, },
{
name: "Addr with leading and trailing spaces",
config: &Config{
Addr: " localhost:5672/myvhost ",
UseTLS: false,
},
},
} }
// Set valid credentials // Set valid credentials
os.Setenv("RABBITMQ_USERNAME", "guest") t.Setenv("RABBITMQ_USERNAME", "guest")
os.Setenv("RABBITMQ_PASSWORD", "guest") t.Setenv("RABBITMQ_PASSWORD", "guest")
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@@ -74,16 +81,12 @@ func TestGetConnURLFailure(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if tt.username == "" { if tt.username != "" {
os.Unsetenv("RABBITMQ_USERNAME") t.Setenv("RABBITMQ_USERNAME", tt.username)
} else {
os.Setenv("RABBITMQ_USERNAME", tt.username)
} }
if tt.password == "" { if tt.password != "" {
os.Unsetenv("RABBITMQ_PASSWORD") t.Setenv("RABBITMQ_PASSWORD", tt.password)
} else {
os.Setenv("RABBITMQ_PASSWORD", tt.password)
} }
url, err := GetConnURL(tt.config) url, err := GetConnURL(tt.config)
@@ -125,22 +128,27 @@ func TestValidateFailure(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
config *Config config *Config
expectedErrr string
}{ }{
{ {
name: "Nil config", name: "Nil config",
config: nil, config: nil,
expectedErrr: "config is nil",
}, },
{ {
name: "Missing Addr", name: "Missing Addr",
config: &Config{Exchange: "ex"}, config: &Config{Exchange: "ex"},
expectedErrr: "missing config.Addr",
}, },
{ {
name: "Missing Exchange", name: "Missing Exchange",
config: &Config{Addr: "localhost:5672"}, config: &Config{Addr: "localhost:5672"},
expectedErrr: "missing config.Exchange",
}, },
{ {
name: "Empty Addr and Exchange", name: "Empty Addr and Exchange",
config: &Config{Addr: " ", Exchange: " "}, config: &Config{Addr: " ", Exchange: " "},
expectedErrr: "missing config.Addr",
}, },
} }
@@ -149,6 +157,10 @@ func TestValidateFailure(t *testing.T) {
err := Validate(tt.config) err := Validate(tt.config)
if err == nil { if err == nil {
t.Errorf("expected error for invalid config, got nil") t.Errorf("expected error for invalid config, got nil")
return
}
if !strings.Contains(err.Error(), tt.expectedErrr) {
t.Errorf("expected error to contain %q, got: %v", tt.expectedErrr, err)
} }
}) })
} }