Skip to content

Commit

Permalink
[FIXED] Trailing comma at end of URL lists should be ignored
Browse files Browse the repository at this point in the history
Resolves #1056

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
  • Loading branch information
kozlovic committed Aug 30, 2022
1 parent 0797ed6 commit f6c1a78
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
11 changes: 8 additions & 3 deletions nats.go
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
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)
}
}

0 comments on commit f6c1a78

Please sign in to comment.