Skip to content

Commit

Permalink
Merge pull request #1058 from nats-io/fix_1056
Browse files Browse the repository at this point in the history
[FIXED] Trailing comma at end of URL lists should be ignored
  • Loading branch information
kozlovic committed Aug 30, 2022
2 parents 0797ed6 + f6c1a78 commit 6de9cf0
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 6de9cf0

Please sign in to comment.