Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] Trailing comma at end of URL lists should be ignored #1058

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,10 +1268,15 @@ func (nc *Conn) SetErrorHandler(cb ErrHandler) {
// Return an array of urls, even if only one.
func processUrlString(url string) []string {
urls := strings.Split(url, ",")
for i, s := range urls {
urls[i] = strings.TrimSpace(s)
var j int
for _, s := range urls {
u := strings.TrimSpace(s)
if len(u) > 0 {
urls[j] = u
j++
}
}
return urls
return urls[:j]
}

// Connect will attempt to connect to a NATS server with multiple options.
Expand Down
20 changes: 20 additions & 0 deletions nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2920,3 +2920,23 @@ func TestInProcessConn(t *testing.T) {
t.Fatal(err)
}
}

func TestServerListWithTrailingComma(t *testing.T) {
s := RunServerOnPort(-1)
defer s.Shutdown()

// Notice the comma at the end of the "list"
nc, err := Connect(fmt.Sprintf("%s,", s.ClientURL()))
if err != nil {
t.Fatalf("Unable to connect: %v", err)
}
defer nc.Close()

// Now check server pool
nc.mu.Lock()
l := len(nc.srvPool)
nc.mu.Unlock()
if l != 1 {
t.Fatalf("There should be only 1 URL in the list, got %v", l)
}
}